Skip to content

Commit

Permalink
Removed circular dependencies in the context
Browse files Browse the repository at this point in the history
  • Loading branch information
whatisbyandby committed Jul 4, 2024
1 parent ab5dd41 commit 42c3e29
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 51 deletions.
44 changes: 23 additions & 21 deletions src/hvac/hvac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ Hvac::Hvac()
{
}

ThermostatError Hvac::initialize(ThermostatContext *context)
ThermostatError Hvac::initialize(Switch *heat, Switch *cool, Switch *fan)
{
this->context = context;
currentState = IDLE;
heatSwitch = heat;
coolSwitch = cool;
fanSwitch = fan;
return THERMOSTAT_OK;
}


ThermostatState Hvac::readCurrentState()
{
bool heaterState = context->heatSwitch->isOn();
bool acState = context->coolSwitch->isOn();
bool fanState = context->fanSwitch->isOn();
bool heaterState = heatSwitch->isOn();
bool acState = coolSwitch->isOn();
bool fanState = fanSwitch->isOn();

if (heaterState)
{
Expand All @@ -40,43 +42,43 @@ ThermostatError Hvac::setDesiredState(ThermostatState state)

if (state == IDLE)
{
context->heatSwitch->turnOff();
context->coolSwitch->turnOff();
context->fanSwitch->turnOff();
heatSwitch->turnOff();
coolSwitch->turnOff();
fanSwitch->turnOff();
currentState = readCurrentState();
return THERMOSTAT_OK;
}

if (state == HEATING)
{
context->heatSwitch->turnOn();
context->coolSwitch->turnOff();
context->fanSwitch->turnOff();
heatSwitch->turnOn();
coolSwitch->turnOff();
fanSwitch->turnOff();
currentState = readCurrentState();
return THERMOSTAT_OK;
}

if (state == COOLING)
{
context->heatSwitch->turnOff();
context->coolSwitch->turnOn();
context->fanSwitch->turnOff();
heatSwitch->turnOff();
coolSwitch->turnOn();
fanSwitch->turnOff();
currentState = readCurrentState();
return THERMOSTAT_OK;
}

if (state == FAN_ON)
{
context->heatSwitch->turnOff();
context->coolSwitch->turnOff();
context->fanSwitch->turnOn();
heatSwitch->turnOff();
coolSwitch->turnOff();
fanSwitch->turnOn();
currentState = readCurrentState();
return THERMOSTAT_OK;
}

context->heatSwitch->turnOff();
context->coolSwitch->turnOff();
context->fanSwitch->turnOff();
heatSwitch->turnOff();
coolSwitch->turnOff();
fanSwitch->turnOff();
currentState = readCurrentState();
return THERMOSTAT_ERROR;
}
Expand Down
7 changes: 4 additions & 3 deletions src/hvac/hvac.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef HVAC_HPP
#define HVAC_HPP

#include "thermostat_context.hpp"
#include "thermostat_common.hpp"
#include "gpio.hpp"

Expand All @@ -10,14 +9,16 @@
class Hvac {
public:
Hvac();
ThermostatError initialize(ThermostatContext *context);
ThermostatError initialize(Switch *heat, Switch *cool, Switch *fan);
ThermostatError setDesiredState(ThermostatState state);
ThermostatState getCurrentState();

private:
ThermostatState currentState;
ThermostatState readCurrentState();
ThermostatContext *context;
Switch *heatSwitch;
Switch *coolSwitch;
Switch *fanSwitch;
};

#endif // HVAC_HPP
4 changes: 1 addition & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,12 @@ int main()
context.mqtt = &mqtt;
context.watchdog = &watchdog;
context.producer = &producer;
context.thermostat = &thermostat;
context.commandParser = &commandParser;
context.repl = &repl;


ThermostatError err = context.initialize();

ThermostatError err = thermostat.initialize(&context);

err = thermostat.connect();

while (true) {
Expand Down
7 changes: 6 additions & 1 deletion src/subscriber/subscriber.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef SUBSCRIBER_HPP
#define SUBSCRIBER_HPP

#include "wifi.hpp"
#include "mqtt.hpp"

Expand All @@ -12,4 +15,6 @@ class Subscriber {
private:
Wifi *wifi;
Mqtt *mqtt;
};
};

#endif // SUBSCRIBER_HPP
45 changes: 29 additions & 16 deletions src/thermostat_context/thermostat_context.hpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
#ifndef THERMOSTAT_CONTEXT_HPP
#define THERMOSTAT_CONTEXT_HPP

class Thermostat;
class Switch;
class TemperatureController;
class Hvac;
class Watchdog;
class I2CBus;
class I2CDevice;
class CommandParser;
class EnvironmentSensor;
class Repl;
class Producer;
class Configuration;
class Wifi;
class Mqtt;

#include "thermostat_common.hpp"
#include "thermostat.hpp"
#include "gpio.hpp"
#include "temperature_controller.hpp"
#include "hvac.hpp"
#include "watchdog.hpp"
#include "i2c_bus.hpp"
#include "command_parser.hpp"
#include "environment_sensor.hpp"
#include "repl.hpp"
#include "producer.hpp"
#include "configuration.hpp"
#include "wifi.hpp"
#include "mqtt.hpp"

class ThermostatContext
{
Expand All @@ -39,7 +38,21 @@ class ThermostatContext
Repl *repl;

Producer *producer;
Thermostat *thermostat;

ThermostatError initialize() {
sensor->initialize(i2cDevice);
tempController->initialize();
hvac->initialize(heatSwitch, coolSwitch, fanSwitch);
wifi->initialize(config);
mqtt->initialize(config);
watchdog->initialize();
producer->initalize(mqtt);
config->load();
i2cBus->initialize();
i2cDevice->initialize(i2cBus, 0x38);
repl->initialize(commandParser);
return THERMOSTAT_OK;
}
};

#endif // THERMOSTAT_CONTEXT_HPP
9 changes: 2 additions & 7 deletions test/hvac/test_hvac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ static Hvac *hvac;
static Switch *heater;
static Switch *ac;
static Switch *fan;
static ThermostatContext *context;

TEST_GROUP(HVACTestGroup)
{
Expand All @@ -19,13 +18,10 @@ TEST_GROUP(HVACTestGroup)
ac = new Switch(2);
fan = new Switch(3);
hvac = new Hvac();
context = new ThermostatContext();

context->heatSwitch = heater;
context->coolSwitch = ac;
context->fanSwitch = fan;

hvac->initialize(context);

hvac->initialize(heater, ac, fan);

}

Expand All @@ -37,7 +33,6 @@ TEST_GROUP(HVACTestGroup)
delete heater;
delete ac;
delete fan;
delete context;
}
};

Expand Down

0 comments on commit 42c3e29

Please sign in to comment.