diff --git a/src/hvac/hvac.cpp b/src/hvac/hvac.cpp index c3842f3..4cbd527 100644 --- a/src/hvac/hvac.cpp +++ b/src/hvac/hvac.cpp @@ -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) { @@ -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; } diff --git a/src/hvac/hvac.hpp b/src/hvac/hvac.hpp index 162cfd5..a0119a4 100644 --- a/src/hvac/hvac.hpp +++ b/src/hvac/hvac.hpp @@ -1,7 +1,6 @@ #ifndef HVAC_HPP #define HVAC_HPP -#include "thermostat_context.hpp" #include "thermostat_common.hpp" #include "gpio.hpp" @@ -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 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ba8029f..2239162 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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) { diff --git a/src/subscriber/subscriber.hpp b/src/subscriber/subscriber.hpp index b986069..317f0bf 100644 --- a/src/subscriber/subscriber.hpp +++ b/src/subscriber/subscriber.hpp @@ -1,3 +1,6 @@ +#ifndef SUBSCRIBER_HPP +#define SUBSCRIBER_HPP + #include "wifi.hpp" #include "mqtt.hpp" @@ -12,4 +15,6 @@ class Subscriber { private: Wifi *wifi; Mqtt *mqtt; -}; \ No newline at end of file +}; + +#endif // SUBSCRIBER_HPP \ No newline at end of file diff --git a/src/thermostat_context/thermostat_context.hpp b/src/thermostat_context/thermostat_context.hpp index 917c612..41b2f61 100644 --- a/src/thermostat_context/thermostat_context.hpp +++ b/src/thermostat_context/thermostat_context.hpp @@ -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 { @@ -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 diff --git a/test/hvac/test_hvac.cpp b/test/hvac/test_hvac.cpp index ccb372a..ce0f496 100644 --- a/test/hvac/test_hvac.cpp +++ b/test/hvac/test_hvac.cpp @@ -9,7 +9,6 @@ static Hvac *hvac; static Switch *heater; static Switch *ac; static Switch *fan; -static ThermostatContext *context; TEST_GROUP(HVACTestGroup) { @@ -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); } @@ -37,7 +33,6 @@ TEST_GROUP(HVACTestGroup) delete heater; delete ac; delete fan; - delete context; } };