101 lines
3.0 KiB
C++
101 lines
3.0 KiB
C++
#include "OutputHandler.h"
|
|
#include "DemandHandler.h"
|
|
#include "OutputHandlerConfig.h"
|
|
#include "PressureCalculator.h"
|
|
#include "SettingsHandler.h"
|
|
#include "tmfe.h"
|
|
#include <string>
|
|
|
|
void OutputHandler::enableSetPressure(bool enable) {
|
|
isSetPressureEnable = enable;
|
|
update();
|
|
}
|
|
|
|
void OutputHandler::setPressure(float value) {
|
|
pressure = value;
|
|
update();
|
|
}
|
|
|
|
void OutputHandler::setOutputPressureSPIndex(int index) {
|
|
outputPressureSPIndex = index;
|
|
update();
|
|
}
|
|
|
|
void OutputHandler::setEquipmentPath(std::string equipmentName) {
|
|
equipmentPath = OutputHandlerConfig::PATH_PREFIX + equipmentName +
|
|
OutputHandlerConfig::PATH_SUFFIX;
|
|
update();
|
|
}
|
|
|
|
void OutputHandler::update() {
|
|
if (equipmentPath.empty())
|
|
return;
|
|
static bool isIndexValid = true;
|
|
static bool isKeyValid = true;
|
|
midas::odb o(equipmentPath);
|
|
|
|
if (!midas::odb::exists(equipmentPath + "/" +
|
|
OutputHandlerConfig::OUTPUT_VARIABLE) &&
|
|
isKeyValid) {
|
|
TMFE::Instance()->Msg(MERROR, __FUNCTION__,
|
|
"Key at %s%s doesn't exists",
|
|
equipmentPath.c_str(),
|
|
OutputHandlerConfig::OUTPUT_VARIABLE.c_str());
|
|
isKeyValid = false;
|
|
} else {
|
|
isKeyValid = true;
|
|
}
|
|
|
|
if ((o[OutputHandlerConfig::OUTPUT_VARIABLE].size() <
|
|
outputPressureSPIndex) &&
|
|
isIndexValid) {
|
|
TMFE::Instance()->Msg(MERROR, __FUNCTION__,
|
|
"SP index is %i, where array size is %i at %s.",
|
|
outputPressureSPIndex, o.size(),
|
|
equipmentPath.c_str());
|
|
isIndexValid = false;
|
|
} else {
|
|
isIndexValid = true;
|
|
}
|
|
|
|
//// SHOULD I CRASH IF KEY NOT VALID ?
|
|
//// FOR NOW, FAILED SILENTLY
|
|
|
|
if (isSetPressureEnable && isKeyValid)
|
|
o(OutputHandlerConfig::OUTPUT_VARIABLE)[outputPressureSPIndex] =
|
|
pressure;
|
|
}
|
|
|
|
OutputHandler::OutputHandler(EventBus &eventBusReference) {
|
|
|
|
eventBusReference.subscribe<DemandHandler::Event>(
|
|
[this](const DemandHandler::Event &e) {
|
|
switch (e.type) {
|
|
case DemandHandler::EventType::SET_PRESSURE:
|
|
enableSetPressure(std::get<bool>(e.value));
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
});
|
|
|
|
eventBusReference.subscribe<SettingsHandler::Event>(
|
|
[this](const SettingsHandler::Event &e) {
|
|
switch (e.type) {
|
|
case SettingsHandler::EventType::EQUIPMENT_NAME:
|
|
setEquipmentPath(std::get<std::string>(e.value));
|
|
break;
|
|
case SettingsHandler::EventType::OUTPUT_PRESSURE_SP_INDEX:
|
|
setOutputPressureSPIndex(std::get<int>(e.value));
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
});
|
|
|
|
eventBusReference.subscribe<PressureCalculator::Event>(
|
|
[this](const PressureCalculator::Event &e) {
|
|
setPressure(e.cappedPressure);
|
|
});
|
|
}
|