/** 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) /** @} */