diff --git a/lib/hooks/Makefile.inc b/lib/hooks/Makefile.inc index 586dd592e..31455293c 100644 --- a/lib/hooks/Makefile.inc +++ b/lib/hooks/Makefile.inc @@ -22,7 +22,7 @@ LIB_SRCS += $(addprefix lib/hooks/, convert.c decimate.c drop.c jitter_calc.c \ map.c restart.c shift_seq.c shift_ts.c \ - skip_first.c stats.c ts.c limit_rate.c) + skip_first.c stats.c ts.c limit_rate.c scale.c) ifeq ($(WITH_IO),1) LIB_SRCS += lib/hooks/print.c diff --git a/lib/hooks/scale.c b/lib/hooks/scale.c new file mode 100644 index 000000000..76c44ec55 --- /dev/null +++ b/lib/hooks/scale.c @@ -0,0 +1,102 @@ +/** Scale hook. + * + * @author Steffen Vogel + * @copyright 2018, 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 . + *********************************************************************************/ + +/** @addtogroup hooks Hook functions + * @{ + */ + +#include + +#include +#include +#include + +struct scale { + double scale; + double offset; +}; + +static int scale_init(struct hook *h) +{ + struct scale *p = (struct scale *) h->_vd; + + p->scale = 1; + p->offset = 0; + + return 0; +} + +static int scale_parse(struct hook *h, json_t *cfg) +{ + struct scale *p = (struct scale *) h->_vd; + + int ret; + json_error_t err; + + ret = json_unpack_ex(cfg, &err, 0, "{ s?: F, s?: F }", + "scale", &p->scale, + "offset", &p->offset + ); + if (ret) + jerror(&err, "Failed to parse configuration of hook '%s'", plugin_name(h->_vt)); + + return 0; +} + +static int scale_process(struct hook *h, struct sample *smps[], unsigned *cnt) +{ + struct scale *p = (struct scale *) h->_vd; + + for (int i = 0; i < *cnt; i++) { + for (int k = 0; k < smps[i]->length; k++) { + + switch (sample_get_data_format(smps[i], k)) { + case SAMPLE_DATA_FORMAT_INT: + smps[i]->data[k].i = smps[i]->data[k].i * p->scale + p->offset; + break; + case SAMPLE_DATA_FORMAT_FLOAT: + smps[i]->data[k].f = smps[i]->data[k].f * p->scale + p->offset; + break; + } + } + } + + return 0; +} + +static struct plugin p = { + .name = "scale", + .description = "Scale all signals by and add offset", + .type = PLUGIN_TYPE_HOOK, + .hook = { + .flags = HOOK_PATH, + .priority = 99, + .init = scale_init, + .parse = scale_parse, + .process= scale_process, + .size = sizeof(struct scale) + } +}; + +REGISTER_PLUGIN(&p) + +/** @} */