diff --git a/edge/build.gradle.kts b/edge/build.gradle.kts index ef3c6cb..c770f5e 100644 --- a/edge/build.gradle.kts +++ b/edge/build.gradle.kts @@ -11,5 +11,21 @@ tasks.register("testRealHw") { } tasks.register("upload") { - commandLine("pio", "run", "-e", "nodemcuv2", "-t", "upload") + + val envs: List = File("platformio.ini").readLines().filter{ + it.matches(Regex("\\[env:.*\\]")) + }.map{ + it.substringAfter("[env:").substringBefore("]") + }.filter { + it != "native" && it != "nodemcuv2" + } + println("Please select the environment to upload: [0-" + (envs.size - 1).toString() + "]\n") + for ((i, item) in envs.withIndex()) { + println(i.toString() + ". " + item) + } + + val userInput = readLine()?.toIntOrNull() + if(userInput != null && userInput >= 0 && userInput < envs.size) { + commandLine("pio", "run", "-e", envs[userInput], "-t", "upload") + } } diff --git a/edge/lib/brittany-concrete/src/digital-light/hw/DigitalLightHw.cpp b/edge/lib/brittany-concrete/src/digital-light/hw/DigitalLightHw.cpp index 33e081b..007ee76 100644 --- a/edge/lib/brittany-concrete/src/digital-light/hw/DigitalLightHw.cpp +++ b/edge/lib/brittany-concrete/src/digital-light/hw/DigitalLightHw.cpp @@ -1,7 +1,7 @@ #include "DigitalLightHw.h" #include -DigitalLightHw::DigitalLightHw(std::string id, uint8_t pin) : DigitalLightHwInterface(id, pin) { +DigitalLightHw::DigitalLightHw(std::string id, uint8_t pin) : DigitalLightHwInterface(id), OnePin(pin) { pinMode(pin, OUTPUT); } diff --git a/edge/lib/brittany-concrete/src/digital-light/hw/DigitalLightHw.h b/edge/lib/brittany-concrete/src/digital-light/hw/DigitalLightHw.h index 1e12d5b..3667c0a 100644 --- a/edge/lib/brittany-concrete/src/digital-light/hw/DigitalLightHw.h +++ b/edge/lib/brittany-concrete/src/digital-light/hw/DigitalLightHw.h @@ -2,12 +2,13 @@ #define BRITTANY_DIGITAL_LIGHT_HW_H #include "hw/interfaces/DigitalLightHwInterface.h" +#include "hw/feature/OnePin.h" /** * @brief Concrete implementation of a Digital Light component. * */ -class DigitalLightHw : public DigitalLightHwInterface { +class DigitalLightHw : public DigitalLightHwInterface, public OnePin { public: diff --git a/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/modules/DHT22Module.h b/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/modules/DHT22Module.h index 30df621..7c171d7 100644 --- a/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/modules/DHT22Module.h +++ b/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/modules/DHT22Module.h @@ -16,18 +16,10 @@ class DHT22Module : public ComponentModule { DHT22Module(std::string name, std::list components): ComponentModule(name, components) { _handlers.push_back( - new DHT22GetTemperatureHandler( - DHT22_GET_TEMPERATURE_HANDLER_NAME, - as_route(DHT22_GET_TEMPERATURE_HANDLER_NAME), - components - ) + new DHT22GetTemperatureHandler(DHT22_GET_TEMPERATURE_HANDLER_NAME, components) ); _handlers.push_back( - new DHT22GetHumidityHandler( - DHT22_GET_HUMIDITY_HANDLER_NAME, - as_route(DHT22_GET_HUMIDITY_HANDLER_NAME), - components - ) + new DHT22GetHumidityHandler(DHT22_GET_HUMIDITY_HANDLER_NAME, components) ); }; diff --git a/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/operation-handler/DHT22GetHumidityHandler.cpp b/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/operation-handler/DHT22GetHumidityHandler.cpp index 036c386..a9d6836 100644 --- a/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/operation-handler/DHT22GetHumidityHandler.cpp +++ b/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/operation-handler/DHT22GetHumidityHandler.cpp @@ -3,9 +3,8 @@ DHT22GetHumidityHandler::DHT22GetHumidityHandler( std::string name, - std::string path, std::list components -): DHT22Handler(name, path, components) { +): DHT22Handler(name, components) { } diff --git a/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/operation-handler/DHT22GetHumidityHandler.h b/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/operation-handler/DHT22GetHumidityHandler.h index fd12c9e..ed74ad6 100644 --- a/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/operation-handler/DHT22GetHumidityHandler.h +++ b/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/operation-handler/DHT22GetHumidityHandler.h @@ -12,7 +12,6 @@ class DHT22GetHumidityHandler : public DHT22Handler { DHT22GetHumidityHandler( std::string name, - std::string path, std::list components ); diff --git a/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/operation-handler/DHT22GetTemperatureHandler.cpp b/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/operation-handler/DHT22GetTemperatureHandler.cpp index 059fa95..bc1d947 100644 --- a/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/operation-handler/DHT22GetTemperatureHandler.cpp +++ b/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/operation-handler/DHT22GetTemperatureHandler.cpp @@ -3,9 +3,8 @@ DHT22GetTemperatureHandler::DHT22GetTemperatureHandler( std::string name, - std::string path, std::list components -): DHT22Handler(name, path, components) { +): DHT22Handler(name, components) { } diff --git a/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/operation-handler/DHT22GetTemperatureHandler.h b/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/operation-handler/DHT22GetTemperatureHandler.h index 3a25b10..24f0476 100644 --- a/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/operation-handler/DHT22GetTemperatureHandler.h +++ b/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/operation-handler/DHT22GetTemperatureHandler.h @@ -10,11 +10,7 @@ class DHT22GetTemperatureHandler : public DHT22Handler { public: - DHT22GetTemperatureHandler( - std::string name, - std::string path, - std::list components - ); + DHT22GetTemperatureHandler(std::string name, std::list components); private: diff --git a/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/operation-handler/DHT22Handler.h b/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/operation-handler/DHT22Handler.h index a59649c..28fffe4 100644 --- a/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/operation-handler/DHT22Handler.h +++ b/edge/lib/brittany-concrete/src/temp-hum-sensor/dht22/operation-handler/DHT22Handler.h @@ -3,34 +3,22 @@ #include #include -#include "operation-handler/interfaces/ValueReturnedAfterActionHandlerInterface.h" +#include "operation-handler/interfaces/RetrieveValueFromComponentInterface.h" #include "temp-hum-sensor/dht22/hw/DHT22SensorHw.h" -class DHT22Handler : public ValueReturnedAfterActionHandlerInterface { +class DHT22Handler : public RetrieveValueFromComponentInterface { public: - DHT22Handler(std::string name, std::string path, std::list components) - : ValueReturnedAfterActionHandlerInterface (name, path, OperationType::PROPERTY, Type::NUMBER) { - _components = components; + DHT22Handler(std::string name, std::list components) + : RetrieveValueFromComponentInterface (name, components) { + }; private: - std::optional retrieveValue(Json::Value args) { - std::optional oc = find_by_id(_components, args["id"].asCString()); - if(oc.has_value()) { - std::optional opt_value = sub_operation(oc.value(), args); - if(opt_value.has_value()) { - return opt_value.value(); - } - } - return std::nullopt; - } - virtual std::optional sub_operation(DHT22SensorHw* hw, Json::Value args) = 0; - std::list _components; }; #endif //BRITTANY_DHT22_HANDLER_H diff --git a/edge/lib/brittany-concrete/src/web-server/Esp8266WebServer.h b/edge/lib/brittany-concrete/src/web-server/Esp8266WebServer.h index 28308d7..2d06d60 100644 --- a/edge/lib/brittany-concrete/src/web-server/Esp8266WebServer.h +++ b/edge/lib/brittany-concrete/src/web-server/Esp8266WebServer.h @@ -3,10 +3,11 @@ #include "web-server/WebServer.h" #include "edge/Edge.h" -#include "ESP8266WebServer.h" +#include #include "HttpStatusCodes_C++.h" #include "json_util.h" #include "util/DebugPrint.h" +#include /** * @brief ESP8266WebServer implementation of web server. diff --git a/edge/lib/brittany-mock/src/mock-digital-light/hw/MockDigitalLightHw.h b/edge/lib/brittany-mock/src/mock-digital-light/hw/MockDigitalLightHw.h index fe0cde6..912bf85 100644 --- a/edge/lib/brittany-mock/src/mock-digital-light/hw/MockDigitalLightHw.h +++ b/edge/lib/brittany-mock/src/mock-digital-light/hw/MockDigitalLightHw.h @@ -12,7 +12,9 @@ class MockDigitalLightHw : public DigitalLightHwInterface { public: - MockDigitalLightHw(std::string id, uint8_t pin) : DigitalLightHwInterface(id, pin){ } + MockDigitalLightHw(std::string id) : DigitalLightHwInterface(id) { + _isOn = false; + } void on() { _isOn = true; diff --git a/edge/lib/brittany-mock/src/mock-digital-light/modules/MockDigitalLightModule.h b/edge/lib/brittany-mock/src/mock-digital-light/modules/MockDigitalLightModule.h index aac5f1c..336e960 100644 --- a/edge/lib/brittany-mock/src/mock-digital-light/modules/MockDigitalLightModule.h +++ b/edge/lib/brittany-mock/src/mock-digital-light/modules/MockDigitalLightModule.h @@ -8,37 +8,17 @@ #include "mock-digital-light/operation-handler/MockIsOnDigitalLightHandler.h" #include "mock-digital-light/operation-handler/MockTurnOffDigitalLightHandler.h" #include "mock-digital-light/operation-handler/MockTurnOnDigitalLightHandler.h" - -#define MOCK_IS_ON_HANDLER_MODULE_NAME "isOn" -#define MOCK_TURN_ON_HANDLER_MODULE_NAME "turnOn" -#define MOCK_TURN_OFF_HANDLER_MODULE_NAME "turnOff" +#include "modules/ModuleNames.h" class MockDigitalLightModule : public ComponentModule { public: - MockDigitalLightModule(std::string name, std::list components): ComponentModule(name, components) { - _handlers.push_back( - new MockIsOnDigitalLightHandler( - MOCK_IS_ON_HANDLER_MODULE_NAME, - as_route(MOCK_IS_ON_HANDLER_MODULE_NAME), - components - ) - ); - _handlers.push_back( - new MockTurnOnDigitalLightHandler( - MOCK_TURN_ON_HANDLER_MODULE_NAME, - as_route(MOCK_TURN_ON_HANDLER_MODULE_NAME), - components - ) - ); - _handlers.push_back( - new MockTurnOffDigitalLightHandler( - MOCK_TURN_OFF_HANDLER_MODULE_NAME, - as_route(MOCK_TURN_OFF_HANDLER_MODULE_NAME), - components - ) - ); + MockDigitalLightModule(std::list components) + : ComponentModule(module_as_string(ModuleNames::Light), components) { + _handlers.push_back(new MockIsOnDigitalLightHandler(components)); + _handlers.push_back(new MockTurnOnDigitalLightHandler(components)); + _handlers.push_back(new MockTurnOffDigitalLightHandler(components)); }; }; diff --git a/edge/lib/brittany-mock/src/mock-digital-light/operation-handler/MockIsOnDigitalLightHandler.h b/edge/lib/brittany-mock/src/mock-digital-light/operation-handler/MockIsOnDigitalLightHandler.h index 125839c..f576ac0 100644 --- a/edge/lib/brittany-mock/src/mock-digital-light/operation-handler/MockIsOnDigitalLightHandler.h +++ b/edge/lib/brittany-mock/src/mock-digital-light/operation-handler/MockIsOnDigitalLightHandler.h @@ -4,42 +4,20 @@ #include #include #include -#include "operation-handler/interfaces/ValueReturnedAfterActionHandlerInterface.h" +#include "operation-handler/interfaces/components/IsOnSwitchableHandler.h" #include "mock-digital-light/hw/MockDigitalLightHw.h" #include "operation-handler/OperationHandlerResult.h" #include "HttpStatusCodes_C++.h" #include "util.h" #include -class MockIsOnDigitalLightHandler : public ValueReturnedAfterActionHandlerInterface { +class MockIsOnDigitalLightHandler: public IsOnSwitchableHandler { public: - MockIsOnDigitalLightHandler( - std::string name, - std::string path, - std::list components - ): ValueReturnedAfterActionHandlerInterface( - name, - path, - OperationType::PROPERTY, - Type::BOOLEAN - ) { - _components = components; + MockIsOnDigitalLightHandler(std::list components) + : IsOnSwitchableHandler("isOn", components) { }; - -private: - - std::optional retrieveValue(Json::Value args) { - std::optional oc = find_by_id(_components, args["id"].asCString()); - if(oc.has_value()){ - return std::optional(oc.value() -> isOn()); - } - return std::nullopt; - } - - std::list _components; - }; #endif //BRITTANY_MOCK_IS_ON_DIGITAL_LIGHT_HANDLER_INTERFACE_H diff --git a/edge/lib/brittany-mock/src/mock-digital-light/operation-handler/MockTurnOffDigitalLightHandler.h b/edge/lib/brittany-mock/src/mock-digital-light/operation-handler/MockTurnOffDigitalLightHandler.h index d1362d0..c34474c 100644 --- a/edge/lib/brittany-mock/src/mock-digital-light/operation-handler/MockTurnOffDigitalLightHandler.h +++ b/edge/lib/brittany-mock/src/mock-digital-light/operation-handler/MockTurnOffDigitalLightHandler.h @@ -3,38 +3,19 @@ #include #include -#include -#include "operation-handler/interfaces/TurnOffHandlerInterface.h" -#include "operation-handler/interfaces/TurnOffHandlerInterface.cpp" +#include "operation-handler/interfaces/components/TurnOffSwitchableHandler.h" #include "mock-digital-light/hw/MockDigitalLightHw.h" #include "operation-handler/OperationHandlerResult.h" #include "util.h" -class MockTurnOffDigitalLightHandler : public TurnOffHandlerInterface { +class MockTurnOffDigitalLightHandler: public TurnOffSwitchableHandler { public: - MockTurnOffDigitalLightHandler( - std::string name, - std::string path, - std::list components - ): TurnOffHandlerInterface(name, path) { - _components = components; - }; - -private: + MockTurnOffDigitalLightHandler(std::list components) + : TurnOffSwitchableHandler("turnOff", components) { - bool turnOff(std::string id) { - std::optional oc = find_by_id(_components, id); - if(oc.has_value()) { - oc.value() -> off(); - return true; - } else { - return false; - } }; - - std::list _components; }; diff --git a/edge/lib/brittany-mock/src/mock-digital-light/operation-handler/MockTurnOnDigitalLightHandler.h b/edge/lib/brittany-mock/src/mock-digital-light/operation-handler/MockTurnOnDigitalLightHandler.h index b6dab08..6b91d63 100644 --- a/edge/lib/brittany-mock/src/mock-digital-light/operation-handler/MockTurnOnDigitalLightHandler.h +++ b/edge/lib/brittany-mock/src/mock-digital-light/operation-handler/MockTurnOnDigitalLightHandler.h @@ -3,39 +3,19 @@ #include #include -#include -#include "operation-handler/interfaces/TurnOnHandlerInterface.h" -#include "operation-handler/interfaces/TurnOnHandlerInterface.cpp" +#include "operation-handler/interfaces/components/TurnOnSwitchableHandler.h" #include "mock-digital-light/hw/MockDigitalLightHw.h" #include "operation-handler/OperationHandlerResult.h" #include "util.h" -class MockTurnOnDigitalLightHandler : public TurnOnHandlerInterface { +class MockTurnOnDigitalLightHandler: public TurnOnSwitchableHandler { public: - MockTurnOnDigitalLightHandler( - std::string name, - std::string path, - std::list components - ): TurnOnHandlerInterface(name, path) { - _components = components; - }; - -private: - - bool turnOn(std::string id) { - std::optional oc = find_by_id(_components, id); - if(oc.has_value()) { - oc.value() -> on(); - return true; - } else { - return false; - } - }; - - std::list _components; + MockTurnOnDigitalLightHandler(std::list components) + : TurnOnSwitchableHandler("turnOn", components) { + } }; #endif //BRITTANY_MOCK_TURN_ON_DIGITAL_LIGHT_HANDLER_INTERFACE_H diff --git a/edge/lib/brittany-mock/src/mock-humidity-actuator/modules/MockDehumidifierModule.h b/edge/lib/brittany-mock/src/mock-humidity-actuator/modules/MockDehumidifierModule.h new file mode 100644 index 0000000..920a7ff --- /dev/null +++ b/edge/lib/brittany-mock/src/mock-humidity-actuator/modules/MockDehumidifierModule.h @@ -0,0 +1,40 @@ +#ifndef BRITTANY_MOCK_DEHUMIDIFIER_MODULE_H +#define BRITTANY_MOCK_DEHUMIDIFIER_MODULE_H + +#include +#include "operation-handler/interfaces/components/TurnOffSwitchableHandler.h" +#include "operation-handler/interfaces/components/TurnOnSwitchableHandler.h" +#include "operation-handler/interfaces/components/IsOnSwitchableHandler.h" +#include "mock-switchable/hw/MockSwitchableHw.h" +#include "modules/ComponentModule.h" +#include "modules/ModuleNames.h" + +class MockDehumidifierModule : public ComponentModule { + +public: + + MockDehumidifierModule(std::list components) + : ComponentModule(module_as_string(ModuleNames::Humidity), components) { + _handlers.push_back( + new TurnOnSwitchableHandler( + "dehumidifyOn", + components + ) + ); + _handlers.push_back( + new TurnOffSwitchableHandler( + "dehumidifyOff", + components + ) + ); + _handlers.push_back( + new IsOnSwitchableHandler( + "isDehumidifyOn", + components + ) + ); + }; + +}; + +#endif //BRITTANY_MOCK_DEHUMIDIFIER_MODULE_H \ No newline at end of file diff --git a/edge/lib/brittany-mock/src/mock-humidity-actuator/modules/MockHumidifierModule.h b/edge/lib/brittany-mock/src/mock-humidity-actuator/modules/MockHumidifierModule.h new file mode 100644 index 0000000..a3dd978 --- /dev/null +++ b/edge/lib/brittany-mock/src/mock-humidity-actuator/modules/MockHumidifierModule.h @@ -0,0 +1,40 @@ +#ifndef BRITTANY_MOCK_HUMIDIFIER_MODULE_H +#define BRITTANY_MOCK_HUMIDIFIER_MODULE_H + +#include +#include "operation-handler/interfaces/components/TurnOffSwitchableHandler.h" +#include "operation-handler/interfaces/components/TurnOnSwitchableHandler.h" +#include "operation-handler/interfaces/components/IsOnSwitchableHandler.h" +#include "mock-switchable/hw/MockSwitchableHw.h" +#include "modules/ComponentModule.h" +#include "modules/ModuleNames.h" + +class MockHumidifierModule : public ComponentModule { + +public: + + MockHumidifierModule(std::list components) + : ComponentModule(module_as_string(ModuleNames::Humidity), components) { + _handlers.push_back( + new TurnOnSwitchableHandler( + "humidifyOn", + components + ) + ); + _handlers.push_back( + new TurnOffSwitchableHandler( + "humidifyOff", + components + ) + ); + _handlers.push_back( + new IsOnSwitchableHandler( + "isHumidifyOn", + components + ) + ); + }; + +}; + +#endif //BRITTANY_MOCK_HUMIDIFIER_MODULE_H \ No newline at end of file diff --git a/edge/lib/brittany-mock/src/mock-humidity-sensor/hw/MockHumiditySensorHw.h b/edge/lib/brittany-mock/src/mock-humidity-sensor/hw/MockHumiditySensorHw.h new file mode 100644 index 0000000..d388930 --- /dev/null +++ b/edge/lib/brittany-mock/src/mock-humidity-sensor/hw/MockHumiditySensorHw.h @@ -0,0 +1,28 @@ +#ifndef MOCK_HUMIDITY_SENSOR_HW_H +#define MOCK_HUMIDITY_SENSOR_HW_H + +#include "hw/interfaces/sensors/HumiditySensorHwInterface.h" +#include "util.h" + +#define MOCK_MAX_HUMIDITY 100 + +/** + * @brief Mock Implementation of a humidity sensor hw. + */ +class MockHumiditySensorHw : public HumiditySensorHwInterface { + +public: + + /** + * @brief Construct a new MockHumiditySensorHw object. + * + * @param id the hw id. + */ + MockHumiditySensorHw(std::string id) : HumiditySensorHwInterface(id) { }; + + std::optional humidity() { + return std::optional(random(0, MOCK_MAX_HUMIDITY)); + } + +}; +#endif //MOCK_HUMIDITY_SENSOR_HW_H diff --git a/edge/lib/brittany-mock/src/mock-humidity-sensor/modules/MockHumidityModule.h b/edge/lib/brittany-mock/src/mock-humidity-sensor/modules/MockHumidityModule.h new file mode 100644 index 0000000..86ebba8 --- /dev/null +++ b/edge/lib/brittany-mock/src/mock-humidity-sensor/modules/MockHumidityModule.h @@ -0,0 +1,22 @@ +#ifndef BRITTANY_MOCK_HUMIDITY_MODULE_H +#define BRITTANY_MOCK_HUMIDITY_MODULE_H + +#include +#include "operation-handler/OperationHandler.h" +#include "mock-humidity-sensor/hw/MockHumiditySensorHw.h" +#include "mock-humidity-sensor/operation-handler/MockHumidityHandler.h" +#include "modules/ComponentModule.h" +#include "modules/ModuleNames.h" + +class MockHumidityModule : public ComponentModule { + +public: + + MockHumidityModule(std::list components) + : ComponentModule(module_as_string(ModuleNames::Humidity), components) { + _handlers.push_back(new MockHumidityHandler(components)); + }; + +}; + +#endif //BRITTANY_MOCK_HUMIDITY_MODULE_H \ No newline at end of file diff --git a/edge/lib/brittany-mock/src/mock-humidity-sensor/operation-handler/MockHumidityHandler.h b/edge/lib/brittany-mock/src/mock-humidity-sensor/operation-handler/MockHumidityHandler.h new file mode 100644 index 0000000..c2b3f7c --- /dev/null +++ b/edge/lib/brittany-mock/src/mock-humidity-sensor/operation-handler/MockHumidityHandler.h @@ -0,0 +1,26 @@ +#ifndef MOCK_HUMIDITY_HANDLER_H +#define MOCK_HUMIDITY_HANDLER_H + +#include +#include +#include "operation-handler/interfaces/RetrieveValueFromComponentInterface.h" +#include "mock-humidity-sensor/hw/MockHumiditySensorHw.h" + +class MockHumidityHandler : public RetrieveValueFromComponentInterface { + +public: + + MockHumidityHandler(std::list components) + : RetrieveValueFromComponentInterface ("airHumidity", components) { + + }; + +private: + + std::optional sub_operation(MockHumiditySensorHw* hw, Json::Value args) { + return hw-> humidity(); + } + +}; + +#endif // MOCK_HUMIDITY_HANDLER_H \ No newline at end of file diff --git a/edge/lib/brittany-mock/src/mock-switchable/hw/MockSwitchableHw.h b/edge/lib/brittany-mock/src/mock-switchable/hw/MockSwitchableHw.h new file mode 100644 index 0000000..fd3f89f --- /dev/null +++ b/edge/lib/brittany-mock/src/mock-switchable/hw/MockSwitchableHw.h @@ -0,0 +1,41 @@ +#ifndef MOCK_SWITCHABLE_HW_H +#define MOCK_SWITCHABLE_HW_H + +#include +#include "hw/interfaces/actuators/SwitchableActuatorHwInterface.h" + +/** + * @brief Represent a MockHw that can be turned on and off. + */ +class MockSwitchableHw : public SwitchableActuatorHwInterface { + +public: + + /** + * @brief Construct a new Mock Switchable Hw object. + * + * @param id the id of the hw. + */ + MockSwitchableHw(std::string id) : SwitchableActuatorHwInterface(id) { + _isOn = false; + }; + + void on() { + _isOn = true; + } + + void off() { + _isOn = false; + } + + bool isOn() { + return _isOn; + } + +private: + + bool _isOn; + +}; + +#endif //MOCK_SWITCHABLE_HW_H diff --git a/edge/lib/brittany-mock/src/mock-temp-hum-sensor/hw/MockTempHumSensorHw.h b/edge/lib/brittany-mock/src/mock-temp-hum-sensor/hw/MockTempHumSensorHw.h deleted file mode 100644 index 7117b80..0000000 --- a/edge/lib/brittany-mock/src/mock-temp-hum-sensor/hw/MockTempHumSensorHw.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef BRITTANY_MOCK_TEMP_HUM_SENSOR_HW_H -#define BRITTANY_MOCK_TEMP_HUM_SENSOR_HW_H - -#include "hw/interfaces/TempHumHwInterface.h" -#include -#include - -/** - * @brief Mock Implemenattion of a Mock temperature and Humidity sensor. - * - */ -class MockTempHumSensorHw : public TempHumHwInterface { - -public: - - /** - * @brief Construct a new Mock Temp Hum Sensor Hw object - * - * @param id the hw id. - * @param pin the hw pin. - * @param celsius_value the mock value to return as celsius. - * @param humidity_value the mock value to return as humidity. - */ - MockTempHumSensorHw( - std::string id, - uint8_t pin, - std::optional celsius_value, - std::optional humidity_value) : TempHumHwInterface(id, pin) { - _celsius_value = celsius_value; - _humidity_value = humidity_value; - }; - - std::optional temperatureCelsius() { - return _celsius_value; - }; - - std::optional temperatureKelvin() { - return temperatureCelsius().has_value() - ? std::optional(Temperature::fromCToK(temperatureCelsius().value())) - : std::nullopt; - }; - - std::optional temperatureFahrenheit() { - return temperatureCelsius().has_value() - ? std::optional(Temperature::fromCToF(temperatureCelsius().value())) - : std::nullopt; - }; - - std::optional humidity() { - return _humidity_value; - }; - -private: - - std::optional _celsius_value; - std::optional _humidity_value; - -}; -#endif //BRITTANY_MOCK_TEMP_HUM_SENSOR_HW_H diff --git a/edge/lib/brittany-mock/src/mock-temp-hum-sensor/operaion-handler/MockGetTemperatureHandler.h b/edge/lib/brittany-mock/src/mock-temp-hum-sensor/operaion-handler/MockGetTemperatureHandler.h deleted file mode 100644 index 37b1850..0000000 --- a/edge/lib/brittany-mock/src/mock-temp-hum-sensor/operaion-handler/MockGetTemperatureHandler.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef BRITTANY_DHT22_GET_TEMPERATURE_HANDLER_H -#define BRITTANY_DHT22_GET_TEMPERATURE_HANDLER_H - -#include -#include - -class DHT22GetTemperatureHandler : public DHT22Handler { - -public: - - DHT22GetTemperatureHandler( - std::string name, - std::string path, - std::list components - ); - -private: - - std::optional sub_operation(DHT22SensorHw* hw, Json::Value args); - - std::list _components; -}; - -#endif //BRITTANY_DHT22_GET_TEMPERATURE_HANDLER_H diff --git a/edge/lib/brittany-mock/src/mock-temperature-actuator/modules/MockTemperatureCoolerModule.h b/edge/lib/brittany-mock/src/mock-temperature-actuator/modules/MockTemperatureCoolerModule.h new file mode 100644 index 0000000..9b1160e --- /dev/null +++ b/edge/lib/brittany-mock/src/mock-temperature-actuator/modules/MockTemperatureCoolerModule.h @@ -0,0 +1,41 @@ +#ifndef BRITTANY_MOCK_TEMPERATURE_COOLER_MODULE_H +#define BRITTANY_MOCK_TEMPERATURE_COOLER_MODULE_H + +#include + +#include "operation-handler/interfaces/components/TurnOffSwitchableHandler.h" +#include "operation-handler/interfaces/components/TurnOnSwitchableHandler.h" +#include "operation-handler/interfaces/components/IsOnSwitchableHandler.h" +#include "mock-switchable/hw/MockSwitchableHw.h" +#include "modules/ComponentModule.h" +#include "modules/ModuleNames.h" + +class MockTemperatureCoolerModule : public ComponentModule { + +public: + + MockTemperatureCoolerModule(std::list components) + : ComponentModule(module_as_string(ModuleNames::Temperature), components) { + _handlers.push_back( + new TurnOnSwitchableHandler( + "coolOn", + components + ) + ); + _handlers.push_back( + new TurnOffSwitchableHandler( + "coolOff", + components + ) + ); + _handlers.push_back( + new IsOnSwitchableHandler( + "isCoolOn", + components + ) + ); + }; + +}; + +#endif //BRITTANY_MOCK_TEMPERATURE_COOLER_MODULE_H \ No newline at end of file diff --git a/edge/lib/brittany-mock/src/mock-temperature-actuator/modules/MockTemperatureHeaterModule.h b/edge/lib/brittany-mock/src/mock-temperature-actuator/modules/MockTemperatureHeaterModule.h new file mode 100644 index 0000000..af31921 --- /dev/null +++ b/edge/lib/brittany-mock/src/mock-temperature-actuator/modules/MockTemperatureHeaterModule.h @@ -0,0 +1,41 @@ +#ifndef BRITTANY_MOCK_TEMPERATURE_HEATER_MODULE_H +#define BRITTANY_MOCK_TEMPERATURE_HEATER_MODULE_H + +#include + +#include "operation-handler/interfaces/components/TurnOffSwitchableHandler.h" +#include "operation-handler/interfaces/components/TurnOnSwitchableHandler.h" +#include "operation-handler/interfaces/components/IsOnSwitchableHandler.h" +#include "mock-switchable/hw/MockSwitchableHw.h" +#include "modules/ComponentModule.h" +#include "modules/ModuleNames.h" + +class MockTemperatureHeaterModule : public ComponentModule { + +public: + + MockTemperatureHeaterModule(std::list components) + : ComponentModule(module_as_string(ModuleNames::Temperature), components) { + _handlers.push_back( + new TurnOnSwitchableHandler( + "heatOn", + components + ) + ); + _handlers.push_back( + new TurnOffSwitchableHandler( + "heatOff", + components + ) + ); + _handlers.push_back( + new IsOnSwitchableHandler( + "isHeatOn", + components + ) + ); + }; + +}; + +#endif //BRITTANY_MOCK_TEMPERATURE_HEATER_MODULE_H \ No newline at end of file diff --git a/edge/lib/brittany-mock/src/mock-temperature-sensor/hw/MockTemperatureSensorHw.h b/edge/lib/brittany-mock/src/mock-temperature-sensor/hw/MockTemperatureSensorHw.h new file mode 100644 index 0000000..64b6c99 --- /dev/null +++ b/edge/lib/brittany-mock/src/mock-temperature-sensor/hw/MockTemperatureSensorHw.h @@ -0,0 +1,43 @@ +#ifndef MOCK_TEMPERATURE_SENSOR_HW_H +#define MOCK_TEMPERATURE_SENSOR_HW_H + +#include "hw/interfaces/sensors/TemperatureSensorHwInterface.h" + +#define MOCK_MAX_TEMPERATURE_CELSIUS 40 + +/** + * @brief Mock Implementation of a temperature sensor hw. + */ +class MockTemperatureSensorHw : public TemperatureSensorHwInterface { + +public: + + /** + * @brief Construct a new MockTemperatureSensorHw object. + * + * @param id the hw id. + */ + MockTemperatureSensorHw(std::string id) : TemperatureSensorHwInterface(id) { }; + + std::optional temperatureCelsius() { + return std::optional(random(0, MOCK_MAX_TEMPERATURE_CELSIUS)); + } + + std::optional temperatureKelvin() { + std::optional tempC = temperatureCelsius(); + if(tempC.has_value()){ + return std::optional(Temperature::fromCToK(tempC.value())); + } + return std::nullopt; + } + + std::optional temperatureFahrenheit(){ + std::optional tempC = temperatureCelsius(); + if(tempC.has_value()){ + return std::optional(Temperature::fromCToF(tempC.value())); + } + return std::nullopt; + } + +}; +#endif //MOCK_TEMPERATURE_SENSOR_HW_H diff --git a/edge/lib/brittany-mock/src/mock-temperature-sensor/modules/MockTemperatureModule.h b/edge/lib/brittany-mock/src/mock-temperature-sensor/modules/MockTemperatureModule.h new file mode 100644 index 0000000..02e4397 --- /dev/null +++ b/edge/lib/brittany-mock/src/mock-temperature-sensor/modules/MockTemperatureModule.h @@ -0,0 +1,22 @@ +#ifndef BRITTANY_MOCK_TEMPERATURE_MODULE_H +#define BRITTANY_MOCK_TEMPERATURE_MODULE_H + +#include +#include "operation-handler/OperationHandler.h" +#include "mock-temperature-sensor/hw/MockTemperatureSensorHw.h" +#include "mock-temperature-sensor/operation-handler/MockTemperatureHandler.h" +#include "modules/ComponentModule.h" +#include "modules/ModuleNames.h" + +class MockTemperatureModule : public ComponentModule { + +public: + + MockTemperatureModule(std::list components) + : ComponentModule(module_as_string(ModuleNames::Temperature), components) { + _handlers.push_back(new MockTemperatureHandler(components)); + }; + +}; + +#endif //BRITTANY_MOCK_TEMPERATURE_MODULE_H \ No newline at end of file diff --git a/edge/lib/brittany-mock/src/mock-temperature-sensor/operation-handler/MockTemperatureHandler.h b/edge/lib/brittany-mock/src/mock-temperature-sensor/operation-handler/MockTemperatureHandler.h new file mode 100644 index 0000000..67cccab --- /dev/null +++ b/edge/lib/brittany-mock/src/mock-temperature-sensor/operation-handler/MockTemperatureHandler.h @@ -0,0 +1,36 @@ +#ifndef BRITTANY_MOCK_TEMPERATURE_HANDLER_H +#define BRITTANY_MOCK_TEMPERATURE_HANDLER_H + +#include +#include +#include "operation-handler/interfaces/RetrieveValueFromComponentInterface.h" +#include "mock-temperature-sensor/hw/MockTemperatureSensorHw.h" + +class MockTemperatureHandler : public RetrieveValueFromComponentInterface { + +public: + + MockTemperatureHandler(std::list components) + : RetrieveValueFromComponentInterface ("temperature", components) { + + }; + +private: + + std::optional sub_operation(MockTemperatureSensorHw* hw, Json::Value args) { + if(args.isMember("unit")) { + if(args["unit"] == "C" || args["unit"] == "c") { + return hw -> temperatureCelsius(); + } else if(args["unit"] == "K" || args["unit"] == "k") { + return hw -> temperatureKelvin(); + } else if(args["unit"] == "F" || args["unit"] == "f") { + return hw -> temperatureFahrenheit(); + } + return std::nullopt; + } + return hw -> temperatureCelsius(); + } + +}; + +#endif // BRITTANY_MOCK_TEMPERATURE_HANDLER_H \ No newline at end of file diff --git a/edge/lib/brittany-mock/src/mock/modules/MockModule.h b/edge/lib/brittany-mock/src/mock/modules/MockModule.h index 32f2450..60a0df0 100644 --- a/edge/lib/brittany-mock/src/mock/modules/MockModule.h +++ b/edge/lib/brittany-mock/src/mock/modules/MockModule.h @@ -18,12 +18,7 @@ class MockModule : public Module { public: MockModule(std::string name): Module(name) { - _handlers.push_back( - new MockOperationHandler( - OPERATION_HANDLER_IN_MOCK_MODULE_NAME, - as_route(OPERATION_HANDLER_IN_MOCK_MODULE_NAME) - ) - ); + _handlers.push_back(new MockOperationHandler()); }; }; diff --git a/edge/lib/brittany-mock/src/mock/operation-handler/MockArgsOperationHandler.h b/edge/lib/brittany-mock/src/mock/operation-handler/MockArgsOperationHandler.h deleted file mode 100644 index 514d2fa..0000000 --- a/edge/lib/brittany-mock/src/mock/operation-handler/MockArgsOperationHandler.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef BRITTANY_MOCK_ARGS_OPERATION_HANDLER_H -#define BRITTANY_MOCK_ARGS_OPERATION_HANDLER_H - -#include -#include -#include "operation-handler/OperationHandlerResult.h" -#include "operation-handler/OperationHandler.h" -#include "HttpStatusCodes_C++.h" -#include "json_util.h" - -/** - * @brief Class that represents a mock OperationHandler that sends back - * the arguments as a JSON string used only for testing purposes. - */ -class MockArgsOperationHandler: public OperationHandler { - -public: - - MockArgsOperationHandler(std::string name, std::string path) : - OperationHandler(name, path, OperationType::PROPERTY, Type::INTEGER) { - - }; - - OperationHandlerResult handle(Json::Value args) { - const std::string output = stringify(args); - stdOutStrategy(output.c_str()); - return OperationHandlerResult(HttpStatus::OK, output); - }; - -private: - - /** - * @brief Describe how to print to the std output the content. - * - * @param content The content to print on the std output. - */ - virtual void stdOutStrategy(std::string content) = 0; - -}; - -#endif //BRITTANY_OPERATION_HANDLER_H \ No newline at end of file diff --git a/edge/lib/brittany-mock/src/mock/operation-handler/MockOperationHandler.h b/edge/lib/brittany-mock/src/mock/operation-handler/MockOperationHandler.h index f9ced7c..3e34ad9 100644 --- a/edge/lib/brittany-mock/src/mock/operation-handler/MockOperationHandler.h +++ b/edge/lib/brittany-mock/src/mock/operation-handler/MockOperationHandler.h @@ -14,8 +14,8 @@ class MockOperationHandler: public OperationHandler { public: - MockOperationHandler(std::string name, std::string path) : - OperationHandler(name, path, OperationType::PROPERTY, Type::INTEGER) { + MockOperationHandler() : + OperationHandler("value", OperationType::PROPERTY, Type::INTEGER) { _value = 0; }; diff --git a/edge/lib/brittany/src/hw/interfaces/DigitalLightHwInterface.h b/edge/lib/brittany/src/hw/interfaces/DigitalLightHwInterface.h index 2c23429..4a96806 100644 --- a/edge/lib/brittany/src/hw/interfaces/DigitalLightHwInterface.h +++ b/edge/lib/brittany/src/hw/interfaces/DigitalLightHwInterface.h @@ -8,9 +8,8 @@ /** * @brief An interface to create a component that represent a digital light. - * */ -class DigitalLightHwInterface : public ComponentHw, public OnePin, public Switchable { +class DigitalLightHwInterface : public ComponentHw, public Switchable { public: @@ -18,11 +17,9 @@ class DigitalLightHwInterface : public ComponentHw, public OnePin, public Switch * @brief Construct a new Digital Light Hw Interface object. * * @param id the unique id of the component. - * @param pin the pin attached to the component. */ - DigitalLightHwInterface(std::string id, uint8_t pin) : + DigitalLightHwInterface(std::string id) : ComponentHw(id), - OnePin(pin), Switchable() { }; diff --git a/edge/lib/brittany/src/hw/interfaces/actuators/SwitchableActuatorHwInterface.h b/edge/lib/brittany/src/hw/interfaces/actuators/SwitchableActuatorHwInterface.h new file mode 100644 index 0000000..e135115 --- /dev/null +++ b/edge/lib/brittany/src/hw/interfaces/actuators/SwitchableActuatorHwInterface.h @@ -0,0 +1,28 @@ +#ifndef BRITTANY_SWITCHABLE_ACTUATOR_HW_INTERFACE_H +#define BRITTANY_SWITCHABLE_ACTUATOR_HW_INTERFACE_H + +#include +#include "hw/ComponentHw.h" +#include "hw/feature/Switchable.h" + +/** + * @brief An interface to create a component that represent an actuator that can be turned on and off. + */ +class SwitchableActuatorHwInterface : public ComponentHw, public Switchable { + +public: + + /** + * @brief Construct a new Switchable Actuator Hw Interface object. + * + * @param id + */ + SwitchableActuatorHwInterface(std::string id) : + ComponentHw(id), + Switchable() { + + }; + +}; + +#endif //BRITTANY_SWITCHABLE_ACTUATOR_HW_INTERFACE_H \ No newline at end of file diff --git a/edge/lib/brittany/src/hw/interfaces/sensors/HumiditySensorHwInterface.h b/edge/lib/brittany/src/hw/interfaces/sensors/HumiditySensorHwInterface.h new file mode 100644 index 0000000..4e8dd25 --- /dev/null +++ b/edge/lib/brittany/src/hw/interfaces/sensors/HumiditySensorHwInterface.h @@ -0,0 +1,26 @@ +#ifndef BRITTANY_HUMIDITY_SENSOR_HW_INTERFACE_H +#define BRITTANY_HUMIDITY_SENSOR_HW_INTERFACE_H + +#include +#include "hw/ComponentHw.h" +#include "hw/feature/Humidity.h" + +/** + * @brief An interface to create a component that represents a sensor that can + * obtain humidity(air) values. + */ +class HumiditySensorHwInterface : + public ComponentHw, + public Humidity { + +public: + + HumiditySensorHwInterface(std::string id) : + ComponentHw(id), + Humidity() { + + }; + +}; + +#endif //BRITTANY_HUMIDITY_SENSOR_HW_INTERFACE_H \ No newline at end of file diff --git a/edge/lib/brittany/src/hw/interfaces/sensors/TemperatureSensorHwInterface.h b/edge/lib/brittany/src/hw/interfaces/sensors/TemperatureSensorHwInterface.h new file mode 100644 index 0000000..f364812 --- /dev/null +++ b/edge/lib/brittany/src/hw/interfaces/sensors/TemperatureSensorHwInterface.h @@ -0,0 +1,26 @@ +#ifndef BRITTANY_TEMPERATURE_SENSOR_HW_INTERFACE_H +#define BRITTANY_TEMPERATURE_SENSOR_HW_INTERFACE_H + +#include +#include "hw/ComponentHw.h" +#include "hw/feature/Temperature.h" + +/** + * @brief An interface to create a component that represents a sensor that can + * obtain temperature(air) values. + */ +class TemperatureSensorHwInterface : + public ComponentHw, + public Temperature{ + +public: + + TemperatureSensorHwInterface(std::string id) : + ComponentHw(id), + Temperature() { + + }; + +}; + +#endif //BRITTANY_TEMPERATURE_SENSOR_HW_INTERFACE_H \ No newline at end of file diff --git a/edge/lib/brittany/src/modules/ModuleNames.h b/edge/lib/brittany/src/modules/ModuleNames.h new file mode 100644 index 0000000..a335df8 --- /dev/null +++ b/edge/lib/brittany/src/modules/ModuleNames.h @@ -0,0 +1,37 @@ +#ifndef MODULE_NAMES_H +#define MODULE_NAMES_H + +#include "hw/ComponentHw.h" +#include +#include +#include + +/** + * @brief Enum to define the module names used by the system. + */ +enum class ModuleNames { + Temperature, + Humidity, + Light +}; + +/** + * @brief Return the Module name as a string. + * + * @param moduleName the ModuleNames value. + * @return std::string A string that represent the passed value. + */ +inline std::string module_as_string(ModuleNames moduleName) { + switch (moduleName) { + case ModuleNames::Temperature: + return "temperature"; + case ModuleNames::Humidity: + return "airHumidity"; + case ModuleNames::Light: + return "light"; + default: + return ""; + } +} + +#endif // MODULE_NAMES_H \ No newline at end of file diff --git a/edge/lib/brittany/src/operation-handler/HandlerNames.h b/edge/lib/brittany/src/operation-handler/HandlerNames.h new file mode 100644 index 0000000..e69de29 diff --git a/edge/lib/brittany/src/operation-handler/OperationHandler.cpp b/edge/lib/brittany/src/operation-handler/OperationHandler.cpp index 0e436bd..7193df8 100644 --- a/edge/lib/brittany/src/operation-handler/OperationHandler.cpp +++ b/edge/lib/brittany/src/operation-handler/OperationHandler.cpp @@ -1,4 +1,5 @@ #include "OperationHandler.h" +#include "util.h" OperationHandler::OperationHandler( std::string name, @@ -12,6 +13,19 @@ OperationHandler::OperationHandler( _outputType = outputType; } + +OperationHandler::OperationHandler( + std::string name, + OperationType operationType, + Type outputType +) { + _name = name; + _path = as_route(name); + _operationType = operationType; + _outputType = outputType; +} + + std::string OperationHandler::name() { return _name; } diff --git a/edge/lib/brittany/src/operation-handler/OperationHandler.h b/edge/lib/brittany/src/operation-handler/OperationHandler.h index d1d6490..e46f40d 100644 --- a/edge/lib/brittany/src/operation-handler/OperationHandler.h +++ b/edge/lib/brittany/src/operation-handler/OperationHandler.h @@ -31,6 +31,20 @@ class OperationHandler { Type outputType ); + + /** + * @brief Construct a new Operation Handler object. + * + * @param name The name of the OperationHandler. Will also be the path with a "/" in front. + * @param operationTye The operation type of the OperationHandler. + * @param outputType The type of the Output. + */ + OperationHandler( + std::string name, + OperationType operationType, + Type outputType + ); + /** * @brief Return the name of the OperationHandler. * diff --git a/edge/lib/brittany/src/operation-handler/interfaces/RetrieveValueFromComponentInterface.h b/edge/lib/brittany/src/operation-handler/interfaces/RetrieveValueFromComponentInterface.h new file mode 100644 index 0000000..7e354d5 --- /dev/null +++ b/edge/lib/brittany/src/operation-handler/interfaces/RetrieveValueFromComponentInterface.h @@ -0,0 +1,73 @@ +#ifndef RETRIEVE_VALUE_FROM_COMPONENT_INTERFACE_H +#define RETRIEVE_VALUE_FROM_COMPONENT_INTERFACE_H + +#include +#include +#include "operation-handler/interfaces/ValueReturnedAfterActionHandlerInterface.h" + +template + +class RetrieveValueFromComponentInterface : public ValueReturnedAfterActionHandlerInterface { + +public: + + /** + * @brief Construct a new Retrieve Value From Component Interface object. + * + * @param name the name of the operation handler. + * @param operationType the operation type. + * @param returnedType the returned type. + * @param components the list of components. + */ + RetrieveValueFromComponentInterface( + std::string name, + OperationType operationType, + Type returnedType, + std::list components + ) + : ValueReturnedAfterActionHandlerInterface ( + name, + operationType, + returnedType + ) { + _components = components; + }; + + /** + * @brief Construct a new Retrieve Value From Component Interface object. + * Simplified Constructor that set the handler to a property and return a number. + * + * @param name the name of the operation handler. + * @param components the list of components. + */ + RetrieveValueFromComponentInterface( + std::string name, + std::list components + ) + : ValueReturnedAfterActionHandlerInterface ( + name, + OperationType::PROPERTY, + Type::NUMBER + ) { + _components = components; + }; + +private: + + std::optional retrieveValue(Json::Value args) { + std::optional oc = find_by_id(_components, args["id"].asCString()); + if(oc.has_value()) { + std::optional opt_value = sub_operation(oc.value(), args); + if(opt_value.has_value()) { + return opt_value.value(); + } + } + return std::nullopt; + } + + virtual std::optional sub_operation(C* hw, Json::Value args) = 0; + + std::list _components; +}; + +#endif //RETRIEVE_VALUE_FROM_COMPONENT_INTERFACE_H diff --git a/edge/lib/brittany/src/operation-handler/interfaces/TurnOffHandlerInterface.cpp b/edge/lib/brittany/src/operation-handler/interfaces/TurnOffHandlerInterface.cpp deleted file mode 100644 index 50fc45a..0000000 --- a/edge/lib/brittany/src/operation-handler/interfaces/TurnOffHandlerInterface.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "TurnOffHandlerInterface.h" -#include "HttpStatusCodes_C++.h" -#include "util.h" - -TurnOffHandlerInterface::TurnOffHandlerInterface(std::string name, std::string path) -: ValueReturnedAfterActionHandlerInterface(name, path, OperationType::ACTION, Type::STRING) { - //does nothing -} - - std::optional TurnOffHandlerInterface::retrieveValue(Json::Value args) { - if(turnOff(args["id"].asCString())){ - return std::optional(phrase(ContentResult::Ok)); - } - return std::nullopt; -} \ No newline at end of file diff --git a/edge/lib/brittany/src/operation-handler/interfaces/TurnOffHandlerInterface.h b/edge/lib/brittany/src/operation-handler/interfaces/TurnOffHandlerInterface.h deleted file mode 100644 index d87db89..0000000 --- a/edge/lib/brittany/src/operation-handler/interfaces/TurnOffHandlerInterface.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef BRITTANY_TURN_OFF_HANDLER_INTERFACE_H -#define BRITTANY_TURN_OFF_HANDLER_INTERFACE_H - -#include -#include -#include -#include -#include "operation-handler/interfaces/ValueReturnedAfterActionHandlerInterface.h" - -/** - * @brief Operation Handler Interface whose objective is to turn off a component. - */ -class TurnOffHandlerInterface : public ValueReturnedAfterActionHandlerInterface { - -public: - - TurnOffHandlerInterface(std::string name, std::string path); - - - /** - * @brief Calls the turnOff method. - * - * @param args the arguments passed from operation. - * @return std::optional a string with an "Ok." message if everything worked. - * Empty otherise. - */ - std::optional retrieveValue(Json::Value args); - - /** - * @brief turn off the chosen component. - * - * @param id the id of the component to turn off. - * @return true if the operation succeed. - * @return false if the operation fails. - */ - virtual bool turnOff(std::string id) = 0; - -}; - -#endif //BRITTANY_TURN_OFF_HANDLER_INTERFACE_H diff --git a/edge/lib/brittany/src/operation-handler/interfaces/TurnOnHandlerInterface.cpp b/edge/lib/brittany/src/operation-handler/interfaces/TurnOnHandlerInterface.cpp deleted file mode 100644 index dca594e..0000000 --- a/edge/lib/brittany/src/operation-handler/interfaces/TurnOnHandlerInterface.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "TurnOnHandlerInterface.h" -#include "HttpStatusCodes_C++.h" -#include "util.h" - -TurnOnHandlerInterface::TurnOnHandlerInterface(std::string name, std::string path) -: ValueReturnedAfterActionHandlerInterface(name, path, OperationType::ACTION, Type::STRING) { - //does nothing -} - -std::optional TurnOnHandlerInterface::retrieveValue(Json::Value args) { - if(turnOn(args["id"].asCString())) { - return std::optional(phrase(ContentResult::Ok)); - } - return std::nullopt; -} diff --git a/edge/lib/brittany/src/operation-handler/interfaces/TurnOnHandlerInterface.h b/edge/lib/brittany/src/operation-handler/interfaces/TurnOnHandlerInterface.h deleted file mode 100644 index 61a5224..0000000 --- a/edge/lib/brittany/src/operation-handler/interfaces/TurnOnHandlerInterface.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef BRITTANY_TURN_ON_HANDLER_INTERFACE_H -#define BRITTANY_TURN_ON_HANDLER_INTERFACE_H - -#include -#include -#include -#include -#include "operation-handler/interfaces/ValueReturnedAfterActionHandlerInterface.h" - -/** - * @brief Operation Handler Interface whose objective is to turn on a component. - * - */ -class TurnOnHandlerInterface : public ValueReturnedAfterActionHandlerInterface { - -public: - - TurnOnHandlerInterface(std::string name,std::string path); - - /** - * @brief Calls the turnOn method. - * - * @param args the arguments passed from operation. - * @return std::optional a string with an "Ok." message if everything worked. - * Empty otherise. - */ - std::optional retrieveValue(Json::Value args); - - /** - * @brief turn on the chosen component. - * - * @param id the id of the component to turn on. - * @return true if the operation succeed. - * @return false if the operation fails. - */ - virtual bool turnOn(std::string id) = 0; - -}; - -#endif //BRITTANY_TURN_ON_HANDLER_INTERFACE_H diff --git a/edge/lib/brittany/src/operation-handler/interfaces/ValueReturnedAfterActionHandlerInterface.h b/edge/lib/brittany/src/operation-handler/interfaces/ValueReturnedAfterActionHandlerInterface.h index dbe7927..dbefa0f 100644 --- a/edge/lib/brittany/src/operation-handler/interfaces/ValueReturnedAfterActionHandlerInterface.h +++ b/edge/lib/brittany/src/operation-handler/interfaces/ValueReturnedAfterActionHandlerInterface.h @@ -25,18 +25,15 @@ class ValueReturnedAfterActionHandlerInterface : public ValueReturnedHandlerInte * @brief Construct a new Value Returned After Action Handler Interface object. * * @param name the name of the handler. - * @param path the path of the handler. * @param operationType the operationType of the handler. * @param outputType the output type of the handler. */ ValueReturnedAfterActionHandlerInterface( std::string name, - std::string path, OperationType operationType, Type outputType ) : ValueReturnedHandlerInterface( name, - path, operationType, outputType ) {}; diff --git a/edge/lib/brittany/src/operation-handler/interfaces/ValueReturnedHandlerInterface.h b/edge/lib/brittany/src/operation-handler/interfaces/ValueReturnedHandlerInterface.h index e56af51..e4e8798 100644 --- a/edge/lib/brittany/src/operation-handler/interfaces/ValueReturnedHandlerInterface.h +++ b/edge/lib/brittany/src/operation-handler/interfaces/ValueReturnedHandlerInterface.h @@ -29,16 +29,13 @@ class ValueReturnedHandlerInterface : public OperationHandler { * @brief Construct a new Value Returned Handler Interface object * * @param name the name of the handler. - * @param path the path(route) of the handler. */ ValueReturnedHandlerInterface( std::string name, - std::string path, OperationType operationType, Type outputType ) : OperationHandler( name, - path, operationType, outputType ) { diff --git a/edge/lib/brittany/src/operation-handler/interfaces/components/IsOnSwitchableHandler.h b/edge/lib/brittany/src/operation-handler/interfaces/components/IsOnSwitchableHandler.h new file mode 100644 index 0000000..4e8d2fa --- /dev/null +++ b/edge/lib/brittany/src/operation-handler/interfaces/components/IsOnSwitchableHandler.h @@ -0,0 +1,36 @@ +#ifndef BRITTANY_IS_ON_SWITCHABLE_HANDLER_H +#define BRITTANY_IS_ON_SWITCHABLE_HANDLER_H + +#include +#include +#include +#include "operation-handler/interfaces/RetrieveValueFromComponentInterface.h" +#include "util.h" + +template + +class IsOnSwitchableHandler: public RetrieveValueFromComponentInterface { + +public: + + IsOnSwitchableHandler( + std::string name, + std::list components + ): RetrieveValueFromComponentInterface( + name, + OperationType::PROPERTY, + Type::BOOLEAN, + components + ) { + + } + +private: + + std::optional sub_operation(C* hw, Json::Value args) { + return std::optional(hw -> isOn()); + } + +}; + +#endif //BRITTANY_IS_ON_SWITCHABLE_HANDLER_H diff --git a/edge/lib/brittany/src/operation-handler/interfaces/components/TurnOffSwitchableHandler.h b/edge/lib/brittany/src/operation-handler/interfaces/components/TurnOffSwitchableHandler.h new file mode 100644 index 0000000..68734f9 --- /dev/null +++ b/edge/lib/brittany/src/operation-handler/interfaces/components/TurnOffSwitchableHandler.h @@ -0,0 +1,38 @@ +#ifndef BRITTANY_TURN_OFF_SWITCHABLE_HANDLER_H +#define BRITTANY_TURN_OFF_SWITCHABLE_HANDLER_H + +#include +#include +#include +#include "operation-handler/interfaces/RetrieveValueFromComponentInterface.h" +#include "util.h" + +template + +class TurnOffSwitchableHandler +: public RetrieveValueFromComponentInterface { + +public: + + TurnOffSwitchableHandler( + std::string name, + std::list components + ): RetrieveValueFromComponentInterface( + name, + OperationType::ACTION, + Type::STRING, + components + ) { + + } + +private: + + std::optional sub_operation(C* hw, Json::Value args) { + hw -> off(); + return std::optional(phrase(ContentResult::Ok)); + }; + +}; + +#endif //BRITTANY_TURN_OFF_SWITCHABLE_HANDLER_H diff --git a/edge/lib/brittany/src/operation-handler/interfaces/components/TurnOnSwitchableHandler.h b/edge/lib/brittany/src/operation-handler/interfaces/components/TurnOnSwitchableHandler.h new file mode 100644 index 0000000..a391049 --- /dev/null +++ b/edge/lib/brittany/src/operation-handler/interfaces/components/TurnOnSwitchableHandler.h @@ -0,0 +1,38 @@ +#ifndef BRITTANY_TURN_ON_SWITCHABLE_HANDLER_H +#define BRITTANY_TURN_ON_SWITCHABLE_HANDLER_H + +#include +#include +#include +#include "operation-handler/interfaces/RetrieveValueFromComponentInterface.h" +#include "util.h" + +template + +class TurnOnSwitchableHandler +: public RetrieveValueFromComponentInterface { + +public: + + TurnOnSwitchableHandler( + std::string name, + std::list components + ): RetrieveValueFromComponentInterface( + name, + OperationType::ACTION, + Type::STRING, + components + ) { + + } + +private: + + std::optional sub_operation(C* hw, Json::Value args) { + hw -> on(); + return std::optional(phrase(ContentResult::Ok)); + }; + +}; + +#endif //BRITTANY_TURN_ON_SWITCHABLE_HANDLER_H diff --git a/edge/lib/brittany/src/thing-descriptor/ThingDescriptorBuilder.h b/edge/lib/brittany/src/thing-descriptor/ThingDescriptorBuilder.h index 93d3682..b8d3406 100644 --- a/edge/lib/brittany/src/thing-descriptor/ThingDescriptorBuilder.h +++ b/edge/lib/brittany/src/thing-descriptor/ThingDescriptorBuilder.h @@ -7,6 +7,7 @@ #include "modules/ComponentModule.h" #include "hw/ComponentHw.h" #include +#include using namespace TD; @@ -50,15 +51,26 @@ class ThingDescriptorBuilder { } static void add_modules(Json::Value &td, std::list modules) { - int i, j = 0; + std::unordered_map> map; for(Module* m : modules) { - td["modules"][0]["module"] = m -> name(); ComponentModule* cm = static_cast*>(m); - j = 0; - for(ComponentHw h : cm -> components()) { - td["modules"][0]["components"][j++] = h.id(); + std::list components; + if(map.find(cm -> name()) == map.end()) { + components = cm -> components(); + } else { + components = map[cm -> name()]; + components.splice(components.end(), cm -> components()); + } + map.insert_or_assign(cm -> name(), components); + } + int i = 0; + for(const std::pair> n : map) { + td["modules"][i]["module"] = n.first.c_str(); + int j = 0; + for(ComponentHw c : n.second) { + td["modules"][i]["components"][j++] = c.id().c_str(); } - i++; + i++; } } @@ -73,10 +85,7 @@ class ThingDescriptorBuilder { std::string("http://") + ip + ":" + std::to_string(port) + h -> path() + "?id=" + c.id(); object["forms"][0]["contentType"] = "application/json"; std::string objectName; - std::string name = h -> name(); - if(cm->components().size() != 1) { - name += "-" + c.id(); - } + std::string name = h -> name() + "-" + c.id(); std::string type = type_to_string(h -> outputType()); switch (h -> operationType()) { case OperationType::ACTION: diff --git a/edge/lib/brittany/src/util.h b/edge/lib/brittany/src/util.h index d2a8733..6bef109 100644 --- a/edge/lib/brittany/src/util.h +++ b/edge/lib/brittany/src/util.h @@ -5,6 +5,7 @@ #include #include #include +#include enum class ContentResult { Ok, @@ -68,4 +69,15 @@ inline std::optional find_by_id(std::list list, std::string id) { return std::nullopt; } +/** + * @brief Return a random number from min to max (inclusive) + * + * @param min the min number that could be returned. + * @param max the max number that could be returned. + * @return int the random number. + */ +inline int random(int min, int max) { + return min + rand() % (( max + 1 ) - min); +} + #endif //BRITTANY_UTIL_H \ No newline at end of file diff --git a/edge/platformio.ini b/edge/platformio.ini index b2de5a0..cc0c61e 100644 --- a/edge/platformio.ini +++ b/edge/platformio.ini @@ -44,4 +44,16 @@ build_flags = -D BRITTANY_MAIN_MOCK_DIGITAL_LIGHT [env:dht22] extends = env:nodemcuv2 -build_flags = -D BRITTANY_MAIN_DHT22 \ No newline at end of file +build_flags = -D BRITTANY_MAIN_DHT22 + +[env:mock-temperature-humidity-sensor] +extends = env:nodemcuv2 +build_flags = -D BRITTANY_MAIN_MOCK_TEMPERATURE_HUMIDITY_SENSOR + +[env:mock-temperature-humidity-actuator] +extends = env:nodemcuv2 +build_flags = -D BRITTANY_MAIN_MOCK_TEMPERATURE_HUMIDITY_ACTUATOR + +[env:mock-temperature-humidity] +extends = env:nodemcuv2 +build_flags = -D BRITTANY_MAIN_MOCK_TEMPERATURE_HUMIDITY diff --git a/edge/src/main.cpp b/edge/src/main.cpp index 3680852..6c7b234 100644 --- a/edge/src/main.cpp +++ b/edge/src/main.cpp @@ -4,8 +4,14 @@ #include "web-server/Esp8266WebServer.h" #include "wifi_secret.h" #include "modules/Module.h" -#include "modules/dht22.h" -#include "modules/mock-digital-light.h" + +#include + +#include "modules/concrete/concrete-dht22.h" +#include "modules/mock/mock-digital-light.h" +#include "modules/mock/mock-temperature-humidity.h" +#include "modules/mock/mock-temperature-humidity-sensor.h" +#include "modules/mock/mock-temperature-humidity-actuator.h" Esp8266WebServer* server; diff --git a/edge/src/modules/dht22.h b/edge/src/modules/concrete/concrete-dht22.h similarity index 100% rename from edge/src/modules/dht22.h rename to edge/src/modules/concrete/concrete-dht22.h diff --git a/edge/src/modules/mock-digital-light.h b/edge/src/modules/mock/mock-digital-light.h similarity index 64% rename from edge/src/modules/mock-digital-light.h rename to edge/src/modules/mock/mock-digital-light.h index f2d46c6..febd1d7 100644 --- a/edge/src/modules/mock-digital-light.h +++ b/edge/src/modules/mock/mock-digital-light.h @@ -4,11 +4,11 @@ #include "mock-digital-light/modules/MockDigitalLightModule.h" Edge* edge() { - MockDigitalLightHw* light0 = new MockDigitalLightHw("0", 0); - MockDigitalLightHw* light1 = new MockDigitalLightHw("1", 1); + MockDigitalLightHw* light0 = new MockDigitalLightHw("0"); + MockDigitalLightHw* light1 = new MockDigitalLightHw("1"); std::list lights = std::list({light0, light1}); std::list modules; - modules.push_back(new MockDigitalLightModule("light", lights)); + modules.push_back(new MockDigitalLightModule(lights)); return new Edge("Mock Digital Light Edge", modules); } diff --git a/edge/src/modules/mock/mock-temperature-humidity-actuator.h b/edge/src/modules/mock/mock-temperature-humidity-actuator.h new file mode 100644 index 0000000..96861c2 --- /dev/null +++ b/edge/src/modules/mock/mock-temperature-humidity-actuator.h @@ -0,0 +1,24 @@ +#ifdef BRITTANY_MAIN_MOCK_TEMPERATURE_HUMIDITY_ACTUATOR + +#include "edge/Edge.h" +#include "mock-switchable/hw/MockSwitchableHw.h" +#include "mock-temperature-actuator/modules/MockTemperatureCoolerModule.h" +#include "mock-temperature-actuator/modules/MockTemperatureHeaterModule.h" +#include "mock-humidity-actuator/modules/MockHumidifierModule.h" +#include "mock-humidity-actuator/modules/MockDehumidifierModule.h" + +Edge* edge() { + MockSwitchableHw* heater = new MockSwitchableHw("heater"); + MockSwitchableHw* cooler = new MockSwitchableHw("cooler"); + MockSwitchableHw* humidifier = new MockSwitchableHw("humidifier"); + MockSwitchableHw* dehumidifier = new MockSwitchableHw("dehumidifier"); + std::list modules; + modules.push_back(new MockTemperatureHeaterModule({heater})); + modules.push_back(new MockTemperatureCoolerModule({cooler})); + modules.push_back(new MockHumidifierModule({humidifier})); + modules.push_back(new MockDehumidifierModule({dehumidifier})); + std::string edge_desc = "Mock Temperature Edge that can increase and lower the temperature and the humidity."; + return new Edge(edge_desc, modules); +} + +#endif \ No newline at end of file diff --git a/edge/src/modules/mock/mock-temperature-humidity-sensor.h b/edge/src/modules/mock/mock-temperature-humidity-sensor.h new file mode 100644 index 0000000..ffa2c51 --- /dev/null +++ b/edge/src/modules/mock/mock-temperature-humidity-sensor.h @@ -0,0 +1,17 @@ +#ifdef BRITTANY_MAIN_MOCK_TEMPERATURE_HUMIDITY_SENSOR + +#include "mock-temperature-sensor/hw/MockTemperatureSensorHw.h" +#include "mock-temperature-sensor/modules/MockTemperatureModule.h" +#include "mock-humidity-sensor/hw/MockHumiditySensorHw.h" +#include "mock-humidity-sensor/modules/MockHumidityModule.h" + +Edge* edge() { + MockHumiditySensorHw* humSensor = new MockHumiditySensorHw("hum"); + MockTemperatureSensorHw* tempSensor = new MockTemperatureSensorHw("temp"); + std::list modules; + modules.push_back(new MockHumidityModule({humSensor})); + modules.push_back(new MockTemperatureModule({tempSensor})); + return new Edge("Mock Humidity and Temperature Edge", modules); +} + +#endif \ No newline at end of file diff --git a/edge/src/modules/mock/mock-temperature-humidity.h b/edge/src/modules/mock/mock-temperature-humidity.h new file mode 100644 index 0000000..fdf4958 --- /dev/null +++ b/edge/src/modules/mock/mock-temperature-humidity.h @@ -0,0 +1,31 @@ +#ifdef BRITTANY_MAIN_MOCK_TEMPERATURE_HUMIDITY + +#include "mock-switchable/hw/MockSwitchableHw.h" +#include "mock-temperature-actuator/modules/MockTemperatureCoolerModule.h" +#include "mock-temperature-actuator/modules/MockTemperatureHeaterModule.h" +#include "mock-humidity-actuator/modules/MockHumidifierModule.h" +#include "mock-humidity-actuator/modules/MockDehumidifierModule.h" +#include "mock-temperature-sensor/hw/MockTemperatureSensorHw.h" +#include "mock-temperature-sensor/modules/MockTemperatureModule.h" +#include "mock-humidity-sensor/hw/MockHumiditySensorHw.h" +#include "mock-humidity-sensor/modules/MockHumidityModule.h" + +Edge* edge() { + MockSwitchableHw* heater = new MockSwitchableHw("heater"); + MockSwitchableHw* cooler = new MockSwitchableHw("cooler"); + MockSwitchableHw* humidifier = new MockSwitchableHw("humidifier"); + MockSwitchableHw* dehumidifier = new MockSwitchableHw("dehumidifier"); + MockHumiditySensorHw* humSensor = new MockHumiditySensorHw("hum"); + MockTemperatureSensorHw* tempSensor = new MockTemperatureSensorHw("temp"); + std::list modules; + modules.push_back(new MockTemperatureHeaterModule({heater})); + modules.push_back(new MockTemperatureCoolerModule({cooler})); + modules.push_back(new MockHumidifierModule({humidifier})); + modules.push_back(new MockDehumidifierModule({dehumidifier})); + modules.push_back(new MockHumidityModule({humSensor})); + modules.push_back(new MockTemperatureModule({tempSensor})); + std::string edge_desc = "Mock Temperature Edge for handling temperature and humidity."; + return new Edge(edge_desc, modules); +} + +#endif \ No newline at end of file diff --git a/edge/test/brittany/edge/EdgeTest.h b/edge/test/brittany/edge/EdgeTest.h index 5f86a74..24b355a 100644 --- a/edge/test/brittany/edge/EdgeTest.h +++ b/edge/test/brittany/edge/EdgeTest.h @@ -8,10 +8,8 @@ #define EDGE_MOCK_TITLE "MockEdge" #define EDGE_MOCK_MODULE_NAME "mock-module" -#define EDGE_MOCK_DIGITAL_LIGHT_MODULE_NAME "light-module" #define MOCK_LIGHT_IN_EDGE_NAME "light" -#define MOCK_LIGHT_IN_EDGE_PIN 5 #define TESTING_EXECUTE_ATTEMPT 10 #define INCREMENT_VALUE_TEST 6 @@ -27,15 +25,15 @@ void test_string_element_is_in_list(std::list list, std::string str void test_light_module_available_path(Edge* edge) { test_string_element_is_in_list( edge -> availablePaths(), - as_route(MOCK_IS_ON_LIGHT_NAME) + "/isOn" ); test_string_element_is_in_list( edge -> availablePaths(), - as_route(MOCK_TURN_OFF_LIGHT_NAME) + "/turnOff" ); test_string_element_is_in_list( edge -> availablePaths(), - as_route(MOCK_TURN_ON_LIGHT_NAME) + "/turnOn" ); } @@ -47,10 +45,7 @@ void test_mock_module_available_path(Edge* edge) { } void setup_test_edge() { - mockLightInEdge = new MockDigitalLightHw( - MOCK_LIGHT_IN_EDGE_NAME, - MOCK_LIGHT_IN_EDGE_PIN - ); + mockLightInEdge = new MockDigitalLightHw(MOCK_LIGHT_IN_EDGE_NAME); mockDigitalLights.push_back(mockLightInEdge); } @@ -94,10 +89,10 @@ void test_edge_execute_working(Edge* edge) { TEST_ASSERT_EQUAL(INCREMENT_VALUE_TEST, result0.content().asInt()); Json::Value args; args["id"] = MOCK_LIGHT_IN_EDGE_NAME; - auto result1 = edge -> execute(as_route(MOCK_TURN_ON_HANDLER_MODULE_NAME), args); + auto result1 = edge -> execute("/turnOn", args); check_edge_result_code_is_ok(result1); TEST_ASSERT_EQUAL_STRING(phrase(ContentResult::Ok).c_str(), result1.content().asCString()); - auto result = edge -> execute(as_route(MOCK_IS_ON_HANDLER_MODULE_NAME), args); + auto result = edge -> execute("/isOn", args); check_edge_result_code_is_ok(result); TEST_ASSERT_TRUE(result.content().asBool()); } @@ -115,7 +110,7 @@ void test_edge_list() { Edge* edge = new Edge(EDGE_MOCK_TITLE, std::list({ new MockModule(EDGE_MOCK_MODULE_NAME), - new MockDigitalLightModule(EDGE_MOCK_DIGITAL_LIGHT_MODULE_NAME, mockDigitalLights) + new MockDigitalLightModule(mockDigitalLights) }) ); test_edge_title(edge); diff --git a/edge/test/brittany/edge/ThingDescriptorTest.h b/edge/test/brittany/edge/ThingDescriptorTest.h index 7d95fa5..593fe90 100644 --- a/edge/test/brittany/edge/ThingDescriptorTest.h +++ b/edge/test/brittany/edge/ThingDescriptorTest.h @@ -5,15 +5,16 @@ #include "mock-digital-light/modules/MockDigitalLightModule.h" #define EDGE_MOCK_TITLE_TD "MockEdge" -#define MOCK_MODULE_TD_NAME "light-module" #define MOCK_LIGHT_TD_0_ID "0" #define MOCK_LIGHT_TD_1_ID "1" +#define MOCK_LIGHT_TD_2_ID "2" #define TD_IP_TEST "127.0.0.1" #define TD_PORT_TEST 80 MockDigitalLightHw* mockLight0; MockDigitalLightHw* mockLight1; +MockDigitalLightHw* mockLight2; Edge* tdEdge; @@ -59,12 +60,13 @@ void contains_modules() { TEST_ASSERT_TRUE(modulesArray.isArray()); Json::Value moduleObj = modulesArray[0]; TEST_ASSERT_TRUE(moduleObj.isMember("module")); - TEST_ASSERT_EQUAL_STRING(MOCK_MODULE_TD_NAME, moduleObj["module"].asCString()); + TEST_ASSERT_EQUAL_STRING(module_as_string(ModuleNames::Light).c_str(), moduleObj["module"].asCString()); TEST_ASSERT_TRUE(moduleObj.isMember("components")); Json::Value moduleComponents = moduleObj["components"]; TEST_ASSERT_TRUE(moduleComponents.isArray()); TEST_ASSERT_EQUAL_STRING(MOCK_LIGHT_TD_0_ID, moduleComponents[0].asCString()); TEST_ASSERT_EQUAL_STRING(MOCK_LIGHT_TD_1_ID, moduleComponents[1].asCString()); + TEST_ASSERT_EQUAL_STRING(MOCK_LIGHT_TD_2_ID, moduleComponents[2].asCString()); } void check_additional_actions(Json::Value action) { @@ -87,7 +89,7 @@ void check_actions_and_properties(Json::Value object, std::string names[], int s for(std::string c : tdComponents) { Json::Value name = object[names[i] + "-" + c]; TEST_ASSERT_TRUE(name.isMember("module")); - TEST_ASSERT_EQUAL_STRING(MOCK_MODULE_TD_NAME, name["module"].asCString()); + TEST_ASSERT_EQUAL_STRING(module_as_string(ModuleNames::Light).c_str(), name["module"].asCString()); TEST_ASSERT_TRUE(name.isMember("forms")); Json::Value forms = name["forms"]; TEST_ASSERT_TRUE(forms.isArray()); @@ -143,20 +145,23 @@ void contains_all_elements() { } void setup_thing_descriptor_test() { - mockLight0 = new MockDigitalLightHw(MOCK_LIGHT_TD_0_ID, 0); - mockLight1 = new MockDigitalLightHw(MOCK_LIGHT_TD_1_ID, 1); + mockLight0 = new MockDigitalLightHw(MOCK_LIGHT_TD_0_ID); + mockLight1 = new MockDigitalLightHw(MOCK_LIGHT_TD_1_ID); + mockLight2 = new MockDigitalLightHw(MOCK_LIGHT_TD_2_ID); tdEdge = new Edge(EDGE_MOCK_TITLE_TD, std::list({ new MockDigitalLightModule( - MOCK_MODULE_TD_NAME, std::list({mockLight0, mockLight1}) + ), + new MockDigitalLightModule( + std::list({mockLight2}) )} ) ); } void post_thing_descriptor_test() { - delete mockLight0, mockLight1, tdEdge; + delete mockLight0, mockLight1, mockLight2, tdEdge; } void test_ThingDescriptor() { diff --git a/edge/test/brittany/hw/MockDigitalLightHwTest.h b/edge/test/brittany/hw/MockDigitalLightHwTest.h index dfea3d8..8345086 100644 --- a/edge/test/brittany/hw/MockDigitalLightHwTest.h +++ b/edge/test/brittany/hw/MockDigitalLightHwTest.h @@ -2,18 +2,13 @@ #include "mock-digital-light/hw/MockDigitalLightHw.h" #define DIGITAL_LIGHT_ID "light" -#define DIGITAL_LIGHT_PIN 10 -MockDigitalLightHw mockDigitalLight = MockDigitalLightHw(DIGITAL_LIGHT_ID, DIGITAL_LIGHT_PIN); +MockDigitalLightHw mockDigitalLight = MockDigitalLightHw(DIGITAL_LIGHT_ID); void test_get_mock_digital_light_id() { TEST_ASSERT_EQUAL_STRING(mockDigitalLight.id().c_str(), DIGITAL_LIGHT_ID); } -void test_get_mock_digital_light_pin() { - TEST_ASSERT_EQUAL(mockDigitalLight.pin(), DIGITAL_LIGHT_PIN); -} - void test_mock_digital_light_is_on() { TEST_ASSERT_TRUE(mockDigitalLight.isOn()); } @@ -34,7 +29,6 @@ void test_mock_digital_light_off_operation() { void test_MockDigitalLightHw() { RUN_TEST(test_get_mock_digital_light_id); - RUN_TEST(test_get_mock_digital_light_pin); RUN_TEST(test_mock_digital_light_is_off); // test that light is initally off. RUN_TEST(test_mock_digital_light_on_operation); RUN_TEST(test_mock_digital_light_off_operation); diff --git a/edge/test/brittany/hw/MockHumiditySensorHwTest.h b/edge/test/brittany/hw/MockHumiditySensorHwTest.h new file mode 100644 index 0000000..0a89461 --- /dev/null +++ b/edge/test/brittany/hw/MockHumiditySensorHwTest.h @@ -0,0 +1,29 @@ +#include +#include +#include "mock-humidity-sensor/hw/MockHumiditySensorHw.h" + +#define MOCK_HUMIDITY_TEST_ATTEMPT 100 +#define MOCK_HUMIDITY_SENSOR_NAME "jotaro" + +MockHumiditySensorHw mockHumiditySensor = + MockHumiditySensorHw(MOCK_HUMIDITY_SENSOR_NAME); + +void mock_humidity_value_is_valid(int value, int min, int max) { + TEST_ASSERT_TRUE(value <= max && value >= min); +} + +void test_mock_humidity_value() { + std::optional hum = mockHumiditySensor.humidity(); + TEST_ASSERT_TRUE(hum.has_value()); + mock_temperature_value_is_valid(hum.value(), 0, MOCK_MAX_HUMIDITY); +} + +void test_mock_humidity_sensor() { + for(int i = 0; i < MOCK_TEMPERATURE_TEST_ATTEMPT; i++) { + test_mock_humidity_value(); + } +} + +void test_MockHumiditySensorHwTest() { + RUN_TEST(test_mock_humidity_sensor); +} diff --git a/edge/test/brittany/hw/MockSwitchableHwTest.h b/edge/test/brittany/hw/MockSwitchableHwTest.h new file mode 100644 index 0000000..53b31a1 --- /dev/null +++ b/edge/test/brittany/hw/MockSwitchableHwTest.h @@ -0,0 +1,30 @@ +#include +#include "mock-switchable/hw/MockSwitchableHw.h" + +#define MOCK_SWITCHABLE_HW_ID "switch" + +MockSwitchableHw mockSwitchableHw = MockSwitchableHw(MOCK_SWITCHABLE_HW_ID); + +void mock_switchable_hw_is_on_must_be(bool must_be) { + if(must_be) { + TEST_ASSERT_TRUE(mockSwitchableHw.isOn()); + } else { + TEST_ASSERT_FALSE(mockSwitchableHw.isOn()); + } +} + +void test_mock_switchable_hw_is_off() { + mock_switchable_hw_is_on_must_be(false); +} + +void test_mock_switchable_hw_is_on() { + mock_switchable_hw_is_on_must_be(true); +} + +void test_MockSwitchableHwTest() { + RUN_TEST(test_mock_switchable_hw_is_off); + mockSwitchableHw.on(); + RUN_TEST(test_mock_switchable_hw_is_on); + mockSwitchableHw.off(); + RUN_TEST(test_mock_switchable_hw_is_off); +} diff --git a/edge/test/brittany/hw/MockTempHumSensorHwTest.h b/edge/test/brittany/hw/MockTempHumSensorHwTest.h deleted file mode 100644 index 0252b8a..0000000 --- a/edge/test/brittany/hw/MockTempHumSensorHwTest.h +++ /dev/null @@ -1,64 +0,0 @@ -#include -#include -#include "mock-temp-hum-sensor/hw/MockTempHumSensorHw.h" - -#define MOCK_TEMP_HUM_SENSOR_NAME "temp-hum" -#define MOCK_TEMP_HUM_SENSOR_PIN 0 -#define MOCK_TEMP_HUM_SENSOR_CELSIUS 12.5 -#define MOCK_TEMP_HUM_SENSOR_HUMIDITY 50 - -MockTempHumSensorHw tempHum = - MockTempHumSensorHw( - MOCK_TEMP_HUM_SENSOR_NAME, - MOCK_TEMP_HUM_SENSOR_PIN, - MOCK_TEMP_HUM_SENSOR_CELSIUS, - MOCK_TEMP_HUM_SENSOR_HUMIDITY - ); - -MockTempHumSensorHw brokenTempHum = - MockTempHumSensorHw( - MOCK_TEMP_HUM_SENSOR_NAME, - MOCK_TEMP_HUM_SENSOR_PIN, - std::nullopt, - std::nullopt - ); - - -void test_mock_celsius() { - TEST_ASSERT_EQUAL_FLOAT( - MOCK_TEMP_HUM_SENSOR_CELSIUS, - tempHum.temperatureCelsius().value() - ); - TEST_ASSERT_FALSE(brokenTempHum.temperatureCelsius().has_value()); -} - -void test_mock_fahrenheit() { - TEST_ASSERT_EQUAL_FLOAT( - MOCK_TEMP_HUM_SENSOR_CELSIUS * 1.8 + 32, - tempHum.temperatureFahrenheit().value() - ); - TEST_ASSERT_FALSE(brokenTempHum.temperatureFahrenheit().has_value()); -} - -void test_mock_kelvin() { - TEST_ASSERT_EQUAL_FLOAT( - MOCK_TEMP_HUM_SENSOR_CELSIUS + 273.15, - tempHum.temperatureKelvin().value() - ); - TEST_ASSERT_FALSE(brokenTempHum.temperatureKelvin().has_value()); -} - -void test_mock_humidity() { - TEST_ASSERT_EQUAL_FLOAT( - MOCK_TEMP_HUM_SENSOR_HUMIDITY, - tempHum.humidity().value() - ); - TEST_ASSERT_FALSE(brokenTempHum.humidity().has_value()); -} - -void test_MockTempHumSensorHwTest() { - RUN_TEST(test_mock_celsius); - RUN_TEST(test_mock_fahrenheit); - RUN_TEST(test_mock_kelvin); - RUN_TEST(test_mock_humidity); -} diff --git a/edge/test/brittany/hw/MockTemperatureSensorHwTest.h b/edge/test/brittany/hw/MockTemperatureSensorHwTest.h new file mode 100644 index 0000000..258c456 --- /dev/null +++ b/edge/test/brittany/hw/MockTemperatureSensorHwTest.h @@ -0,0 +1,52 @@ +#include +#include +#include "mock-temperature-sensor/hw/MockTemperatureSensorHw.h" + +#define MOCK_TEMPERATURE_TEST_ATTEMPT 100 +#define MOCK_TEMPERATURE_SENSOR_NAME "jotaro" + +MockTemperatureSensorHw mockTemperatureSensor = + MockTemperatureSensorHw(MOCK_TEMPERATURE_SENSOR_NAME); + + +void mock_temperature_value_is_valid(int value, int min, int max) { + TEST_ASSERT_TRUE(value <= max && value >= min); +} + +void test_mock_temperature_celsius() { + std::optional tempC = mockTemperatureSensor.temperatureCelsius(); + TEST_ASSERT_TRUE(tempC.has_value()); + mock_temperature_value_is_valid(tempC.value(), 0, MOCK_MAX_TEMPERATURE_CELSIUS); +} + +void test_mock_temperature_fahrenheit() { + std::optional tempF = mockTemperatureSensor.temperatureFahrenheit(); + TEST_ASSERT_TRUE(tempF.has_value()); + mock_temperature_value_is_valid( + tempF.value(), + Temperature::fromCToF(0), + Temperature::fromCToF(MOCK_MAX_TEMPERATURE_CELSIUS) + ); +} + +void test_mock_temperature_kelvin() { + std::optional tempK = mockTemperatureSensor.temperatureKelvin(); + TEST_ASSERT_TRUE(tempK.has_value()); + mock_temperature_value_is_valid( + tempK.value(), + Temperature::fromCToK(0), + Temperature::fromCToK(MOCK_MAX_TEMPERATURE_CELSIUS) + ); +} + +void test_mock_temperature() { + for(int i = 0; i < MOCK_TEMPERATURE_TEST_ATTEMPT; i++) { + test_mock_temperature_celsius(); + test_mock_temperature_fahrenheit(); + test_mock_temperature_kelvin(); + } +} + +void test_MockTemperatureSensorHwTest() { + RUN_TEST(test_mock_temperature); +} diff --git a/edge/test/brittany/main.cpp b/edge/test/brittany/main.cpp index 84ea07b..0d51a6e 100644 --- a/edge/test/brittany/main.cpp +++ b/edge/test/brittany/main.cpp @@ -2,14 +2,19 @@ #include "operation-handler/OperationHandlerResultTest.h" #include "operation-handler/OperationHandlerTest.h" #include "operation-handler/MockDigitalLightHandlersTest.h" +#include "operation-handler/MockHumidityHandlerTest.h" +#include "operation-handler/MockTemperatureHandlerTest.h" #include "operation-handler/util/ValueReturnedResultTest.h" #include "operation-handler/util/ValueReturnedResultFactoryTest.h" #include "operation-handler/types/TypeTest.h" #include "hw/ComponentHwTest.h" #include "hw/OnePinTest.h" +#include "hw/MockSwitchableHwTest.h" #include "hw/MockDigitalLightHwTest.h" -#include "hw/MockTempHumSensorHwTest.h" +#include "hw/MockTemperatureSensorHwTest.h" +#include "hw/MockHumiditySensorHwTest.h" #include "modules/MockDigitalLightModuleTest.h" +#include "modules/ModuleNamesTest.h" #include "edge/EdgeTest.h" #include "edge/ThingDescriptorTest.h" #include "utilTest.h" @@ -18,13 +23,17 @@ void test_hw() { test_ComponentHw(); //ComponentHwTest test_OnePin(); //OnePinTest test_MockDigitalLightHw(); //MockDigitalLightHwTest - test_MockTempHumSensorHwTest(); //MockTempHumSensorHwTest + test_MockSwitchableHwTest(); //MockSwitchableHwTest + test_MockTemperatureSensorHwTest(); //MockTemperatureSensoreHwTest + test_MockHumiditySensorHwTest(); //MockTemperatureSensoreHwTest } void test_operation_handler() { test_OperationHandlerResult(); //OperationHandlerResultTest test_OperationHandler(); //OperationHandlerTest test_MockDigitalLightHandlersTest(); //MockDigitalLightHandlersTest + test_MockHumidityHandler(); //MockHumidityHandlerTest + test_MockTemperatureHandler(); //MockTemperatureHandlerTest test_ValueReturnedResultTest(); //ValueReturnedResultTest test_ValueReturnedResultFactoryTest(); //ValueReturnedResultFactoryTest test_Type(); //TypeTest @@ -32,6 +41,7 @@ void test_operation_handler() { void test_modules() { test_ComponentModuleTest(); //ModuleTest + test_ModuleNamesTest(); //ModuleNamesTest } void test_edge() { diff --git a/edge/test/brittany/modules/MockDigitalLightModuleTest.h b/edge/test/brittany/modules/MockDigitalLightModuleTest.h index 674ae85..c5bf6ff 100644 --- a/edge/test/brittany/modules/MockDigitalLightModuleTest.h +++ b/edge/test/brittany/modules/MockDigitalLightModuleTest.h @@ -5,15 +5,10 @@ #include "mock/operation-handler/MockOperationHandler.h" #include "mock-digital-light/hw/MockDigitalLightHw.h" #include "mock-digital-light/modules/MockDigitalLightModule.h" -#include "modules/Module.h" -#define MOCK_MODULE_NAME "light" #define MOCK_LIGHT_IN_MODULE_NAME_0 "light0" #define MOCK_LIGHT_IN_MODULE_NAME_1 "light1" -#define MOCK_LIGHT_IN_MODULE_PIN_0 1 -#define MOCK_LIGHT_IN_MODULE_PIN_1 2 - MockDigitalLightHw* lightInModule0; MockDigitalLightHw* lightInModule1; MockDigitalLightModule* moduleTest; @@ -22,11 +17,11 @@ std::list components; std::list lightNames = {MOCK_LIGHT_IN_MODULE_NAME_0, MOCK_LIGHT_IN_MODULE_NAME_1}; void setup_module_test() { - lightInModule0 = new MockDigitalLightHw(MOCK_LIGHT_IN_MODULE_NAME_0, MOCK_LIGHT_IN_MODULE_PIN_0); - lightInModule1 = new MockDigitalLightHw(MOCK_LIGHT_IN_MODULE_NAME_1, MOCK_LIGHT_IN_MODULE_PIN_1); + lightInModule0 = new MockDigitalLightHw(MOCK_LIGHT_IN_MODULE_NAME_0); + lightInModule1 = new MockDigitalLightHw(MOCK_LIGHT_IN_MODULE_NAME_1); components.push_back(lightInModule0); components.push_back(lightInModule1); - moduleTest = new MockDigitalLightModule(MOCK_MODULE_NAME, components); + moduleTest = new MockDigitalLightModule(components); } void post_module_test() { @@ -57,7 +52,7 @@ void test_turn_on_turn_off_handler_in_module(OperationHandler* handler, std::str } void test_is_on_is_off_handler_in_module(OperationHandler* handler, std::string component_name, bool isOn) { - check_handler_path(handler, as_route(MOCK_IS_ON_HANDLER_MODULE_NAME)); + check_handler_path(handler, "/isOn"); Json::Value args; args["id"] = component_name; OperationHandlerResult result = handler->handle(args); @@ -70,11 +65,11 @@ void test_is_on_is_off_handler_in_module(OperationHandler* handler, std::string } void test_turn_on_handler_in_module(OperationHandler* handler, std::string component_name) { - test_turn_on_turn_off_handler_in_module(handler, as_route(MOCK_TURN_ON_HANDLER_MODULE_NAME), component_name); + test_turn_on_turn_off_handler_in_module(handler, "/turnOn", component_name); } void test_turn_off_handler_in_module(OperationHandler* handler, std::string component_name) { - test_turn_on_turn_off_handler_in_module(handler, as_route(MOCK_TURN_OFF_HANDLER_MODULE_NAME), component_name); + test_turn_on_turn_off_handler_in_module(handler, "/turnOff", component_name); } void test_is_off_handler_in_module(OperationHandler* handler, std::string component_name) { @@ -87,7 +82,10 @@ void test_is_on_handler_in_module(OperationHandler* handler, std::string compone // TEST void test_module_name() { - TEST_ASSERT_EQUAL_STRING(MOCK_MODULE_NAME, moduleTest -> name().c_str()); + TEST_ASSERT_EQUAL_STRING( + module_as_string(ModuleNames::Light).c_str(), + moduleTest -> name().c_str() + ); } // TEST diff --git a/edge/test/brittany/modules/MockHumidityModuleTest.h b/edge/test/brittany/modules/MockHumidityModuleTest.h new file mode 100644 index 0000000..138e4f2 --- /dev/null +++ b/edge/test/brittany/modules/MockHumidityModuleTest.h @@ -0,0 +1,46 @@ +#ifndef MOCK_HUMIDITY_MODULE_TEST_H +#define MOCK_HUMIDITY_MODULE_TEST_H + +#include +#include +#include "mock-humidity-sensor/modules/MockHumidityModule.h" +#include "../operation-handler/MockHumidityHandlerTest.h" + +MockHumidityModule* mockHumidityModule; + +void setup_humidity_module_test(std::list components) { + mockHumidityModule = new MockHumidityModule(components); +} + +void post_humidity_module_test() { + delete mockHumidityModule; +} + +void test_humidity_module_name() { + TEST_ASSERT_EQUAL_STRING( + module_as_string(ModuleNames::Humidity).c_str(), + mockHumidityModule -> name().c_str() + ); +} + +void test_humidity_module_components() { + std::list comps = mockHumidityModule -> components(); + TEST_ASSERT_EQUAL(1, comps.size()); + TEST_ASSERT_EQUAL_STRING(MOCK_HUMIDITY_SENSOR_NAME_IN_HANDLER, comps.front().id().c_str()); +} + +void test_humidity_module_get_handlers() { + std::list handlers = mockHumidityModule -> handlers(); + TEST_ASSERT_EQUAL(1, handlers.size()); + test_mock_humidity_handler_handle_using_handler(handlers.front()); +} + +void test_MockHumidityModule(std::list components) { + setup_humidity_module_test(components); + RUN_TEST(test_humidity_module_name); + RUN_TEST(test_humidity_module_components); + RUN_TEST(test_humidity_module_get_handlers); + post_humidity_module_test(); +} + +#endif // MOCK_HUMIDITY_MODULE_TEST_H \ No newline at end of file diff --git a/edge/test/brittany/modules/MockTemperatureModelTest.h b/edge/test/brittany/modules/MockTemperatureModelTest.h new file mode 100644 index 0000000..fc1bb75 --- /dev/null +++ b/edge/test/brittany/modules/MockTemperatureModelTest.h @@ -0,0 +1,46 @@ +#ifndef MOCK_TEMPERATURE_MODULE_TEST_H +#define MOCK_TEMPERATURE_MODULE_TEST_H + +#include +#include +#include "mock-temperature-sensor/modules/MockTemperatureModule.h" +#include "../operation-handler/MockTemperatureHandlerTest.h" + +MockTemperatureModule* mockTemperatureModule; + +void setup_temperature_module_test(std::list components) { + mockTemperatureModule = new MockTemperatureModule(components); +} + +void post_temperature_module_test() { + delete mockTemperatureModule; +} + +void test_temperature_module_name() { + TEST_ASSERT_EQUAL_STRING( + module_as_string(ModuleNames::Temperature).c_str(), + mockTemperatureModule -> name().c_str() + ); +} + +void test_temperature_module_components() { + std::list comps = mockTemperatureModule -> components(); + TEST_ASSERT_EQUAL(1, comps.size()); + TEST_ASSERT_EQUAL_STRING(MOCK_TEMPERATURE_SENSOR_NAME_IN_HANDLER, comps.front().id().c_str()); +} + +void test_temperature_module_get_handlers() { + std::list handlers = mockTemperatureModule -> handlers(); + TEST_ASSERT_EQUAL(1, handlers.size()); + test_mock_temperature_handler_handle_using_handler(handlers.front()); +} + +void test_MockTemperatureModule(std::list components) { + setup_temperature_module_test(components); + RUN_TEST(test_temperature_module_name); + RUN_TEST(test_temperature_module_components); + RUN_TEST(test_temperature_module_get_handlers); + post_temperature_module_test(); +} + +#endif // MOCK_TEMPERATURE_MODULE_TEST_H \ No newline at end of file diff --git a/edge/test/brittany/modules/ModuleNamesTest.h b/edge/test/brittany/modules/ModuleNamesTest.h new file mode 100644 index 0000000..f45cde3 --- /dev/null +++ b/edge/test/brittany/modules/ModuleNamesTest.h @@ -0,0 +1,13 @@ +#include +#include "modules/ModuleNames.h" + +void test_module_as_string() { + TEST_ASSERT_EQUAL_STRING("light", module_as_string(ModuleNames::Light).c_str()); + TEST_ASSERT_EQUAL_STRING("temperature", module_as_string(ModuleNames::Temperature).c_str()); + TEST_ASSERT_EQUAL_STRING("airHumidity", module_as_string(ModuleNames::Humidity).c_str()); + TEST_ASSERT_EQUAL_STRING("", module_as_string(ModuleNames(104131)).c_str()); +} + +void test_ModuleNamesTest() { + RUN_TEST(test_module_as_string); +} \ No newline at end of file diff --git a/edge/test/brittany/operation-handler/MockDigitalLightHandlersTest.h b/edge/test/brittany/operation-handler/MockDigitalLightHandlersTest.h index 00bc3a8..371415a 100644 --- a/edge/test/brittany/operation-handler/MockDigitalLightHandlersTest.h +++ b/edge/test/brittany/operation-handler/MockDigitalLightHandlersTest.h @@ -6,13 +6,7 @@ #include "util.h" #define LIGHT_0_NAME "light0" -#define LIGHT_0_PIN 10 #define LIGHT_1_NAME "light1" -#define LIGHT_1_PIN 11 - -#define MOCK_TURN_OFF_LIGHT_NAME "turnOff" -#define MOCK_TURN_ON_LIGHT_NAME "turnOn" -#define MOCK_IS_ON_LIGHT_NAME "isOn" MockDigitalLightHw* light0; MockDigitalLightHw* light1; @@ -27,25 +21,13 @@ Json::Value argsNotFound; Json::Value argsBadRequest; void setup_mock_light_handler_test() { - light0 = new MockDigitalLightHw(LIGHT_0_NAME, LIGHT_0_PIN); - light1 = new MockDigitalLightHw(LIGHT_1_NAME, LIGHT_1_PIN); + light0 = new MockDigitalLightHw(LIGHT_0_NAME); + light1 = new MockDigitalLightHw(LIGHT_1_NAME); light_list.push_front(light0); light_list.push_front(light1); - turnOffDigitalLightHandler = new MockTurnOffDigitalLightHandler( - MOCK_TURN_OFF_LIGHT_NAME, - as_route(MOCK_TURN_OFF_LIGHT_NAME), - light_list - ); - turnOnDigitalLightHandler = new MockTurnOnDigitalLightHandler( - MOCK_TURN_ON_LIGHT_NAME, - as_route(MOCK_TURN_ON_LIGHT_NAME), - light_list - ); - isOnDigitalLightHandler = new MockIsOnDigitalLightHandler( - MOCK_IS_ON_LIGHT_NAME, - as_route(MOCK_IS_ON_LIGHT_NAME), - light_list - ); + turnOffDigitalLightHandler = new MockTurnOffDigitalLightHandler(light_list); + turnOnDigitalLightHandler = new MockTurnOnDigitalLightHandler(light_list); + isOnDigitalLightHandler = new MockIsOnDigitalLightHandler(light_list); args0["id"] = LIGHT_0_NAME; args1["id"] = LIGHT_1_NAME; argsNotFound["id"] = "the game"; @@ -101,13 +83,13 @@ void test_handle_with_different_args(T h) { //TEST void test_mock_turn_off_digital_light_handler() { - test_handler_name_and_path(turnOffDigitalLightHandler, MOCK_TURN_OFF_LIGHT_NAME); + test_handler_name_and_path(turnOffDigitalLightHandler, "turnOff"); test_handle_with_different_args(turnOffDigitalLightHandler); } //TEST void test_mock_turn_on_digital_light_handler(){ - test_handler_name_and_path(turnOnDigitalLightHandler, MOCK_TURN_ON_LIGHT_NAME); + test_handler_name_and_path(turnOnDigitalLightHandler, "turnOn"); test_handle_with_different_args(turnOnDigitalLightHandler); } @@ -117,7 +99,7 @@ void light_state_check(OperationHandlerResult result, bool isOn) { } void is_on_handler_state_must_be(bool isOn) { - test_handler_name_and_path(isOnDigitalLightHandler, MOCK_IS_ON_LIGHT_NAME); + test_handler_name_and_path(isOnDigitalLightHandler, "isOn"); light_state_check(isOnDigitalLightHandler->handle(args0), isOn); light_state_check(isOnDigitalLightHandler->handle(args1), isOn); check_result_is_not_found(isOnDigitalLightHandler->handle(argsNotFound)); diff --git a/edge/test/brittany/operation-handler/MockHumidityHandlerTest.h b/edge/test/brittany/operation-handler/MockHumidityHandlerTest.h new file mode 100644 index 0000000..52ce97a --- /dev/null +++ b/edge/test/brittany/operation-handler/MockHumidityHandlerTest.h @@ -0,0 +1,75 @@ +#ifndef MOCK_HUMIDITY_HANDLER_TEST_H +#define MOCK_HUMIDITY_HANDLER_TEST_H + +#include +#include +#include "mock-humidity-sensor/operation-handler/MockHumidityHandler.h" +#include "mock-humidity-sensor/hw/MockHumiditySensorHw.h" + + +#define MOCK_HUMIDITY_SENSOR_NAME_IN_HANDLER "sensor" + +MockHumidityHandler* mockHumidityHandler; +MockHumiditySensorHw* mockHumSensor; + +Json::Value argsMockHumSensor; +Json::Value argsMockHumSensorBadReq; +Json::Value argsMockHumSensorNotFound; + +void setup_mock_humidity_handler() { + mockHumSensor = new MockHumiditySensorHw(MOCK_HUMIDITY_SENSOR_NAME_IN_HANDLER); + argsMockHumSensor["id"] = MOCK_HUMIDITY_SENSOR_NAME_IN_HANDLER; + argsMockHumSensorBadReq["za"] = "warudo"; + argsMockHumSensorNotFound["id"] = "giogia"; + mockHumidityHandler = new MockHumidityHandler({mockHumSensor}); +} + +void post_mock_humidity_handler() { + delete mockHumSensor; +} + +void test_mock_humidity_handler_success(OperationHandler* h) { + OperationHandlerResult r = h -> handle(argsMockHumSensor); + TEST_ASSERT_EQUAL(HttpStatus::OK, r.code()); + float c = r.content().asFloat(); + TEST_ASSERT_TRUE(c >= 0 && c <= MOCK_MAX_HUMIDITY); +} + +void test_mock_humidity_handler_not_found(OperationHandler* h) { + OperationHandlerResult r = h -> handle(argsMockHumSensorNotFound); + TEST_ASSERT_EQUAL(HttpStatus::NotFound, r.code()); + TEST_ASSERT_EQUAL_STRING( + phrase(ContentResult::ResourceNotFound).c_str(), + r.content().asCString() + ); +} + +void test_mock_humidity_handler_bad_req(OperationHandler* h) { + OperationHandlerResult r = h -> handle(argsMockHumSensorBadReq); + TEST_ASSERT_EQUAL(HttpStatus::BadRequest, r.code()); + TEST_ASSERT_EQUAL_STRING( + phrase(ContentResult::BadRequest).c_str(), + r.content().asCString() + ); +} + +void test_mock_humidity_handler_handle_using_handler(OperationHandler* h) { + test_mock_humidity_handler_success(h); + test_mock_humidity_handler_not_found(h); + test_mock_humidity_handler_bad_req(h); +} + +#include "../modules/MockHumidityModuleTest.h" + +void test_mock_humidity_handler_handle() { + test_mock_humidity_handler_handle_using_handler(mockHumidityHandler); +} + +void test_MockHumidityHandler() { + setup_mock_humidity_handler(); + RUN_TEST(test_mock_humidity_handler_handle); + test_MockHumidityModule({mockHumSensor}); + post_mock_humidity_handler(); +} + +#endif // MOCK_HUMIDITY_HANDLER_TEST_H \ No newline at end of file diff --git a/edge/test/brittany/operation-handler/MockTemperatureHandlerTest.h b/edge/test/brittany/operation-handler/MockTemperatureHandlerTest.h new file mode 100644 index 0000000..5ad46d9 --- /dev/null +++ b/edge/test/brittany/operation-handler/MockTemperatureHandlerTest.h @@ -0,0 +1,99 @@ +#ifndef MOCK_TEMPERATURE_HANDLER_TEST_H +#define MOCK_TEMPERATURE_HANDLER_TEST_H + +#include +#include +#include "mock-temperature-sensor/operation-handler/MockTemperatureHandler.h" +#include "mock-temperature-sensor/hw/MockTemperatureSensorHw.h" + +#define MOCK_TEMPERATURE_SENSOR_NAME_IN_HANDLER "sensor" + +MockTemperatureHandler* mockTemperatureHandler; +MockTemperatureSensorHw* mockTempSensor; + +Json::Value argsMockTempSensor; +Json::Value argsMockTempSensorBadReq; +Json::Value argsMockTempSensorNotFound; + +void setup_mock_temperature_handler() { + mockTempSensor = new MockTemperatureSensorHw(MOCK_TEMPERATURE_SENSOR_NAME_IN_HANDLER); + argsMockTempSensor["id"] = MOCK_TEMPERATURE_SENSOR_NAME_IN_HANDLER; + argsMockTempSensorBadReq["za"] = "warudo"; + argsMockTempSensorNotFound["id"] = "giogia"; + mockTemperatureHandler = new MockTemperatureHandler({mockTempSensor}); +} + +void post_mock_temperature_handler() { + delete mockTempSensor; +} + +void check_temp_value_is_correct(OperationHandlerResult r, float min, float max) { + TEST_ASSERT_EQUAL(HttpStatus::OK, r.code()); + float c = r.content().asFloat(); + TEST_ASSERT_TRUE(c >= min && c <= max); +} + +void test_mock_temperature_handler_success(OperationHandler* h) { + check_temp_value_is_correct( + h -> handle(argsMockTempSensor), + 0, + MOCK_MAX_TEMPERATURE_CELSIUS + ); + argsMockTempSensor["unit"] = "f"; + check_temp_value_is_correct( + h -> handle(argsMockTempSensor), + Temperature::fromCToF(0), + Temperature::fromCToF(MOCK_MAX_TEMPERATURE_CELSIUS) + ); + argsMockTempSensor["unit"] = "k"; + check_temp_value_is_correct( + h -> handle(argsMockTempSensor), + Temperature::fromCToK(0), + Temperature::fromCToK(MOCK_MAX_TEMPERATURE_CELSIUS) + ); + argsMockTempSensor["unit"] = "c"; + check_temp_value_is_correct( + h -> handle(argsMockTempSensor), + 0, + MOCK_MAX_TEMPERATURE_CELSIUS + ); +} + +void test_mock_temperature_handler_not_found(OperationHandler* h) { + OperationHandlerResult r = h -> handle(argsMockTempSensorNotFound); + TEST_ASSERT_EQUAL(HttpStatus::NotFound, r.code()); + TEST_ASSERT_EQUAL_STRING( + phrase(ContentResult::ResourceNotFound).c_str(), + r.content().asCString() + ); +} + +void test_mock_temperature_handler_bad_req(OperationHandler* h) { + OperationHandlerResult r = h -> handle(argsMockTempSensorBadReq); + TEST_ASSERT_EQUAL(HttpStatus::BadRequest, r.code()); + TEST_ASSERT_EQUAL_STRING( + phrase(ContentResult::BadRequest).c_str(), + r.content().asCString() + ); +} + +void test_mock_temperature_handler_handle_using_handler(OperationHandler* h) { + test_mock_temperature_handler_success(h); + test_mock_temperature_handler_not_found(h); + test_mock_temperature_handler_bad_req(h); +} + +#include "../modules/MockTemperatureModelTest.h" + +void test_mock_temperature_handler_handle() { + test_mock_temperature_handler_handle_using_handler(mockTemperatureHandler); +} + +void test_MockTemperatureHandler() { + setup_mock_temperature_handler(); + RUN_TEST(test_mock_temperature_handler_handle); + test_MockTemperatureModule({mockTempSensor}); + post_mock_temperature_handler(); +} + +#endif // MOCK_TEMPERATURE_HANDLER_TEST_H \ No newline at end of file diff --git a/edge/test/brittany/operation-handler/OperationHandlerTest.h b/edge/test/brittany/operation-handler/OperationHandlerTest.h index 5c2944d..e93f4db 100644 --- a/edge/test/brittany/operation-handler/OperationHandlerTest.h +++ b/edge/test/brittany/operation-handler/OperationHandlerTest.h @@ -1,20 +1,15 @@ #include #include "mock/operation-handler/MockOperationHandler.h" +#include "util.h" -#define OPERATION_HANDLER_NAME "Operation" -#define OPERATION_HANDLER_PATH "/operation" - -MockOperationHandler mockOperationHandler = MockOperationHandler( - OPERATION_HANDLER_NAME, - OPERATION_HANDLER_PATH -); +MockOperationHandler mockOperationHandler = MockOperationHandler(); void test_operation_handler_name() { - TEST_ASSERT_EQUAL_STRING(OPERATION_HANDLER_NAME, mockOperationHandler.name().c_str()); + TEST_ASSERT_EQUAL_STRING("value", mockOperationHandler.name().c_str()); } void test_operation_handler_path() { - TEST_ASSERT_EQUAL_STRING(OPERATION_HANDLER_PATH, mockOperationHandler.path().c_str()); + TEST_ASSERT_EQUAL_STRING("/value", mockOperationHandler.path().c_str()); } void test_operation_handler_operation_type() { diff --git a/edge/test/brittany/utilTest.h b/edge/test/brittany/utilTest.h index 69123bf..b9dedf6 100644 --- a/edge/test/brittany/utilTest.h +++ b/edge/test/brittany/utilTest.h @@ -4,10 +4,10 @@ #include "mock-digital-light/hw/MockDigitalLightHw.h" #include "util.h" +#define UTIL_TEST_RANDOM_ATTEMPT 100 #define COMPONENT_0_NAME "name0" #define COMPONENT_1_NAME "name1" #define COMPONENT_2_NAME "name2" -#define COMPONENT_2_PIN 10 ComponentHw* component0; ComponentHw* component1; @@ -18,7 +18,7 @@ std::list list_of_light; void setup_util_test() { component0 = new ComponentHw(COMPONENT_0_NAME); component1 = new ComponentHw(COMPONENT_1_NAME); - component2 = new MockDigitalLightHw(COMPONENT_2_NAME, COMPONENT_2_PIN); + component2 = new MockDigitalLightHw(COMPONENT_2_NAME); list.push_front(component0); list.push_front(component1); list.push_front(component2); @@ -37,8 +37,20 @@ void test_util_find_by_id() { TEST_ASSERT_FALSE(find_by_id(list, "the game").has_value()); } +void test_util_random() { + TEST_ASSERT_EQUAL(1, random(1,1)); + TEST_ASSERT_EQUAL(2, random(2,2)); + int min = 0; + int max = 20; + for(int i = 0; i <= UTIL_TEST_RANDOM_ATTEMPT; i++) { + int r = random(min, max); + TEST_ASSERT_TRUE(r >= min && r<= max); + } +} + void test_util() { setup_util_test(); RUN_TEST(test_util_find_by_id); + RUN_TEST(test_util_random); post_util_test(); }