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/formats/json_reserve.cpp

224 lines
5.2 KiB
C++
Raw Permalink Normal View History

2018-04-09 08:28:21 +02:00
/** JSON serializtion for RESERVE project.
*
* @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
2018-04-09 08:28:21 +02: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/>.
*********************************************************************************/
#include <cstring>
2018-06-04 14:22:38 +02:00
#include <villas/timing.h>
2021-05-10 00:12:30 +02:00
#include <villas/formats/json_reserve.hpp>
using namespace villas::node;
2018-04-09 08:28:21 +02:00
#define JSON_RESERVE_INTEGER_TARGET 1
2021-05-10 00:12:30 +02:00
int JsonReserveFormat::packSample(json_t **json_smp, const struct sample *smp)
2018-04-09 08:28:21 +02:00
{
json_error_t err;
2021-05-10 00:12:30 +02:00
json_t *json_root;
json_t *json_data;
json_t *json_name;
json_t *json_unit;
json_t *json_value;
2019-06-23 16:58:28 +02:00
json_t *json_created = nullptr;
json_t *json_sequence = nullptr;
2018-04-09 08:28:21 +02:00
2019-06-23 16:13:23 +02:00
if (smp->flags & (int) SampleFlags::HAS_TS_ORIGIN)
json_created = json_integer(time_to_double(&smp->ts.origin) * 1e3);
2018-04-09 08:28:21 +02:00
2019-06-23 16:13:23 +02:00
if (smp->flags & (int) SampleFlags::HAS_SEQUENCE)
json_sequence = json_integer(smp->sequence);
2018-04-09 08:28:21 +02:00
json_data = json_array();
2018-04-09 08:28:21 +02:00
2019-04-07 15:13:40 +02:00
for (unsigned i = 0; i < smp->length; i++) {
struct signal *sig = (struct signal *) vlist_at_safe(smp->signals, i);
if (!sig)
return -1;
2018-05-25 12:56:29 +02:00
if (sig->name)
json_name = json_string(sig->name);
else {
char name[32];
snprintf(name, 32, "signal%u", i);
2018-04-09 08:28:21 +02:00
json_name = json_string(name);
2018-04-09 08:28:21 +02:00
}
if (sig->unit)
json_unit = json_string(sig->unit);
else
2019-06-23 16:58:28 +02:00
json_unit = nullptr;
json_value = json_pack_ex(&err, 0, "{ s: o, s: f }",
"name", json_name,
"value", smp->data[i].f
);
if (!json_value)
continue;
if (json_unit)
2021-05-10 00:12:30 +02:00
json_object_set_new(json_value, "unit", json_unit);
if (json_created)
2021-05-10 00:12:30 +02:00
json_object_set_new(json_value, "created", json_created);
if (json_sequence)
2021-05-10 00:12:30 +02:00
json_object_set_new(json_value, "sequence", json_sequence);
2021-05-10 00:12:30 +02:00
json_array_append_new(json_data, json_value);
2018-04-09 08:28:21 +02:00
}
2021-05-10 00:12:30 +02:00
json_root = json_pack_ex(&err, 0, "{ s: o }",
"measurements", json_data
);
2021-05-10 00:12:30 +02:00
if (json_root == nullptr)
return -1;
#if 0
#ifdef JSON_RESERVE_INTEGER_TARGET
2018-08-09 08:39:27 +02:00
if (io->out.node) {
char *endptr;
2018-08-09 08:39:27 +02:00
char *id_str = strrchr(io->out.node->name, '_');
if (!id_str)
return -1;
int id = strtoul(id_str+1, &endptr, 10);
if (endptr[0] != 0)
return -1;
2021-05-10 00:12:30 +02:00
json_object_set_new(json_root, "target", json_integer(id));
}
#else
2018-08-09 08:39:27 +02:00
if (io->out.node)
2021-05-10 00:12:30 +02:00
json_object_set_new(json_root, "target", json_string(io->out.node->name));
#endif
#endif
2021-05-10 00:12:30 +02:00
*json_smp = json_root;
2018-04-09 08:28:21 +02:00
return 0;
}
2021-05-10 00:12:30 +02:00
int JsonReserveFormat::unpackSample(json_t *json_smp, struct sample *smp)
2018-04-09 08:28:21 +02:00
{
int ret, idx;
double created = -1;
json_error_t err;
2019-06-23 16:58:28 +02:00
json_t *json_value, *json_data = nullptr;
json_t *json_origin = nullptr, *json_target = nullptr;
2018-04-09 08:28:21 +02:00
size_t i;
ret = json_unpack_ex(json_smp, &err, 0, "{ s?: o, s?: o, s?: o, s?: o }",
"origin", &json_origin,
"target", &json_target,
"measurements", &json_data,
"setpoints", &json_data
);
2018-04-09 08:28:21 +02:00
if (ret)
return -1;
2018-04-09 08:28:21 +02:00
#if 0
#ifdef JSON_RESERVE_INTEGER_TARGET
2018-08-09 08:39:27 +02:00
if (json_target && io->in.node) {
if (!json_is_integer(json_target))
return -1;
char *endptr;
2018-08-09 08:39:27 +02:00
char *id_str = strrchr(io->in.node->name, '_');
if (!id_str)
2018-05-24 09:07:18 +02:00
return -1;
int id = strtoul(id_str+1, &endptr, 10);
if (endptr[0] != 0)
return -1;
if (id != json_integer_value(json_target))
return 0;
}
#else
2018-08-09 08:39:27 +02:00
if (json_target && io->in.node) {
const char *target = json_string_value(json_target);
if (!target)
return -1;
2018-08-09 08:39:27 +02:00
if (strcmp(target, io->in.node->name))
return 0;
2018-05-24 09:07:18 +02:00
}
#endif
#endif
if (!json_data || !json_is_array(json_data))
2018-04-09 08:28:21 +02:00
return -1;
smp->flags = 0;
2018-04-09 08:28:21 +02:00
smp->length = 0;
json_array_foreach(json_data, i, json_value) {
2019-06-23 16:58:28 +02:00
const char *name, *unit = nullptr;
double value;
ret = json_unpack_ex(json_value, &err, 0, "{ s: s, s?: s, s: F, s?: F }",
"name", &name,
"unit", &unit,
"value", &value,
"created", &created
);
if (ret)
return -1;
2018-04-09 08:28:21 +02:00
2018-05-25 12:56:29 +02:00
struct signal *sig;
2021-05-10 00:12:30 +02:00
sig = vlist_lookup_name<struct signal>(signals, name);
2018-05-25 12:56:29 +02:00
if (sig) {
if (!sig->enabled)
continue;
2021-05-10 00:12:30 +02:00
idx = vlist_index(signals, sig);
2018-05-25 12:56:29 +02:00
}
else {
2018-05-12 18:02:37 +02:00
ret = sscanf(name, "signal_%d", &idx);
if (ret != 1)
continue;
}
2018-04-09 08:28:21 +02:00
2019-04-07 15:13:40 +02:00
if (idx < 0)
return -1;
if (idx < (int) smp->capacity) {
2018-05-12 18:02:37 +02:00
smp->data[idx].f = value;
2018-04-09 08:28:21 +02:00
2019-04-07 15:13:40 +02:00
if (idx >= (int) smp->length)
2018-05-24 09:07:18 +02:00
smp->length = idx + 1;
2018-05-12 18:02:37 +02:00
}
2018-04-09 08:28:21 +02:00
}
if (smp->length > 0)
2019-06-23 16:13:23 +02:00
smp->flags |= (int) SampleFlags::HAS_DATA;
2018-04-09 08:28:21 +02:00
if (created > 0) {
smp->ts.origin = time_from_double(created * 1e-3);
2019-06-23 16:13:23 +02:00
smp->flags |= (int) SampleFlags::HAS_TS_ORIGIN;
}
2018-05-24 09:07:18 +02:00
return smp->length > 0 ? 1 : 0;
2018-04-09 08:28:21 +02:00
}
2019-06-23 16:13:23 +02:00
2021-05-10 00:12:30 +02:00
static char n[] = "json.reserve";
static char d[] = "RESERVE JSON format";
static FormatPlugin<JsonReserveFormat, n, d, (int) SampleFlags::HAS_TS_ORIGIN | (int) SampleFlags::HAS_SEQUENCE | (int) SampleFlags::HAS_DATA> p;