diff --git a/src/device/Average.cpp b/src/device/Average.cpp index 9617bca..434d04e 100644 --- a/src/device/Average.cpp +++ b/src/device/Average.cpp @@ -1,7 +1,8 @@ -#include "Average.h" #include #include +#include "Average.h" + void Average::setWindowSize(double seconds) { windowSize = Duration(seconds); cleanupAndUpdate(Clock::now()); @@ -9,10 +10,12 @@ void Average::setWindowSize(double seconds) { void Average::addValue(double value) { TimePoint now = Clock::now(); - cleanupAndUpdate(now); - currentValue = value; + + // add a new value history.push_back({now, currentValue}); + + cleanupAndUpdate(now); } double Average::getAverage() { @@ -23,33 +26,32 @@ double Average::getAverage() { return currentValue; } - // If only one element, it's all the window - if (history.size() == 1) { - return history.front().value; - } - double totalWeightedValue = 0.0; double totalDurationSeconds = 0.0; - // Go throught the full historic - for (size_t i = 0; i < history.size() - 1; ++i) { - // How much time the value was present - Duration duration = history[i + 1].timestamp - history[i].timestamp; + TimePoint windowStart = + now - std::chrono::duration_cast(windowSize); - totalWeightedValue += history[i].value * duration.count(); - totalDurationSeconds += duration.count(); + for (size_t i = 0; i < history.size(); ++i) { + TimePoint segmentStart = (i == 0) ? windowStart : history[i].timestamp; + TimePoint segmentEnd = + (i < history.size() - 1) ? history[i + 1].timestamp : now; + + // remove time before the window + if (segmentEnd < windowStart) + continue; + if (segmentStart < windowStart) + segmentStart = windowStart; + + Duration duration = segmentEnd - segmentStart; + double seconds = duration.count(); + + if (seconds > 0.0) { + totalWeightedValue += history[i].value * seconds; + totalDurationSeconds += seconds; + } } - // add last segment : duration = now - timestamp - Duration lastDuration = now - history.back().timestamp; - totalWeightedValue += history.back().value * lastDuration.count(); - totalDurationSeconds += lastDuration.count(); - - /* - Safety to prevent dividing by 0 - This can occure if only one point is set and we compute the average too - fast - */ if (totalDurationSeconds <= 0.0) { return history.back().value; } @@ -58,14 +60,11 @@ double Average::getAverage() { } void Average::cleanupAndUpdate(TimePoint now) { - // Clean all element completly ouside the temporal window - while (history.size() > 1 && (now - history[1].timestamp) >= windowSize) { + TimePoint windowStart = + now - std::chrono::duration_cast(windowSize); + + // Remove all elements completely outside of the window + while (history.size() > 1 && history[1].timestamp <= windowStart) { history.pop_front(); } - - // If last element is partialy outside, get his timestamp back - if (!history.empty() && (now - history.front().timestamp) > windowSize) { - history.front().timestamp = - now - std::chrono::duration_cast(windowSize); - } } \ No newline at end of file diff --git a/src/device/DemandHandler.cpp b/src/device/DemandHandler.cpp index 2c4be25..572e99d 100644 --- a/src/device/DemandHandler.cpp +++ b/src/device/DemandHandler.cpp @@ -40,7 +40,7 @@ bool DemandHandler::init() { indexValid = false; } - if (indexValid) + if (!indexValid) return false; this->pullMinimalPressure(); diff --git a/src/device/InputHandler.cpp b/src/device/InputHandler.cpp index 21d91d9..a16b014 100644 --- a/src/device/InputHandler.cpp +++ b/src/device/InputHandler.cpp @@ -11,13 +11,20 @@ void InputHandler::updatePath(std::string name) { InputHandlerConfig::PATH_SUFFIX; } -void InputHandler::updateSPIndex(int index) { this->SPIndex = index; } +void InputHandler::updateSPIndex(int index) { + this->SPIndex = index; + pullSPValue(); +} void InputHandler::updateTemperatureIndex(int index) { this->temperatureIndex = index; + pullTemperatureValue(); } -void InputHandler::updatePowerIndex(int index) { this->powerIndex = index; } +void InputHandler::updatePowerIndex(int index) { + this->powerIndex = index; + pullPowerValue(); +} void InputHandler::pullSPValue() { midas::odb o(this->path); @@ -34,6 +41,7 @@ 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); } @@ -51,7 +59,6 @@ void InputHandler::updateHotlink() { hotlink->watch([&](midas::odb &arg) { int hotlinkIndex = arg.get_last_index(); if (hotlinkIndex == SPIndex) { - pullSPValue(); } else if (hotlinkIndex == temperatureIndex) { pullTemperatureValue(); } else if (hotlinkIndex == powerIndex) { diff --git a/src/device/InputHandlerConfig.h b/src/device/InputHandlerConfig.h index 258905e..d007c5b 100644 --- a/src/device/InputHandlerConfig.h +++ b/src/device/InputHandlerConfig.h @@ -6,9 +6,7 @@ namespace InputHandlerConfig { const std::string PATH_PREFIX = "/Equipment/"; -const std::string PATH_SUFFIX = "/Variables"; - -const std::string INPUT_VARIABLE = "Input"; +const std::string PATH_SUFFIX = "/Variables/Input"; } // namespace InputHandlerConfig #endif \ No newline at end of file diff --git a/src/device/OutputHandlerConfig.h b/src/device/OutputHandlerConfig.h new file mode 100644 index 0000000..46a6087 --- /dev/null +++ b/src/device/OutputHandlerConfig.h @@ -0,0 +1,13 @@ +#ifndef OUTPUT_HANDLER_CONFIG_H +#define OUTPUT_HANDLER_CONFIG_H + +#include + +namespace OutputHandlerConfig { +const std::string PATH_PREFIX = "/Equipment/"; +const std::string PATH_SUFFIX = "/Variables"; + +const std::string OUTPUT_VARIABLE = "Output"; +} // namespace OutputHandlerConfig + +#endif \ No newline at end of file diff --git a/src/device/PressureCalculator.cpp b/src/device/PressureCalculator.cpp index 58cc47b..a7f1ab6 100644 --- a/src/device/PressureCalculator.cpp +++ b/src/device/PressureCalculator.cpp @@ -29,13 +29,14 @@ void PressureCalculator::update() { Event event = {}; /// ERROR CHECK IF CONDITION IS ALREADY IN LEGACY CODE - if (c2contrib != 0) { - event.c1contrib = c1contrib; - event.c2contrib = c2contrib; - event.uncapedPressure = uncapedPressure; - event.cappedPressure = cappedPressure; - event.averagePower = averagePower; - } + + printf("capped %f et uncapped %f\n", cappedPressure, uncapedPressure); + + event.c1contrib = c1contrib; + event.c2contrib = c2contrib; + event.uncapedPressure = uncapedPressure; + event.cappedPressure = cappedPressure; + event.averagePower = averagePower; eventBus.publish(event); } @@ -97,6 +98,7 @@ void PressureCalculator::updateTemperature(float value) { void PressureCalculator::updatePower(float value) { cachedPower = value; + printf("%f recieved\n", value); average.addValue(static_cast(value)); update(); } diff --git a/src/device/SettingsHandler.cpp b/src/device/SettingsHandler.cpp index 4ee7480..69a40e0 100644 --- a/src/device/SettingsHandler.cpp +++ b/src/device/SettingsHandler.cpp @@ -7,7 +7,7 @@ using namespace SettingsHandlerConfig; void SettingsHandler::pullEquipmentName() { midas::odb o(this->path); - std::string equipmentName = o(SettingsHandlerConfig::EQUIPMENT_NAME); + std::string equipmentName = o[SettingsHandlerConfig::EQUIPMENT_NAME]; Event event = {EventType::EQUIPMENT_NAME, equipmentName}; eventBus.publish(event); } @@ -15,28 +15,28 @@ void SettingsHandler::pullEquipmentName() { void SettingsHandler::pullOutputPressureSPIndex() { midas::odb o(this->path); int outputPressureSPIndex = - o(SettingsHandlerConfig::OUTPUT_PRESSURE_SP_INDEX); + o[SettingsHandlerConfig::OUTPUT_PRESSURE_SP_INDEX]; Event event = {EventType::OUTPUT_PRESSURE_SP_INDEX, outputPressureSPIndex}; eventBus.publish(event); } void SettingsHandler::pullInputSPIndex() { midas::odb o(this->path); - int inputSPIndex = o(SettingsHandlerConfig::INPUT_SP_INDEX); + int inputSPIndex = o[SettingsHandlerConfig::INPUT_SP_INDEX]; Event event = {EventType::INPUT_SP_INDEX, inputSPIndex}; eventBus.publish(event); } void SettingsHandler::pullInputTemperatureIndex() { midas::odb o(this->path); - int temperatureIndex = o(SettingsHandlerConfig::INPUT_TEMPERATURE_INDEX); + int temperatureIndex = o[SettingsHandlerConfig::INPUT_TEMPERATURE_INDEX]; Event event = {EventType::INPUT_TEMPERATURE_INDEX, temperatureIndex}; eventBus.publish(event); } void SettingsHandler::pullInputPowerIndex() { midas::odb o(this->path); - int powerIndex = o(SettingsHandlerConfig::INPUT_POWER_INDEX); + int powerIndex = o[SettingsHandlerConfig::INPUT_POWER_INDEX]; Event event = {EventType::INPUT_POWER_INDEX, powerIndex}; eventBus.publish(event); } @@ -57,19 +57,21 @@ bool SettingsHandler::init() { bool areKeysValids = true; - if (!o.exists(SettingsHandlerConfig::EQUIPMENT_NAME)) { + if (!midas::odb::exists(path + SettingsHandlerConfig::EQUIPMENT_NAME)) { areKeysValids = false; } - if (!o.exists(SettingsHandlerConfig::OUTPUT_PRESSURE_SP_INDEX)) { + if (!midas::odb::exists(path + + SettingsHandlerConfig::OUTPUT_PRESSURE_SP_INDEX)) { areKeysValids = false; } - if (!o.exists(SettingsHandlerConfig::INPUT_SP_INDEX)) { + if (!midas::odb::exists(path + SettingsHandlerConfig::INPUT_SP_INDEX)) { areKeysValids = false; } - if (!o.exists(SettingsHandlerConfig::INPUT_TEMPERATURE_INDEX)) { + if (!midas::odb::exists(path + + SettingsHandlerConfig::INPUT_TEMPERATURE_INDEX)) { areKeysValids = false; } - if (!o.exists(SettingsHandlerConfig::INPUT_POWER_INDEX)) { + if (!midas::odb::exists(path + SettingsHandlerConfig::INPUT_POWER_INDEX)) { areKeysValids = false; } diff --git a/src/device/SettingsHandler.h b/src/device/SettingsHandler.h index cc51847..11e99a5 100644 --- a/src/device/SettingsHandler.h +++ b/src/device/SettingsHandler.h @@ -10,8 +10,6 @@ class SettingsHandler { public: SettingsHandler(EventBus &eventBusReference, std::string equipmentName); - bool init(); - bool setHotlink(); enum class EventType { EQUIPMENT_NAME, @@ -30,6 +28,8 @@ class SettingsHandler { std::string path; EventBus &eventBus; + bool init(); + void pullEquipmentName(); void pullOutputPressureSPIndex(); void pullInputSPIndex(); diff --git a/src/device/SettingsHandlerConfig.h b/src/device/SettingsHandlerConfig.h index 47ea854..4fec93a 100644 --- a/src/device/SettingsHandlerConfig.h +++ b/src/device/SettingsHandlerConfig.h @@ -8,7 +8,7 @@ namespace SettingsHandlerConfig { const std::string PATH_PREFIX = "/Equipment/"; const std::string PATH_SUFFIX = "/Settings/Devices/MITCPRESSC/DD/"; -const std::string EQUIPMENT_NAME = "MITC_equipment"; +const std::string EQUIPMENT_NAME = "MITC_Equipment"; const std::string OUTPUT_PRESSURE_SP_INDEX = "MITC_OutputPressSPIndex"; const std::string INPUT_SP_INDEX = "MITC_InputVarioxSPIndex"; const std::string INPUT_TEMPERATURE_INDEX = "MITC_InputVarioxTempIndex"; diff --git a/src/device/itc_pressure_optimizer.cpp b/src/device/itc_pressure_optimizer.cpp index 754dd5d..821718e 100644 --- a/src/device/itc_pressure_optimizer.cpp +++ b/src/device/itc_pressure_optimizer.cpp @@ -28,5 +28,6 @@ void itcPressureOptimizer::HandlePeriodic() {} TMFeResult itcPressureOptimizer::HandleInit(const std::vector &args) { eventBus.publish(EquipmentInitEvent{}); + printf("EventInit\n"); return TMFeResult(); }