mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
126 lines
2.8 KiB
C
126 lines
2.8 KiB
C
/** Override a value of the sample with a header, timestamp or statistic value.
|
|
*
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
* @copyright 2017, 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 <villas/plugin.h>
|
|
#include <villas/mapping.h>
|
|
#include <villas/list.h>
|
|
#include <villas/utils.h>
|
|
#include <villas/path.h>
|
|
#include <villas/sample.h>
|
|
#include <villas/node.h>
|
|
|
|
struct map {
|
|
struct list mapping;
|
|
|
|
struct stats *stats;
|
|
};
|
|
|
|
static int map_init(struct hook *h)
|
|
{
|
|
struct map *m = (struct map *) h->_vd;
|
|
|
|
m->stats = NULL; /** @todo */
|
|
|
|
return list_init(&m->mapping);
|
|
}
|
|
|
|
static int map_destroy(struct hook *h)
|
|
{
|
|
struct map *m = (struct map *) h->_vd;
|
|
|
|
return list_destroy(&m->mapping, NULL, true);
|
|
}
|
|
|
|
static int map_parse(struct hook *h, json_t *cfg)
|
|
{
|
|
int ret;
|
|
struct map *m = (struct map *) h->_vd;
|
|
json_error_t err;
|
|
json_t *json_mapping;
|
|
|
|
ret = json_unpack_ex(cfg, &err, 0, "{ s: o }",
|
|
"map", &json_mapping
|
|
);
|
|
if (ret)
|
|
jerror(&err, "Failed to parse configuration of hook '%s'", plugin_name(h->_vt));
|
|
|
|
ret = mapping_parse_list(&m->mapping, json_mapping, NULL);
|
|
if (ret)
|
|
return ret;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int map_process(struct hook *h, struct sample *smps[], unsigned *cnt)
|
|
{
|
|
int ret;
|
|
struct map *m = (struct map *) h->_vd;
|
|
struct sample *tmp[*cnt];
|
|
struct pool *p;
|
|
|
|
if (*cnt <= 0)
|
|
return 0;
|
|
|
|
p = sample_pool(smps[0]);
|
|
if (!p)
|
|
return -1;
|
|
|
|
ret = sample_alloc_many(p, tmp, *cnt);
|
|
if (ret != *cnt)
|
|
return ret;
|
|
|
|
for (int i = 0; i < *cnt; i++) {
|
|
tmp[i]->format = 0;
|
|
tmp[i]->length = 0;
|
|
|
|
mapping_remap(&m->mapping, tmp[i], smps[i], m->stats);
|
|
|
|
SWAP(smps[i], tmp[i]);
|
|
}
|
|
|
|
sample_free_many(tmp, *cnt);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static struct plugin p = {
|
|
.name = "map",
|
|
.description = "Remap values and / or add header, timestamp values to the sample",
|
|
.type = PLUGIN_TYPE_HOOK,
|
|
.hook = {
|
|
.flags = HOOK_PATH,
|
|
.priority = 99,
|
|
.init = map_init,
|
|
.destroy= map_destroy,
|
|
.parse = map_parse,
|
|
.process= map_process,
|
|
.size = sizeof(struct map)
|
|
}
|
|
};
|
|
|
|
REGISTER_PLUGIN(&p);
|
|
|
|
/** @} */
|