130 lines
4.0 KiB
C++
130 lines
4.0 KiB
C++
#include "InputHandler.h"
|
|
#include "../EventBus.h"
|
|
#include "InputHandlerConfig.h"
|
|
#include "SettingsHandler.h"
|
|
|
|
#include "tmfe.h"
|
|
#include <string>
|
|
#include <variant>
|
|
|
|
void InputHandler::setPath(std::string name) {
|
|
this->path = InputHandlerConfig::PATH_PREFIX + name +
|
|
InputHandlerConfig::PATH_SUFFIX;
|
|
|
|
if (!midas::odb::exists(path)) {
|
|
TMFE::Instance()->Msg(MERROR, __FUNCTION__, "Key at %s doesn't exists",
|
|
path.c_str());
|
|
|
|
throw std::runtime_error("Key at " + path + " doesn't exists");
|
|
}
|
|
|
|
midas::odb o(path);
|
|
bool isKeyValid = true;
|
|
|
|
if (o.size() < SPIndex) {
|
|
TMFE::Instance()->Msg(MERROR, __FUNCTION__,
|
|
"SP index is %i, where array size is %i.",
|
|
SPIndex, o.size());
|
|
isKeyValid = false;
|
|
}
|
|
if (o.size() < temperatureIndex) {
|
|
TMFE::Instance()->Msg(
|
|
MERROR, __FUNCTION__,
|
|
"Temperature index is %i, where array size is %i.",
|
|
temperatureIndex, o.size());
|
|
isKeyValid = false;
|
|
}
|
|
if (o.size() < powerIndex) {
|
|
TMFE::Instance()->Msg(MERROR, __FUNCTION__,
|
|
"Power index is %i, where array size is %i.",
|
|
powerIndex, o.size());
|
|
isKeyValid = false;
|
|
}
|
|
|
|
if (!isKeyValid) {
|
|
throw std::runtime_error("InputHandler update path failed - see MIDAS "
|
|
"console and take care of all the errors\n");
|
|
}
|
|
}
|
|
|
|
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;
|
|
pullPowerValue();
|
|
}
|
|
|
|
void InputHandler::pullSPValue() {
|
|
midas::odb o(this->path);
|
|
float SPValue = o[SPIndex];
|
|
Event event = {EventType::SP_VALUE, SPValue};
|
|
eventBus.publish(event);
|
|
}
|
|
|
|
void InputHandler::pullTemperatureValue() {
|
|
midas::odb o(this->path);
|
|
float temperatureValue = o[temperatureIndex];
|
|
Event event = {EventType::TEMPERATURE_VALUE, temperatureValue};
|
|
eventBus.publish(event);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
void InputHandler::setHotlink() {
|
|
if (hotlink.has_value()) {
|
|
TMFE::Instance()->Msg(MERROR, __FUNCTION__,
|
|
"Hotlink already established. Will not update it "
|
|
"without restarting the frontend.");
|
|
return;
|
|
}
|
|
hotlink.emplace(path);
|
|
hotlink->watch([&](midas::odb &arg) {
|
|
int hotlinkIndex = arg.get_last_index();
|
|
if (hotlinkIndex == SPIndex) {
|
|
} else if (hotlinkIndex == temperatureIndex) {
|
|
pullTemperatureValue();
|
|
} else if (hotlinkIndex == powerIndex) {
|
|
pullPowerValue();
|
|
}
|
|
});
|
|
}
|
|
|
|
InputHandler::InputHandler(EventBus &eventBusReference)
|
|
: eventBus(eventBusReference) {
|
|
|
|
eventBus.subscribe<SettingsHandler::Event>(
|
|
[this](const SettingsHandler::Event &e) {
|
|
switch (e.type) {
|
|
case SettingsHandler::EventType::EQUIPMENT_NAME:
|
|
setPath(std::get<std::string>(e.value));
|
|
setHotlink();
|
|
break;
|
|
case SettingsHandler::EventType::INPUT_SP_INDEX:
|
|
updateSPIndex(std::get<int>(e.value));
|
|
break;
|
|
case SettingsHandler::EventType::INPUT_TEMPERATURE_INDEX:
|
|
updateTemperatureIndex(std::get<int>(e.value));
|
|
break;
|
|
case SettingsHandler::EventType::INPUT_POWER_INDEX:
|
|
updatePowerIndex(std::get<int>(e.value));
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
});
|
|
}
|