Files
itc_pressure_optimizer/src/device/handlers/DemandHandler.cpp
T

141 lines
4.2 KiB
C++

#include "DemandHandler.h"
#include "../itc_pressure_optimizer.h"
#include "DemandHandlerConfig.h"
#include <functional>
#include <string>
#include "odbxx.h"
#include "tmfe.h"
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;
// This is checked every start, in case of someone change the index in the
// config header
if (o.size() < static_cast<int>(ParameterIndex::MINIMAL_PRESSURE)) {
indexValid = false;
}
if (o.size() < static_cast<int>(ParameterIndex::MAXIMAL_PRESSURE)) {
indexValid = false;
}
if (o.size() < static_cast<int>(ParameterIndex::CONSTANTE_1)) {
indexValid = false;
}
if (o.size() < static_cast<int>(ParameterIndex::CONSTANTE_2)) {
indexValid = false;
}
if (o.size() < static_cast<int>(ParameterIndex::PRESSURE_CONTROL_MODE)) {
indexValid = false;
}
if (o.size() < static_cast<int>(ParameterIndex::SET_PRESSURE)) {
indexValid = false;
}
if (!indexValid)
return false;
this->pullMinimalPressure();
this->pullMaximalPressure();
this->pullConstante1();
this->pullConstante2();
this->pullPressureControlMode();
return true;
}
bool DemandHandler::setHotlink() {
midas::odb to_watch(this->path);
to_watch.watch([&](midas::odb &arg) {
switch (arg.get_last_index()) {
case static_cast<int>(ParameterIndex::MINIMAL_PRESSURE):
pullMinimalPressure();
break;
case static_cast<int>(ParameterIndex::MAXIMAL_PRESSURE):
pullMaximalPressure();
break;
case static_cast<int>(ParameterIndex::CONSTANTE_1):
pullConstante1();
break;
case static_cast<int>(ParameterIndex::CONSTANTE_2):
pullConstante2();
break;
case static_cast<int>(ParameterIndex::PRESSURE_CONTROL_MODE):
pullPressureControlMode();
break;
case static_cast<int>(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[static_cast<int>(ParameterIndex::MINIMAL_PRESSURE)];
Event event = {EventType::MINIMAL_PRESSURE, minimalPressure};
eventBus.publish(event);
}
void DemandHandler::pullMaximalPressure() {
midas::odb o(this->path);
float maximalPressure =
o[static_cast<int>(ParameterIndex::MAXIMAL_PRESSURE)];
Event event = {EventType::MAXIMAL_PRESSURE, maximalPressure};
eventBus.publish(event);
}
void DemandHandler::pullConstante1() {
midas::odb o(this->path);
float constante1 = o[static_cast<int>(ParameterIndex::CONSTANTE_1)];
Event event = {EventType::CONSTANTE_1, constante1};
eventBus.publish(event);
}
void DemandHandler::pullConstante2() {
midas::odb o(this->path);
float constance2 = o[static_cast<int>(ParameterIndex::CONSTANTE_2)];
Event event = {EventType::CONSTANTE_2, constance2};
eventBus.publish(event);
}
void DemandHandler::pullPressureControlMode() {
midas::odb o(this->path);
float pressureControlMode =
o[static_cast<int>(ParameterIndex::PRESSURE_CONTROL_MODE)];
Event event = {EventType::PRESSURE_CONTROL_MODE, pressureControlMode != 0};
eventBus.publish(event);
}
void DemandHandler::pullSetPressure() {
midas::odb o(this->path);
float setPressure = o[static_cast<int>(ParameterIndex::SET_PRESSURE)];
Event event = {EventType::SET_PRESSURE, setPressure != 0};
eventBus.publish(event);
}
DemandHandler::DemandHandler(EventBus &eventBusReference,
std::string equipmentName)
: path(DemanHandlerConfig::PATH_PREFIX + equipmentName +
DemanHandlerConfig::PATH_SUFFIX),
eventBus(eventBusReference) {
eventBus.subscribe<itcPressureOptimizer::EquipmentInitEvent>(
[this](const itcPressureOptimizer::EquipmentInitEvent &e) {
this->init();
this->setHotlink();
});
}