From 6ff4c838b4bb4c9ea7532ff49f1265d2f2447a84 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Mon, 21 Jan 2019 22:04:04 +0100 Subject: [PATCH] hook: added new averaging hook --- lib/hooks/CMakeLists.txt | 1 + lib/hooks/average.c | 108 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 lib/hooks/average.c diff --git a/lib/hooks/CMakeLists.txt b/lib/hooks/CMakeLists.txt index eaea3f910..c15b56d02 100644 --- a/lib/hooks/CMakeLists.txt +++ b/lib/hooks/CMakeLists.txt @@ -34,6 +34,7 @@ set(HOOK_SRC scale.c fix.c cast.c + average.c ) if(WITH_IO) diff --git a/lib/hooks/average.c b/lib/hooks/average.c new file mode 100644 index 000000000..f50f8a982 --- /dev/null +++ b/lib/hooks/average.c @@ -0,0 +1,108 @@ +/** Average hook. + * + * @author Steffen Vogel + * @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC + * @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 . + *********************************************************************************/ + +/** @addtogroup hooks Hook functions + * @{ + */ + +#include + +#include +#include +#include + +struct average { + int mask; + int offset; +}; + +static int average_parse(struct hook *h, json_t *cfg) +{ + struct average *p = (struct average *) h->_vd; + + int ret; + json_error_t err; + + ret = json_unpack_ex(cfg, &err, 0, "{ s: i, s: i }", + "offset", &p->offset, + "mask", &p->mask + ); + if (ret) + jerror(&err, "Failed to parse configuration of hook '%s'", hook_type_name(h->_vt)); + + return 0; +} + +static int average_process(struct hook *h, struct sample *smps[], unsigned *cnt) +{ + struct average *p = (struct average *) h->_vd; + + for (int i = 0; i < *cnt; i++) { + struct sample *smp = smps[i]; + double sum = 0; + int n = 0; + + for (int k = 0; k < smp->length; k++) { + if (!(p->mask & (1 << k))) + continue; + + switch (sample_format(smps[i], k)) { + case SIGNAL_TYPE_INTEGER: + sum += smp->data[k].i; + break; + + case SIGNAL_TYPE_FLOAT: + sum += smp->data[k].f; + break; + + case SIGNAL_TYPE_AUTO: + case SIGNAL_TYPE_INVALID: + case SIGNAL_TYPE_COMPLEX: + case SIGNAL_TYPE_BOOLEAN: + return -1; /* not supported */ + } + + n++; + } + + smp->data[p->offset].f = sum / n; + } + + return 0; +} + +static struct plugin p = { + .name = "average", + .description = "Calculate average over some signals", + .type = PLUGIN_TYPE_HOOK, + .hook = { + .flags = HOOK_PATH | HOOK_NODE_READ | HOOK_NODE_WRITE, + .priority = 99, + .parse = average_parse, + .process = average_process, + .size = sizeof(struct average) + } +}; + +REGISTER_PLUGIN(&p) + +/** @} */