1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-23 00:00:01 +01:00
VILLASnode/lib/hooks/skip_first.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

125 lines
2.7 KiB
C++
Raw Normal View History

2019-03-26 15:33:47 +01:00
/* Skip first hook.
*
2022-03-15 09:18:01 -04:00
* Author: Steffen Vogel <post@steffenvogel.de>
2022-03-15 09:28:57 -04:00
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
2022-07-04 18:20:03 +02:00
* SPDX-License-Identifier: Apache-2.0
2019-03-26 15:33:47 +01:00
*/
#include <villas/hook.hpp>
#include <villas/sample.hpp>
#include <villas/timing.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 } mode;
2019-03-26 15:33:47 +01:00
union {
struct {
timespec until;
timespec wait; // Absolute point in time from where we accept samples.
} seconds;
2019-03-26 15:33:47 +01:00
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;
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);
2021-02-16 14:15:14 +01:00
Hook::parse(json);
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-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-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-06-23 16:13:23 +02:00
virtual void restart() { skip_state = SkipState::STARTED; }
virtual Hook::Reason process(struct Sample *smp) {
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
}
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;
2019-03-26 15:33:47 +01:00
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