1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/lib/hook.cpp

179 lines
3.7 KiB
C++
Raw Permalink Normal View History

2019-03-26 15:33:47 +01:00
/** Hook-releated functions.
*
* @author Steffen Vogel <post@steffenvogel.de>
2022-03-15 09:28:57 -04:00
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
2022-07-04 18:20:03 +02:00
* @license Apache 2.0
2019-03-26 15:33:47 +01:00
*********************************************************************************/
#include <cstring>
#include <cmath>
2019-03-26 15:33:47 +01:00
#include <villas/timing.hpp>
#include <villas/node/config.hpp>
2019-03-26 15:33:47 +01:00
#include <villas/hook.hpp>
#include <villas/node/exceptions.hpp>
#include <villas/path.hpp>
#include <villas/utils.hpp>
#include <villas/node.hpp>
2019-03-26 15:33:47 +01:00
const char *hook_reasons[] = {
"ok", "error", "skip-sample", "stop-processing"
};
using namespace villas;
using namespace villas::node;
Hook::Hook(Path *p, Node *n, int fl, int prio, bool en) :
2021-07-13 11:22:40 +02:00
logger(logging.get("hook")),
factory(nullptr),
state(fl & (int) Hook::Flags::BUILTIN
? State::CHECKED
: State::INITIALIZED), /* We dont need to parse builtin hooks. */
2019-03-26 15:33:47 +01:00
flags(fl),
priority(prio),
enabled(en),
path(p),
node(n),
2021-02-16 14:15:14 +01:00
config(nullptr)
{ }
2019-03-26 15:33:47 +01:00
void Hook::prepare(SignalList::Ptr sigs)
2019-03-26 15:33:47 +01:00
{
2019-06-23 16:13:23 +02:00
assert(state == State::CHECKED);
2019-03-26 15:33:47 +01:00
signals = sigs->clone();
2021-02-19 06:40:26 +01:00
prepare();
2019-06-23 16:13:23 +02:00
state = State::PREPARED;
2019-03-26 15:33:47 +01:00
}
2021-02-16 14:15:14 +01:00
void Hook::parse(json_t *json)
2019-03-26 15:33:47 +01:00
{
int ret;
json_error_t err;
2019-06-23 16:13:23 +02:00
assert(state != State::STARTED);
2019-03-26 15:33:47 +01:00
2021-07-06 18:34:30 +02:00
int prio = -1;
int en = -1;
2021-02-16 14:15:14 +01:00
ret = json_unpack_ex(json, &err, 0, "{ s?: i, s?: b }",
"priority", &prio,
"enabled", &en
2019-03-26 15:33:47 +01:00
);
if (ret)
2021-02-16 14:15:14 +01:00
throw ConfigError(json, err, "node-config-hook");
2019-03-26 15:33:47 +01:00
2021-07-06 18:34:30 +02:00
if (prio >= 0)
priority = prio;
2021-07-06 18:34:30 +02:00
if (en >= 0)
enabled = en;
2021-02-16 14:15:14 +01:00
config = json;
2019-03-26 15:33:47 +01:00
2019-06-23 16:13:23 +02:00
state = State::PARSED;
2019-03-26 15:33:47 +01:00
}
void SingleSignalHook::parse(json_t *json)
{
int ret;
json_error_t err;
json_t *json_signal;
Hook::parse(json);
ret = json_unpack_ex(json, &err, 0, "{ s: o }",
"signal", &json_signal
);
if (ret)
throw ConfigError(json, err, "node-config-hook");
if (!json_is_string(json_signal))
throw ConfigError(json_signal, "node-config-hook-signals", "Invalid value for setting 'signal'");
signalName = json_string_value(json_signal);
}
void SingleSignalHook::prepare()
{
Hook::prepare();
/* Setup mask */
int index = signals->getIndexByName(signalName.c_str());
if (index < 0)
throw RuntimeError("Failed to find signal {}", signalName);
signalIndex = (unsigned) index;
}
/* Multi Signal Hook */
void MultiSignalHook::parse(json_t *json)
{
int ret;
size_t i;
json_error_t err;
json_t *json_signals = nullptr;
json_t *json_signal = nullptr;
2021-07-06 18:34:30 +02:00
Hook::parse(json);
ret = json_unpack_ex(json, &err, 0, "{ s?: o, s?: o }",
"signals", &json_signals,
"signal", &json_signal
);
if (ret)
throw ConfigError(json, err, "node-config-hook");
if (json_signals) {
if (!json_is_array(json_signals))
throw ConfigError(json_signals, "node-config-hook-signals", "Setting 'signals' must be a list of signal names");
json_array_foreach(json_signals, i, json_signal) {
if (!json_is_string(json_signal))
throw ConfigError(json_signal, "node-config-hook-signals", "Invalid value for setting 'signals'");
2021-07-06 18:34:30 +02:00
const char *name = json_string_value(json_signal);
signalNames.push_back(name);
}
}
else if (json_signal) {
if (!json_is_string(json_signal))
throw ConfigError(json_signal, "node-config-hook-signals", "Invalid value for setting 'signals'");
2021-07-06 18:34:30 +02:00
const char *name = json_string_value(json_signal);
signalNames.push_back(name);
}
else
throw ConfigError(json, "node-config-hook-signals", "Missing 'signals' setting");
}
void MultiSignalHook::prepare()
{
2021-07-06 18:34:30 +02:00
Hook::prepare();
for (const auto &signalName : signalNames) {
int index = signals->getIndexByName(signalName.c_str());
if (index < 0)
throw RuntimeError("Failed to find signal {}", signalName);
signalIndices.push_back(index);
}
}
void MultiSignalHook::check()
{
2021-07-06 18:34:30 +02:00
Hook::check();
if (signalNames.size() == 0)
throw RuntimeError("At least a single signal must be provided");
}