2020-08-24 20:57:44 +02:00
|
|
|
/** Limit hook.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
|
|
* @copyright 2014-2020, 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 <bitset>
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <villas/hook.hpp>
|
|
|
|
#include <villas/node/exceptions.hpp>
|
|
|
|
#include <villas/signal.h>
|
|
|
|
#include <villas/sample.h>
|
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
|
2021-07-06 16:28:10 +02:00
|
|
|
class LimitValueHook : public MultiSignalHook {
|
2020-08-24 20:57:44 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
unsigned offset;
|
|
|
|
|
|
|
|
float min, max;
|
|
|
|
|
|
|
|
public:
|
2020-08-25 21:00:52 +02:00
|
|
|
LimitValueHook(struct vpath *p, struct vnode *n, int fl, int prio, bool en = true) :
|
2021-07-06 16:28:10 +02:00
|
|
|
MultiSignalHook(p, n, fl, prio, en),
|
2020-09-11 14:57:05 +02:00
|
|
|
offset(0),
|
2021-07-06 16:28:10 +02:00
|
|
|
min(0),
|
|
|
|
max(0)
|
|
|
|
{ }
|
2020-08-24 20:57:44 +02:00
|
|
|
|
|
|
|
virtual void prepare()
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct signal *avg_sig;
|
|
|
|
|
|
|
|
assert(state == State::CHECKED);
|
|
|
|
|
2021-07-06 16:28:10 +02:00
|
|
|
MultiSignalHook::prepare();
|
2020-08-24 20:57:44 +02:00
|
|
|
|
|
|
|
/* Add averaged signal */
|
|
|
|
avg_sig = signal_create("average", nullptr, SignalType::FLOAT);
|
|
|
|
if (!avg_sig)
|
|
|
|
throw RuntimeError("Failed to create new signal");
|
|
|
|
|
|
|
|
ret = vlist_insert(&signals, offset, avg_sig);
|
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to intialize list");
|
|
|
|
|
|
|
|
state = State::PREPARED;
|
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
virtual void parse(json_t *json)
|
2020-08-24 20:57:44 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
json_error_t err;
|
|
|
|
|
|
|
|
assert(state != State::STARTED);
|
|
|
|
|
2021-07-06 16:28:10 +02:00
|
|
|
MultiSignalHook::parse(json);
|
2020-08-24 20:57:44 +02:00
|
|
|
|
2021-07-06 16:28:10 +02:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s: f, s: f }",
|
2020-08-24 20:57:44 +02:00
|
|
|
"min", &min,
|
2021-07-06 16:28:10 +02:00
|
|
|
"min", &max
|
2020-08-24 20:57:44 +02:00
|
|
|
);
|
|
|
|
if (ret)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json, err, "node-config-hook-average");
|
2020-08-24 20:57:44 +02:00
|
|
|
|
|
|
|
state = State::PARSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Hook::Reason process(sample *smp)
|
|
|
|
{
|
|
|
|
assert(state == State::STARTED);
|
|
|
|
|
2021-07-06 16:28:10 +02:00
|
|
|
for (auto index : signalIndices) {
|
|
|
|
switch (sample_format(smp, index)) {
|
2020-08-24 20:57:44 +02:00
|
|
|
case SignalType::INTEGER:
|
2021-07-06 16:28:10 +02:00
|
|
|
if (smp->data[index].i > max)
|
|
|
|
smp->data[index].i = max;
|
2020-08-24 20:57:44 +02:00
|
|
|
|
2021-07-06 16:28:10 +02:00
|
|
|
if (smp->data[index].i < min)
|
|
|
|
smp->data[index].i = min;
|
2020-08-24 20:57:44 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SignalType::FLOAT:
|
2021-07-06 16:28:10 +02:00
|
|
|
if (smp->data[index].f > max)
|
|
|
|
smp->data[index].f = max;
|
2020-08-24 20:57:44 +02:00
|
|
|
|
2021-07-06 16:28:10 +02:00
|
|
|
if (smp->data[index].f < min)
|
|
|
|
smp->data[index].f = min;
|
2020-08-24 20:57:44 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SignalType::INVALID:
|
|
|
|
case SignalType::COMPLEX:
|
|
|
|
case SignalType::BOOLEAN:
|
|
|
|
return Hook::Reason::ERROR; /* not supported */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Reason::OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Register hook */
|
|
|
|
static char n[] = "average";
|
|
|
|
static char d[] = "Calculate average over some signals";
|
|
|
|
static HookPlugin<LimitValueHook, n , d, (int) Hook::Flags::PATH | (int) Hook::Flags::NODE_READ | (int) Hook::Flags::NODE_WRITE> p;
|
|
|
|
|
|
|
|
} /* namespace node */
|
|
|
|
} /* namespace villas */
|
|
|
|
|
|
|
|
/** @} */
|