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/hooks/average.cpp

134 lines
3.1 KiB
C++
Raw Permalink Normal View History

2019-03-26 15:33:47 +01:00
/** Average hook.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2020-01-20 17:17:00 +01:00
* @copyright 2014-2020, 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/>.
*********************************************************************************/
/** @addtogroup hooks Hook functions
* @{
*/
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.h>
#include <villas/sample.h>
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:
2020-08-25 21:00:52 +02:00
AverageHook(struct vpath *p, struct vnode *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()
{
int ret;
struct signal *avg_sig;
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 */
2019-06-23 16:13:23 +02:00
avg_sig = signal_create("average", nullptr, SignalType::FLOAT);
2019-03-26 15:33:47 +01:00
if (!avg_sig)
throw RuntimeError("Failed to create new signal");
ret = vlist_insert(&signals, offset, avg_sig);
if (ret)
throw RuntimeError("Failed to intialize list");
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
}
2019-06-23 16:13:23 +02:00
virtual Hook::Reason process(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;
2019-03-26 15:33:47 +01:00
sample_data_insert(smp, (union signal_data *) &avg, offset, 1);
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 */
/** @} */