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

111 lines
2.2 KiB
C++
Raw Normal View History

/* Average hook.
2019-03-26 15:33:47 +01:00
*
* Author: Steffen Vogel <post@steffenvogel.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
2019-03-26 15:33:47 +01:00
2019-04-07 15:44:00 +02:00
#include <bitset>
#include <cstring>
2019-03-26 15:33:47 +01:00
#include <villas/hook.hpp>
#include <villas/node/exceptions.hpp>
#include <villas/signal.hpp>
#include <villas/sample.hpp>
2019-03-26 15:33:47 +01:00
namespace villas {
namespace node {
class AverageHook : public MultiSignalHook {
2019-03-26 15:33:47 +01:00
protected:
unsigned offset;
2019-03-26 15:33:47 +01:00
public:
AverageHook(Path *p, Node *n, int fl, int prio, bool en = true) :
MultiSignalHook(p, n, fl, prio, en),
offset(0)
{ }
2019-03-26 15:33:47 +01:00
virtual
void prepare()
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
MultiSignalHook::prepare();
2019-03-27 13:47:09 +01:00
// Add averaged signal
auto avg_sig = std::make_shared<Signal>("average", "", SignalType::FLOAT);
2019-03-26 15:33:47 +01:00
if (!avg_sig)
throw RuntimeError("Failed to create new signal");
signals->insert(signals->begin() + offset, avg_sig);
2019-03-26 15:33:47 +01:00
2019-06-23 16:13:23 +02:00
state = State::PREPARED;
2019-03-26 15:33:47 +01:00
}
virtual
void 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
MultiSignalHook::parse(json);
2019-03-26 15:33:47 +01:00
ret = json_unpack_ex(json, &err, 0, "{ s: i }",
"offset", &offset
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-average");
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
}
virtual
Hook::Reason process(struct Sample *smp)
2019-03-26 15:33:47 +01:00
{
double avg, sum = 0;
int n = 0;
2019-06-23 16:13:23 +02:00
assert(state == State::STARTED);
2019-03-26 15:33:47 +01:00
for (unsigned index : signalIndices) {
switch (sample_format(smp, index)) {
2019-06-23 16:13:23 +02:00
case SignalType::INTEGER:
sum += smp->data[index].i;
2019-03-26 15:33:47 +01:00
break;
2019-06-23 16:13:23 +02:00
case SignalType::FLOAT:
sum += smp->data[index].f;
2019-03-26 15:33:47 +01:00
break;
2019-06-23 16:13:23 +02:00
case SignalType::INVALID:
case SignalType::COMPLEX:
case SignalType::BOOLEAN:
return Hook::Reason::ERROR; // not supported
2019-03-26 15:33:47 +01:00
}
n++;
}
2019-03-27 13:47:09 +01:00
avg = sum / n;
if (offset >= smp->length)
return Reason::ERROR;
sample_data_insert(smp, (union SignalData *) &avg, offset, 1);
2019-03-26 15:33:47 +01:00
2019-06-23 16:13:23 +02:00
return Reason::OK;
2019-03-26 15:33:47 +01:00
}
};
// Register hook
static char n[] = "average";
static char d[] = "Calculate average over some signals";
static HookPlugin<AverageHook, n , d, (int) Hook::Flags::PATH | (int) Hook::Flags::NODE_READ | (int) Hook::Flags::NODE_WRITE> p;
2019-03-26 15:33:47 +01:00
} // namespace node
} // namespace villas