diff --git a/CMakeLists.txt b/CMakeLists.txt index f2e32f1..3b5ed30 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -91,6 +91,36 @@ install( TARGETS itcPressureOptimizerScfe RUNTIME DESTINATION bin ) - endif() +################################################################################ +## Test +################################################################################ + +if(${BUILD_TEST}) + +add_executable(test + test/main.cpp +) + +set_property( + TARGET + test + PROPERTY + CXX_STANDARD 17 +) + +target_include_directories( + test + PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR} +) + +target_link_libraries( + test + PRIVATE + midas::mfe + ${LIBS} +) + +endif() \ No newline at end of file diff --git a/src/device/handlers/InputHandler.cpp b/src/device/handlers/InputHandler.cpp index 5fcb120..c6c7254 100644 --- a/src/device/handlers/InputHandler.cpp +++ b/src/device/handlers/InputHandler.cpp @@ -79,7 +79,6 @@ void InputHandler::pullTemperatureValue() { void InputHandler::pullPowerValue() { midas::odb o(this->path); float powerValue = o[powerIndex]; - printf("Sending power value %f\n", powerValue); Event event = {EventType::POWER_VALUE, powerValue}; eventBus.publish(event); } diff --git a/src/device/handlers/PressureCalculator.cpp b/src/device/handlers/PressureCalculator.cpp index 567be79..d7b9334 100644 --- a/src/device/handlers/PressureCalculator.cpp +++ b/src/device/handlers/PressureCalculator.cpp @@ -5,11 +5,12 @@ #include "SettingsHandler.h" #include "tmfe.h" #include +#include #include void PressureCalculator::update() { float averagePower = average.getAverage(); - float c2contrib = -cachedConstant1 * averagePower * cachedConstant2; + float c2contrib = cachedConstant1 * averagePower * cachedConstant2; float c1contrib = (cachedConstant1 * (cachedTemperature - cachedSP)) - c2contrib; float uncappedPressure = cachedMinimalPressure + c1contrib; @@ -30,6 +31,10 @@ void PressureCalculator::update() { PressureCalculator::PressureCalculator(EventBus &eventBusReference) : average(30), eventBus(eventBusReference) { + + cachedMaximalPressure = std::numeric_limits::infinity(); + cachedMinimalPressure = -cachedMaximalPressure; + eventBus.subscribe( [this](const DemandHandler::Event &e) { switch (e.type) { @@ -73,7 +78,7 @@ PressureCalculator::PressureCalculator(EventBus &eventBusReference) }); eventBus.subscribe( - [this](const itcPressureOptimizer::EquipmentPollEvent &e) { + [this](const SettingsHandler::Event &e) { switch (e.type) { case SettingsHandler::EventType::AVERAGE_TIME_WINDOW: updateAverageTimeWindow(std::get(e.value)); @@ -113,10 +118,12 @@ void PressureCalculator::updateMinimalPressure(float value) { void PressureCalculator::updateMaximalPressure(float value) { cachedMaximalPressure = value; - TMFE::Instance()->Msg( - MERROR, __FUNCTION__, - "Maximum value %f is lower than the minimum value %f !", - cachedMaximalPressure, cachedMinimalPressure); + if (cachedMinimalPressure > cachedMaximalPressure) { + TMFE::Instance()->Msg( + MERROR, __FUNCTION__, + "Maximum value %f is lower than the minimum value %f !", + cachedMaximalPressure, cachedMinimalPressure); + } update(); } diff --git a/main.cpp b/test/main.cpp similarity index 55% rename from main.cpp rename to test/main.cpp index 0779082..960f11b 100644 --- a/main.cpp +++ b/test/main.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -5,35 +6,32 @@ #include #include -#include "src/device/EventBus.h" +#include "../src/device/EventBus.h" class ProdA {}; class ProdB {}; class ConsA { public: - void surProdA(const ProdA &) { - std::cout << "ConsA : Reçu ProdA" << std::endl; - } + int counter = 0; + void surProdA(const ProdA &) { counter++; } }; class ConsB { public: - void surProdA(const ProdA &) { - std::cout << "ConsB : Reçu ProdA" << std::endl; - } - void surProdB(const ProdB &) { - std::cout << "ConsB : Reçu ProdB" << std::endl; - } + int counter = 0; + void surProdA(const ProdA &) { counter++; } + void surProdB(const ProdB &) { counter++; } }; class ConsC { public: - void surProdB(const ProdB &) { - std::cout << "ConsC : Reçu ProdB" << std::endl; - } + int counter = 0; + void surProdB(const ProdB &) { counter++; } }; int main() { + std::cout << "--- Starting EventBus Test ---" << std::endl; + EventBus bus; // Consumer @@ -54,13 +52,27 @@ int main() { bus.subscribe([&consC](const ProdB &e) { consC.surProdB(e); }); // Test of publishement + + assert(consA.counter == 0); + assert(consB.counter == 0); + assert(consC.counter == 0); std::cout << "--- Publishing of ProdA ---" << std::endl; ProdA prodA; + bus.publish(prodA); - std::cout << "\n--- Publishing of ProdB ---" << std::endl; + assert(consA.counter == 1); + assert(consB.counter == 1); + assert(consC.counter == 0); + + std::cout << "--- Publishing of ProdB ---" << std::endl; ProdB prodB; bus.publish(prodB); + assert(consA.counter == 1); + assert(consB.counter == 2); + assert(consC.counter == 1); + + std::cout << "--- SUCCESS ---" << std::endl; return 0; } \ No newline at end of file