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

131 lines
2.6 KiB
C++
Raw Permalink Normal View History

2019-03-26 15:33:47 +01:00
/** Skip first hook.
*
* @author Steffen Vogel <post@steffenvogel.de>
2022-03-15 09:28:57 -04:00
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
2022-07-04 18:20:03 +02:00
* @license Apache 2.0
2019-03-26 15:33:47 +01:00
*********************************************************************************/
#include <villas/hook.hpp>
#include <villas/timing.hpp>
#include <villas/sample.hpp>
2019-03-26 15:33:47 +01:00
namespace villas {
namespace node {
class SkipFirstHook : public Hook {
protected:
2019-06-23 16:13:23 +02:00
enum class SkipState {
STARTED, /**< Path just started. First sample not received yet. */
SKIPPING, /**< First sample received. Skipping samples now. */
NORMAL /**< All samples skipped. Normal operation. */
2019-03-26 15:33:47 +01:00
} skip_state;
2019-06-23 16:13:23 +02:00
enum class Mode {
SECONDS,
SAMPLES
2019-03-26 15:33:47 +01:00
} mode;
union {
struct {
timespec until;
timespec wait; /**< Absolute point in time from where we accept samples. */
} seconds;
struct {
uint64_t until;
int wait;
} samples;
};
public:
using Hook::Hook;
2021-02-16 14:15:14 +01:00
virtual void parse(json_t *json)
2019-03-26 15:33:47 +01:00
{
double s;
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
2021-02-16 14:15:14 +01:00
Hook::parse(json);
2020-08-28 19:49:36 +02:00
2021-02-16 14:15:14 +01:00
ret = json_unpack_ex(json, &err, 0, "{ s: F }", "seconds", &s);
2019-03-26 15:33:47 +01:00
if (!ret) {
seconds.wait = time_from_double(s);
2019-06-23 16:13:23 +02:00
mode = Mode::SECONDS;
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
return;
}
2021-02-16 14:15:14 +01:00
ret = json_unpack_ex(json, &err, 0, "{ s: i }", "samples", &samples.wait);
2019-03-26 15:33:47 +01:00
if (!ret) {
2019-06-23 16:13:23 +02:00
mode = Mode::SAMPLES;
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
return;
}
2021-02-16 14:15:14 +01:00
throw ConfigError(json, err, "node-config-hook-skip_first");
2019-03-26 15:33:47 +01:00
}
2020-08-17 17:10:35 +02:00
virtual void start()
{
skip_state = SkipState::STARTED;
state = State::STARTED;
}
2019-03-26 15:33:47 +01:00
virtual void restart()
{
2019-06-23 16:13:23 +02:00
skip_state = SkipState::STARTED;
2019-03-26 15:33:47 +01:00
}
virtual Hook::Reason process(struct Sample *smp)
2019-03-26 15:33:47 +01:00
{
2019-06-23 16:13:23 +02:00
assert(state == State::STARTED);
2019-03-26 15:33:47 +01:00
/* Remember sequence no or timestamp of first sample. */
2019-06-23 16:13:23 +02:00
if (skip_state == SkipState::STARTED) {
2019-03-26 15:33:47 +01:00
switch (mode) {
2019-06-23 16:13:23 +02:00
case Mode::SAMPLES:
2019-03-26 15:33:47 +01:00
samples.until = smp->sequence + samples.wait;
break;
2019-06-23 16:13:23 +02:00
case Mode::SECONDS:
2019-03-26 15:33:47 +01:00
seconds.until = time_add(&smp->ts.origin, &seconds.wait);
break;
}
2019-06-23 16:13:23 +02:00
skip_state = SkipState::SKIPPING;
2019-03-26 15:33:47 +01:00
}
switch (mode) {
2019-06-23 16:13:23 +02:00
case Mode::SAMPLES:
2019-03-26 15:33:47 +01:00
if (samples.until > smp->sequence)
2019-06-23 16:13:23 +02:00
return Hook::Reason::SKIP_SAMPLE;
2019-03-26 15:33:47 +01:00
break;
2019-06-23 16:13:23 +02:00
case Mode::SECONDS:
2019-03-26 15:33:47 +01:00
if (time_delta(&seconds.until, &smp->ts.origin) < 0)
2019-06-23 16:13:23 +02:00
return Hook::Reason::SKIP_SAMPLE;
2019-03-26 15:33:47 +01:00
break;
default:
break;
}
2019-06-23 16:13:23 +02:00
return Reason::OK;
2019-03-26 15:33:47 +01:00
}
};
/* Register hook */
static char n[] = "skip_first";
static char d[] = "Skip the first samples";
static HookPlugin<SkipFirstHook, n, d, (int) Hook::Flags::NODE_READ | (int) Hook::Flags::NODE_WRITE | (int) Hook::Flags::PATH> p;
2019-03-26 15:33:47 +01:00
} /* namespace node */
} /* namespace villas */