From bb596ca10051ff694b68ba4e115f1f7b98af1dbb Mon Sep 17 00:00:00 2001 From: Hugo Jean Ponsin Date: Thu, 16 Jul 2026 13:11:01 +0200 Subject: [PATCH] demandHandler working --- CMakeLists.txt | 11 +-- main.cpp | 2 +- src/device/DemandHandler.cpp | 82 ++++++++++++-------- src/device/DemandHandler.h | 26 ++++--- src/device/DemandHandlerConfig.h | 6 +- src/device/{EventBus.cpp => EventBus.h} | 7 +- src/device/FeedbackHandler.h | 2 +- src/device/itc_pressure_optimizer.cpp | 33 ++++++++ src/device/itc_pressure_optimizer.h | 48 ++++++++++++ src/frontend/itc_pressure_optimizer_scfe.cpp | 2 +- 10 files changed, 159 insertions(+), 60 deletions(-) rename src/device/{EventBus.cpp => EventBus.h} (97%) create mode 100644 src/device/itc_pressure_optimizer.cpp create mode 100644 src/device/itc_pressure_optimizer.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8716eb4..50a2740 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,7 @@ find_package(Midas REQUIRED) add_library( itcPressureOptimizer src/device/itc_pressure_optimizer.cpp + src/device/DemandHandler.cpp ) set_property( @@ -49,11 +50,6 @@ target_include_directories( itcPressureOptimizer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} - PRIVATE - ${EPICSSYS}/include - ${EPICSSYS}/include/os/Linux - ${EPICSSYS}/include/compiler/gcc - ${EPICSSYS}/include/compiler/clang ) ################################################################################ @@ -75,11 +71,6 @@ target_include_directories( itcPressureOptimizerScfe PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} - PRIVATE - ${EPICSSYS}/include - ${EPICSSYS}/include/os/Linux - ${EPICSSYS}/include/compiler/gcc - ${EPICSSYS}/include/compiler/clang ) target_link_libraries( diff --git a/main.cpp b/main.cpp index 51e8b34..0779082 100644 --- a/main.cpp +++ b/main.cpp @@ -5,7 +5,7 @@ #include #include -#include "src/device/EventBus.cpp" +#include "src/device/EventBus.h" class ProdA {}; class ProdB {}; diff --git a/src/device/DemandHandler.cpp b/src/device/DemandHandler.cpp index 3750a16..4e9f7d9 100644 --- a/src/device/DemandHandler.cpp +++ b/src/device/DemandHandler.cpp @@ -1,28 +1,34 @@ #include "DemandHandler.h" #include "DemandHandlerConfig.h" +#include "itc_pressure_optimizer.h" #include #include -bool DemandHandler::init() { - midas::odb o(this->path); +#include "odbxx.h" +#include "tmfe.h" - if (!o.exists()) { - // TODO : ERROR MESSAGE +using namespace DemanHandlerConfig; + +bool DemandHandler::init() { + + if (!midas::odb::exists(this->path)) { + printf("[DEMANDE HANDLER] : Path doesn't exist\n"); return false; } + midas::odb o(this->path); bool indexValid = true; - if (o.size() < ParameterIndex::MINIMAL_PRESSURE) { + if (o.size() < static_cast(ParameterIndex::MINIMAL_PRESSURE)) { indexValid = false; } - if (o.size() < ParameterIndex::MAXIMAL_PRESSURE) { + if (o.size() < static_cast(ParameterIndex::MAXIMAL_PRESSURE)) { indexValid = false; } - if (o.size() < ParameterIndex::CONSTANTE_1) { + if (o.size() < static_cast(ParameterIndex::CONSTANTE_1)) { indexValid = false; } - if (o.size() < ParameterIndex::CONSTANTE_2) { + if (o.size() < static_cast(ParameterIndex::CONSTANTE_2)) { indexValid = false; } @@ -39,21 +45,21 @@ bool DemandHandler::init() { bool DemandHandler::setHotlink() { midas::odb to_watch(this->path); - to_watch([&](midas::odb &arg) { + to_watch.watch([&](midas::odb &arg) { switch (arg.get_last_index()) { - case ParameterIndex::MINIMAL_PRESSURE: + case static_cast(ParameterIndex::MINIMAL_PRESSURE): pullMinimalPressure(); break; - case ParameterIndex::MAXIMAL_PRESSURE: + case static_cast(ParameterIndex::MAXIMAL_PRESSURE): pullMaximalPressure(); break; - case ParameterIndex::MINIMAL_PRESSURE: + case static_cast(ParameterIndex::CONSTANTE_1): pullConstante1(); break; - case ParameterIndex::MINIMAL_PRESSURE: + case static_cast(ParameterIndex::CONSTANTE_2): pullConstante2(); break; - case ParameterIndex::PRESSURE_CONTROL_MODE: + case static_cast(ParameterIndex::PRESSURE_CONTROL_MODE): pullPressureControlMode(); break; default: @@ -61,47 +67,55 @@ bool DemandHandler::setHotlink() { break; } }); + return true; } void DemandHandler::pullMinimalPressure() { midas::odb o(this->path); - float minimalPressure = o[ParameterIndex::MINIMAL_PRESSURE]; - pressureCalculator.get().updateMinimalPressure(minimalPressure); + float minimalPressure = + o[static_cast(ParameterIndex::MINIMAL_PRESSURE)]; + Event event = {EventType::MINIMAL_PRESSURE, minimalPressure}; + eventBus.publish(event); } void DemandHandler::pullMaximalPressure() { midas::odb o(this->path); - float maximalPressure = o[ParameterIndex::MAXIMAL_PRESSURE]; - pressureCalculator.get().updateMinimalPressure(maximalPressure); + float maximalPressure = + o[static_cast(ParameterIndex::MAXIMAL_PRESSURE)]; + Event event = {EventType::MAXIMAL_PRESSURE, maximalPressure}; + eventBus.publish(event); } void DemandHandler::pullConstante1() { midas::odb o(this->path); - float constante1 = o[ParameterIndex::CONSTANTE_1]; - pressureCalculator.get().updateMinimalPressure(constante1); - feedbackHandler.get().setConstante1(constante1); + float constante1 = o[static_cast(ParameterIndex::CONSTANTE_1)]; + Event event = {EventType::CONSTANTE_1, constante1}; + eventBus.publish(event); } void DemandHandler::pullConstante2() { midas::odb o(this->path); - float constance2 = o[ParameterIndex::CONSTANTE_2]; - pressureCalculator.get().updateMinimalPressure(constance2); - feedbackHandler.get().setConstante2(constante2); + float constance2 = o[static_cast(ParameterIndex::CONSTANTE_2)]; + Event event = {EventType::CONSTANTE_2, constance2}; + eventBus.publish(event); } void DemandHandler::pullPressureControlMode() { midas::odb o(this->path); - float pressureControlMode = o[ParameterIndex::PRESSURE_CONTROL_MODE]; - feedbackHandler.get().enablePressureControlMode(pressureControlMode); + float pressureControlMode = + o[static_cast(ParameterIndex::PRESSURE_CONTROL_MODE)]; + Event event = {EventType::PRESSURE_CONTROL_MODE, pressureControlMode != 0}; + eventBus.publish(event); } -DemandHandler::DemandHandler( - std::string equipmentName, - std::reference_wrapper feedbackHandler, - std::reference_wrapper pressureCalculator) { +DemandHandler::DemandHandler(EventBus &eventBusReference, + std::string equipmentName) + : path(DemanHandlerConfig::PATH_PREFIX + equipmentName + + DemanHandlerConfig::PATH_SUFFIX), + eventBus(eventBusReference) { - this->path = DemanHandlerConfig::PATH_PREFIX + equipmentName + - DemanHandlerConfig::PATH_SUFFIX; - this->feedbackHandler = feedbackHandler; - this->pressureCalculator = pressureCalculator; + eventBus.subscribe( + [this](const itcPressureOptimizer::EquipmentInitEvent &e) { + printf("[EquipmentInitEvent] : init recived\n"); + }); } diff --git a/src/device/DemandHandler.h b/src/device/DemandHandler.h index be08948..f694fb7 100644 --- a/src/device/DemandHandler.h +++ b/src/device/DemandHandler.h @@ -1,16 +1,27 @@ #ifndef DEMAND_HANDLER_H #define DEMAND_HANDLER_H -#include "FeedbackHandler.h" -#include "PressureCalculator.h" -#include +#include "EventBus.h" #include +#include class DemandHandler { private: std::string path; - std::reference_wrapper feedbackHandler; - std::reference_wrapper pressureCalculator; + EventBus &eventBus; + + enum class EventType { + MINIMAL_PRESSURE, + MAXIMAL_PRESSURE, + CONSTANTE_1, + CONSTANTE_2, + PRESSURE_CONTROL_MODE + }; + + struct Event { + EventType type; + std::variant value; + }; void pullMinimalPressure(); void pullMaximalPressure(); @@ -19,10 +30,7 @@ class DemandHandler { void pullPressureControlMode(); public: - DemandHandler( - std::string equipmentName, - std::reference_wrapper feedbackHandler, - std::reference_wrapper pressureCalculator); + DemandHandler(EventBus &eventBusReference, std::string equipmentName); /* @brief Manualy get all the value, and update owned objects */ diff --git a/src/device/DemandHandlerConfig.h b/src/device/DemandHandlerConfig.h index 1bdf8ff..855efd7 100644 --- a/src/device/DemandHandlerConfig.h +++ b/src/device/DemandHandlerConfig.h @@ -7,14 +7,14 @@ namespace DemanHandlerConfig { const std::string PATH_PREFIX = "/Equipment/"; const std::string PATH_SUFFIX = "/Demand"; -class enum ParameterIndex { +enum class ParameterIndex { PRESSURE_CONTROL_MODE = 6, MINIMAL_PRESSURE = 8, MAXIMAL_PRESSURE = 9, CONSTANTE_1 = 10, CONSTANTE_2 = 11 -} +}; -} // namespace DemanHandlerConfig +}; // namespace DemanHandlerConfig #endif diff --git a/src/device/EventBus.cpp b/src/device/EventBus.h similarity index 97% rename from src/device/EventBus.cpp rename to src/device/EventBus.h index 9b846cb..7259957 100644 --- a/src/device/EventBus.cpp +++ b/src/device/EventBus.h @@ -1,3 +1,6 @@ +#ifndef EVENT_BUS_H +#define EVENT_BUS_H + #include #include #include @@ -55,4 +58,6 @@ class EventBus { } } } -}; \ No newline at end of file +}; + +#endif \ No newline at end of file diff --git a/src/device/FeedbackHandler.h b/src/device/FeedbackHandler.h index 1e11f22..2f40ba0 100644 --- a/src/device/FeedbackHandler.h +++ b/src/device/FeedbackHandler.h @@ -17,7 +17,7 @@ class FeedbackHandler { Constante2 = 11 }; - setValueToOdb(int index, float value); + void setValueToOdb(int index, float value); public: FeedbackHandler(std::string equipmentName); diff --git a/src/device/itc_pressure_optimizer.cpp b/src/device/itc_pressure_optimizer.cpp new file mode 100644 index 0000000..eac4626 --- /dev/null +++ b/src/device/itc_pressure_optimizer.cpp @@ -0,0 +1,33 @@ +#include "itc_pressure_optimizer.h" + +#include "DemandHandler.h" +#include "EventBus.h" +// #include "FeedbackHandler.h" +// #include "InputHandler.h" +// #include "OutputHandler.h" +// #include "PressureCalculator.h" +// #include "SettingsHandler.h" + +#include + +itcPressureOptimizer::itcPressureOptimizer(std::string equipmentName, + const char *equipmentFilename) + : TMFeEquipment(equipmentName.c_str(), equipmentFilename), eventBus(), + demandHandler(eventBus, equipmentName) { + fEqConfPeriodMilliSec = 1000; // refresh every seconds + fEqConfLogHistory = 1; + fEqConfReadOnlyWhenRunning = false; + fEqConfWriteEventsToOdb = true; +} + +/*, feedbackHandler(), inputHandler(), +outputHandler(), pressureCalculator(), + settingsHandler()*/ + +void itcPressureOptimizer::HandlePeriodic() {} + +TMFeResult +itcPressureOptimizer::HandleInit(const std::vector &args) { + eventBus.publish(EquipmentInitEvent{}); + return TMFeResult(); +} diff --git a/src/device/itc_pressure_optimizer.h b/src/device/itc_pressure_optimizer.h new file mode 100644 index 0000000..41ae383 --- /dev/null +++ b/src/device/itc_pressure_optimizer.h @@ -0,0 +1,48 @@ +#ifndef ITC_PRESSURE_OPTIMIZER_H +#define ITC_PRESSURE_OPTIMIZER_H + +#include "DemandHandler.h" +#include "EventBus.h" +// #include "FeedbackHandler.h" +// #include "InputHandler.h" +// #include "OutputHandler.h" +// #include "PressureCalculator.h" +// #include "SettingsHandler.h" + +#include "tmfe.h" +#include + +class itcPressureOptimizer : public TMFeEquipment { + + public: + struct EquipmentInitEvent {}; + + struct EquipmentPollEvent {}; + /** + * @brief Constructor + * @param equipmentName for debug purpose, cpp file of the equipment + */ + itcPressureOptimizer(std::string equipmentName, + const char *equipmentFilename); + + /** + * @brief Method overridden called periodically by Midas + */ + void HandlePeriodic(); + + /** + * @brief Method overridden called by Midas at initialization + */ + TMFeResult HandleInit(const std::vector &args); + + private: + EventBus eventBus; + DemandHandler demandHandler; + // FeedbackHandler feedbackHandler; + // InputHandler inputHandler; + // OutputHandler outputHandler; + // PressureCalculator pressureCalculator; + // SettingsHandler settingsHandler; +}; + +#endif \ No newline at end of file diff --git a/src/frontend/itc_pressure_optimizer_scfe.cpp b/src/frontend/itc_pressure_optimizer_scfe.cpp index 83b82f6..cbd1a36 100644 --- a/src/frontend/itc_pressure_optimizer_scfe.cpp +++ b/src/frontend/itc_pressure_optimizer_scfe.cpp @@ -8,7 +8,7 @@ itcPressureOptimizerScfe::itcPressureOptimizerScfe(std::string frontendName, std::string equipmentName) : TMFrontend() { FeSetName(frontendName.c_str()); - FeAddEquipment(new itcPressureOptimizer(equipmentName, __FILE__, 12)); + FeAddEquipment(new itcPressureOptimizer(equipmentName, __FILE__)); } int main(int argc, char *argv[]) {