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

hooks: add new builtin "fix" hook which adds missing fields to samples

This commit is contained in:
Steffen Vogel 2018-08-17 10:51:23 +02:00
parent 5f729883cb
commit 77850451b4
3 changed files with 76 additions and 18 deletions

View file

@ -32,6 +32,7 @@ set(HOOK_SRC
ts.c
limit_rate.c
scale.c
fix.c
)
if(WITH_IO)

75
lib/hooks/fix.c Normal file
View file

@ -0,0 +1,75 @@
/** Drop hook.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, 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 <http://www.gnu.org/licenses/>.
*********************************************************************************/
/** @addtogroup hooks Hook functions
* @{
*/
#include <inttypes.h>
#include <villas/hook.h>
#include <villas/plugin.h>
#include <villas/node.h>
#include <villas/sample.h>
#include <villas/timing.h>
static int fix_read(struct hook *h, struct sample *smps[], unsigned *cnt)
{
struct timespec now = time_now();
for (int i = 0; i < *cnt; i++) {
struct sample *smp = smps[i];
if (!(smp->flags & SAMPLE_HAS_SEQUENCE) && h->node) {
smp->sequence = h->node->sequence++;
smp->flags |= SAMPLE_HAS_SEQUENCE;
}
if (!(smp->flags & SAMPLE_HAS_TS_RECEIVED)) {
smp->ts.received = now;
smp->flags |= SAMPLE_HAS_TS_RECEIVED;
}
if (!(smp->flags & SAMPLE_HAS_TS_ORIGIN)) {
smp->ts.origin = smp->ts.received;
smp->flags |= SAMPLE_HAS_TS_ORIGIN;
}
}
return 0;
}
static struct plugin p = {
.name = "fix",
.description = "Fix received data by adding missing fields",
.type = PLUGIN_TYPE_HOOK,
.hook = {
.flags = HOOK_BUILTIN | HOOK_NODE,
.priority = 1,
.read = fix_read
}
};
REGISTER_PLUGIN(&p)
/** @} */

View file

@ -409,24 +409,6 @@ int node_read(struct node *n, struct sample *smps[], unsigned cnt, unsigned *rel
return nread;
}
/* Add missing fields */
for (int i = 0; i < nread; i++) {
if (!(smps[i]->flags & SAMPLE_HAS_SEQUENCE))
smps[i]->sequence = n->sequence++;
if (!(smps[i]->flags & SAMPLE_HAS_ORIGIN) ||
!(smps[i]->flags & SAMPLE_HAS_RECEIVED)) {
struct timespec now = time_now();
if (!(smps[i]->flags & SAMPLE_HAS_RECEIVED))
smps[i]->ts.received = now;
if (!(smps[i]->flags & SAMPLE_HAS_ORIGIN))
smps[i]->ts.origin = now;
}
}
#ifdef WITH_HOOKS
/* Run read hooks */
int rread = hook_read_list(&n->in.hooks, smps, nread);