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/skip_first.c

165 lines
4 KiB
C
Raw Permalink Normal View History

/** Skip first hook.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
2017-04-27 12:56:43 +02: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.
*
2017-04-27 12:56:43 +02:00
* 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.
*
2017-04-27 12:56:43 +02:00
* 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
* @{
*/
2017-12-09 02:19:28 +08:00
#include <villas/hook.h>
#include <villas/plugin.h>
#include <villas/timing.h>
#include <villas/sample.h>
struct skip_first {
enum {
HOOK_SKIP_FIRST_STATE_STARTED, /**< Path just started. First sample not received yet. */
HOOK_SKIP_FIRST_STATE_SKIPPING, /**< First sample received. Skipping samples now. */
HOOK_SKIP_FIRST_STATE_NORMAL, /**< All samples skipped. Normal operation. */
} state;
enum {
HOOK_SKIP_MODE_SECONDS,
HOOK_SKIP_MODE_SAMPLES
} mode;
union {
struct {
struct timespec until;
struct timespec wait; /**< Absolute point in time from where we accept samples. */
} seconds;
struct {
int until;
int wait;
} samples;
};
};
static int skip_first_parse(struct hook *h, json_t *cfg)
{
2017-10-18 15:39:53 +02:00
struct skip_first *p = (struct skip_first *) h->_vd;
2017-03-27 12:26:11 +02:00
double seconds;
int ret;
json_error_t err;
ret = json_unpack_ex(cfg, &err, 0, "{ s: F }", "seconds", &seconds);
if (!ret) {
2017-03-27 12:26:11 +02:00
p->seconds.wait = time_from_double(seconds);
p->mode = HOOK_SKIP_MODE_SECONDS;
return 0;
2017-03-27 12:26:11 +02:00
}
ret = json_unpack_ex(cfg, &err, 0, "{ s: i }", "samples", &p->samples.wait);
if (!ret) {
2017-03-27 12:26:11 +02:00
p->mode = HOOK_SKIP_MODE_SAMPLES;
return 0;
2017-03-27 12:26:11 +02:00
}
jerror(&err, "Failed to parse configuration of hook '%s'", plugin_name(h->_vt));
return -1;
2017-03-27 12:26:11 +02:00
}
static int skip_first_restart(struct hook *h)
{
2017-10-18 15:39:53 +02:00
struct skip_first *p = (struct skip_first *) h->_vd;
2017-03-27 12:26:11 +02:00
p->state = HOOK_SKIP_FIRST_STATE_STARTED;
2017-03-27 12:26:11 +02:00
return 0;
}
static int skip_first_read(struct hook *h, struct sample *smps[], unsigned *cnt)
2017-03-27 12:26:11 +02:00
{
2017-10-18 15:39:53 +02:00
struct skip_first *p = (struct skip_first *) h->_vd;
2017-03-27 12:26:11 +02:00
/* Remember sequence no or timestamp of first sample. */
2017-03-27 12:26:11 +02:00
if (p->state == HOOK_SKIP_FIRST_STATE_STARTED) {
switch (p->mode) {
case HOOK_SKIP_MODE_SAMPLES:
p->samples.until = smps[0]->sequence + p->samples.wait;
break;
2017-03-27 12:26:11 +02:00
case HOOK_SKIP_MODE_SECONDS:
p->seconds.until = time_add(&smps[0]->ts.origin, &p->seconds.wait);
2017-03-27 12:26:11 +02:00
break;
}
2017-03-27 12:26:11 +02:00
p->state = HOOK_SKIP_FIRST_STATE_SKIPPING;
}
int i, ok;
for (i = 0, ok = 0; i < *cnt; i++) {
bool skip;
switch (p->mode) {
case HOOK_SKIP_MODE_SAMPLES:
skip = p->samples.until > smps[i]->sequence;
2017-03-27 12:26:11 +02:00
break;
2017-03-27 12:26:11 +02:00
case HOOK_SKIP_MODE_SECONDS:
skip = time_delta(&p->seconds.until, &smps[i]->ts.origin) < 0;
2017-03-27 12:26:11 +02:00
break;
default:
skip = false;
break;
2017-03-27 12:26:11 +02:00
}
2017-03-27 12:26:11 +02:00
if (!skip) {
struct sample *tmp;
tmp = smps[i];
smps[i] = smps[ok];
smps[ok++] = tmp;
}
/* To discard the first X samples in 'smps[]' we must
* shift them to the end of the 'smps[]' array.
* In case the hook returns a number 'ok' which is smaller than 'cnt',
* only the first 'ok' samples in 'smps[]' are accepted and further processed.
*/
}
2017-03-27 12:26:11 +02:00
*cnt = ok;
return 0;
}
static struct plugin p = {
.name = "skip_first",
.description = "Skip the first samples",
.type = PLUGIN_TYPE_HOOK,
.hook = {
.flags = HOOK_NODE | HOOK_PATH,
.priority = 99,
2017-03-27 12:26:11 +02:00
.parse = skip_first_parse,
.start = skip_first_restart,
.restart = skip_first_restart,
.read = skip_first_read,
.size = sizeof(struct skip_first)
}
};
REGISTER_PLUGIN(&p)
2017-07-24 19:33:35 +02:00
/** @} */