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

152 lines
3.4 KiB
C++
Raw Permalink Normal View History

2019-03-26 15:33:47 +01:00
/** Skip first hook.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2020-01-20 17:17:00 +01:00
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
2019-03-26 15:33:47 +01: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.
*
* 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 <villas/hook.hpp>
#include <villas/timing.h>
#include <villas/sample.h>
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
}
2021-06-29 10:45:12 -04: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";
2021-06-18 21:00:10 -04:00
static HookPlugin<SkipFirstHook, n, d, (int) Hook::Flags::READ_ONLY | (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 */
/** @} */