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

146 lines
3.3 KiB
C++
Raw Permalink Normal View History

2019-03-26 15:33:47 +01:00
/** Scale 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 <cstring>
2019-03-26 15:33:47 +01:00
#include <villas/hook.hpp>
#include <villas/sample.h>
namespace villas {
namespace node {
class ScaleHook : public Hook {
protected:
char *signal_name;
unsigned signal_index;
double scale;
double offset;
public:
2020-08-25 21:00:52 +02:00
ScaleHook(struct vpath *p, struct vnode *n, int fl, int prio, bool en = true) :
2019-03-26 15:33:47 +01:00
Hook(p, n, fl, prio, en),
signal_name(nullptr),
signal_index(0),
2019-03-26 15:33:47 +01:00
scale(1),
offset(0)
{ }
virtual void prepare()
{
2019-06-23 16:13:23 +02:00
assert(state != State::STARTED);
2019-03-26 15:33:47 +01:00
if (signal_name) {
2020-08-25 20:22:19 +02:00
signal_index = vlist_lookup_index<struct signal>(&signals, signal_name);
2019-03-26 15:33:47 +01:00
if (signal_index < 0)
throw RuntimeError("Failed to find signal: {}", signal_name);
}
2019-06-23 16:13:23 +02:00
state = State::PREPARED;
2019-03-26 15:33:47 +01:00
}
virtual ~ScaleHook()
{
if (signal_name)
free(signal_name);
}
2021-02-16 14:15:14 +01:00
virtual void parse(json_t *json)
2019-03-26 15:33:47 +01:00
{
int ret;
json_t *json_signal;
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, s?: F, s: o }",
2019-03-26 15:33:47 +01:00
"scale", &scale,
"offset", &offset,
"signal", &json_signal
);
if (ret)
2021-02-16 14:15:14 +01:00
throw ConfigError(json, err, "node-config-hook-scale");
2019-03-26 15:33:47 +01:00
switch (json_typeof(json_signal)) {
case JSON_STRING:
signal_name = strdup(json_string_value(json_signal));
break;
case JSON_INTEGER:
2019-04-07 13:23:43 +02:00
signal_name = nullptr;
2019-03-26 15:33:47 +01:00
signal_index = json_integer_value(json_signal);
break;
default:
throw ConfigError(json_signal, "node-config-hook-scale-signal", "Invalid value for setting 'signal'");
}
2019-06-23 16:13:23 +02:00
state = State::PARSED;
2019-03-26 15:33:47 +01:00
}
2021-06-18 14:31:29 -04:00
virtual Hook::Reason process(struct sample *smp)
2019-03-26 15:33:47 +01:00
{
2021-06-18 14:31:29 -04:00
unsigned k = signal_index;
2019-03-26 15:33:47 +01:00
2021-06-18 14:31:29 -04:00
assert(k < smp->length);
2019-06-23 16:13:23 +02:00
assert(state == State::STARTED);
2019-03-26 15:33:47 +01:00
switch (sample_format(smp, k)) {
2019-06-23 16:13:23 +02:00
case SignalType::INTEGER:
2021-06-18 14:31:29 -04:00
smp->data[k].i *= scale;
smp->data[k].i += offset;
2019-03-26 15:33:47 +01:00
break;
2019-06-23 16:13:23 +02:00
case SignalType::FLOAT:
2021-06-18 14:31:29 -04:00
smp->data[k].f *= scale;
smp->data[k].f += offset;
2019-03-26 15:33:47 +01:00
break;
2019-06-23 16:13:23 +02:00
case SignalType::COMPLEX:
2021-06-18 14:31:29 -04:00
smp->data[k].z *= scale;
smp->data[k].z += offset;
2019-03-26 15:33:47 +01:00
break;
default: { }
}
2019-06-23 16:13:23 +02:00
return Reason::OK;
2019-03-26 15:33:47 +01:00
}
};
/* Register hook */
static char n[] = "scale";
static char d[] = "Scale signals by a factor and add offset";
static HookPlugin<ScaleHook, n, d, (int) Hook::Flags::PATH | (int) Hook::Flags::NODE_READ | (int) Hook::Flags::NODE_WRITE> p;
2019-03-26 15:33:47 +01:00
} /* namespace node */
} /* namespace villas */
/** @} */