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

123 lines
2.9 KiB
C++
Raw Normal View History

2019-03-26 15:33:47 +01:00
/** Average hook.
*
2022-03-15 09:18:01 -04:00
* @author Steffen Vogel <svogel2@eonerc.rwth-aachen.de>
2022-03-15 09:28:57 -04:00
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
2019-03-26 15:33:47 +01:00
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
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-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
2019-03-26 15:33:47 +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
}
2021-02-16 14:15:14 +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 */