#include "DemandHandler.h" #include "../itc_pressure_optimizer.h" #include "DemandHandlerConfig.h" #include "odbxx.h" #include "tmfe.h" #include #include using namespace DemandHandlerConfig; bool DemandHandler::init() { bool initSuccess = true; if (!midas::odb::exists(this->path + "/" + DemandHandlerConfig::DEMAND)) { TMFE::Instance()->Msg(MERROR, __FUNCTION__, "Key at %s/%s doesn't exists", path.c_str(), DemandHandlerConfig::DEMAND.c_str()); midas::odb o(this->path); std::vector v; v.resize(static_cast(ParameterIndex::MAXIMUM_PARAMETER_INDEX)); o[DemandHandlerConfig::DEMAND] = v; TMFE::Instance()->Msg(MERROR, __FUNCTION__, "Template generated at %s. Please fill it.", path.c_str()); initSuccess = false; } midas::odb o(this->path); // This is checked every start, in case of someone change the index in the // config header if (o[DemandHandlerConfig::DEMAND].size() < static_cast(ParameterIndex::MINIMAL_PRESSURE)) { TMFE::Instance()->Msg( MERROR, __FUNCTION__, "Minimal pressure index is %i, where array size is %i.", static_cast(ParameterIndex::MINIMAL_PRESSURE), o.size()); initSuccess = false; } if (o[DemandHandlerConfig::DEMAND].size() < static_cast(ParameterIndex::MAXIMAL_PRESSURE)) { TMFE::Instance()->Msg( MERROR, __FUNCTION__, "maximal pressure index is %i, where array size is %i.", static_cast(ParameterIndex::MAXIMAL_PRESSURE), o.size()); initSuccess = false; } if (o[DemandHandlerConfig::DEMAND].size() < static_cast(ParameterIndex::CONSTANT_1)) { TMFE::Instance()->Msg(MERROR, __FUNCTION__, "Constant 1 index is %i, where array size is %i.", static_cast(ParameterIndex::CONSTANT_1), o.size()); initSuccess = false; } if (o[DemandHandlerConfig::DEMAND].size() < static_cast(ParameterIndex::CONSTANT_2)) { TMFE::Instance()->Msg(MERROR, __FUNCTION__, "Constant 2 index is %i, where array size is %i.", static_cast(ParameterIndex::CONSTANT_2), o.size()); initSuccess = false; } if (o[DemandHandlerConfig::DEMAND].size() < static_cast(ParameterIndex::PRESSURE_CONTROL_MODE)) { TMFE::Instance()->Msg( MERROR, __FUNCTION__, "Pressure control mode index is %i, where array size is %i.", static_cast(ParameterIndex::PRESSURE_CONTROL_MODE), o.size()); initSuccess = false; } if (o[DemandHandlerConfig::DEMAND].size() < static_cast(ParameterIndex::SET_PRESSURE)) { TMFE::Instance()->Msg( MERROR, __FUNCTION__, "Set pressure index is %i, where array size is %i.", static_cast(ParameterIndex::SET_PRESSURE), o.size()); initSuccess = false; } if (!initSuccess) throw std::runtime_error("DemandHandler init failed - see MIDAS " "console and take care of all the errors\n"); this->pullMinimalPressure(); this->pullMaximalPressure(); this->pullConstant1(); this->pullConstant2(); this->pullPressureControlMode(); return true; } bool DemandHandler::setHotlink() { midas::odb to_watch(this->path + "/" + DemandHandlerConfig::DEMAND); to_watch.watch([&](midas::odb &arg) { switch (arg.get_last_index()) { case static_cast(ParameterIndex::MINIMAL_PRESSURE): pullMinimalPressure(); break; case static_cast(ParameterIndex::MAXIMAL_PRESSURE): pullMaximalPressure(); break; case static_cast(ParameterIndex::CONSTANT_1): pullConstant1(); break; case static_cast(ParameterIndex::CONSTANT_2): pullConstant2(); break; case static_cast(ParameterIndex::PRESSURE_CONTROL_MODE): pullPressureControlMode(); break; case static_cast(ParameterIndex::SET_PRESSURE): pullSetPressure(); break; default: // Update out of scope, discarded break; } }); return true; } void DemandHandler::pullMinimalPressure() { midas::odb o(this->path); float minimalPressure = o[DemandHandlerConfig::DEMAND] [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[DemandHandlerConfig::DEMAND] [static_cast(ParameterIndex::MAXIMAL_PRESSURE)]; Event event = {EventType::MAXIMAL_PRESSURE, maximalPressure}; eventBus.publish(event); } void DemandHandler::pullConstant1() { midas::odb o(this->path); float constante1 = o[DemandHandlerConfig::DEMAND] [static_cast(ParameterIndex::CONSTANT_1)]; Event event = {EventType::CONSTANT_1, constante1}; eventBus.publish(event); } void DemandHandler::pullConstant2() { midas::odb o(this->path); float constance2 = o[DemandHandlerConfig::DEMAND] [static_cast(ParameterIndex::CONSTANT_2)]; Event event = {EventType::CONSTANT_2, constance2}; eventBus.publish(event); } void DemandHandler::pullPressureControlMode() { midas::odb o(this->path); float pressureControlMode = o[DemandHandlerConfig::DEMAND] [static_cast(ParameterIndex::PRESSURE_CONTROL_MODE)]; Event event = {EventType::ENABLE_PRESSURE_CONTROL_MODE, pressureControlMode != 0}; eventBus.publish(event); } void DemandHandler::pullSetPressure() { midas::odb o(this->path); float setPressure = o[DemandHandlerConfig::DEMAND] [static_cast(ParameterIndex::SET_PRESSURE)]; Event event = {EventType::ENABLE_SET_PRESSURE, setPressure != 0}; eventBus.publish(event); } DemandHandler::DemandHandler(EventBus &eventBusReference, std::string equipmentName) : path(DemandHandlerConfig::PATH_PREFIX + equipmentName + DemandHandlerConfig::PATH_SUFFIX), eventBus(eventBusReference) { eventBus.subscribe( [this](const itcPressureOptimizer::EquipmentInitEvent &e) { this->init(); this->setHotlink(); }); }