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/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstring>
|
2018-06-04 14:22:38 +02:00
|
|
|
|
2018-05-09 09:24:15 +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
|
|
|
|
2018-05-26 01:23:57 +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)
|
2018-05-26 02:23:00 +02:00
|
|
|
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)
|
2018-05-09 09:24:15 +02:00
|
|
|
json_sequence = json_integer(smp->sequence);
|
2018-04-09 08:28:21 +02:00
|
|
|
|
2018-05-09 09:24:15 +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++) {
|
2020-07-06 15:07:05 +02:00
|
|
|
struct signal *sig = (struct signal *) vlist_at_safe(smp->signals, i);
|
2018-08-20 18:30:24 +02:00
|
|
|
if (!sig)
|
|
|
|
return -1;
|
2018-05-25 12:56:29 +02:00
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
if (sig->name)
|
2018-05-09 09:24:15 +02:00
|
|
|
json_name = json_string(sig->name);
|
|
|
|
else {
|
|
|
|
char name[32];
|
2020-09-11 14:57:05 +02:00
|
|
|
snprintf(name, 32, "signal%u", i);
|
2018-04-09 08:28:21 +02:00
|
|
|
|
2018-05-09 09:24:15 +02:00
|
|
|
json_name = json_string(name);
|
2018-04-09 08:28:21 +02:00
|
|
|
}
|
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
if (sig->unit)
|
|
|
|
json_unit = json_string(sig->unit);
|
|
|
|
else
|
2019-06-23 16:58:28 +02:00
|
|
|
json_unit = nullptr;
|
2018-08-20 18:30:24 +02:00
|
|
|
|
2018-05-23 16:41:06 +02:00
|
|
|
json_value = json_pack_ex(&err, 0, "{ s: o, s: f }",
|
2018-05-09 09:24:15 +02:00
|
|
|
"name", json_name,
|
2018-05-23 16:41:06 +02:00
|
|
|
"value", smp->data[i].f
|
2018-05-09 09:24:15 +02:00
|
|
|
);
|
|
|
|
if (!json_value)
|
|
|
|
continue;
|
|
|
|
|
2018-05-23 16:41:06 +02:00
|
|
|
if (json_unit)
|
2021-05-10 00:12:30 +02:00
|
|
|
json_object_set_new(json_value, "unit", json_unit);
|
2018-05-23 16:41:06 +02:00
|
|
|
if (json_created)
|
2021-05-10 00:12:30 +02:00
|
|
|
json_object_set_new(json_value, "created", json_created);
|
2018-05-23 16:41:06 +02:00
|
|
|
if (json_sequence)
|
2021-05-10 00:12:30 +02:00
|
|
|
json_object_set_new(json_value, "sequence", json_sequence);
|
2018-05-23 16:41:06 +02:00
|
|
|
|
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 }",
|
2018-05-09 09:24:15 +02:00
|
|
|
"measurements", json_data
|
|
|
|
);
|
2021-05-10 00:12:30 +02:00
|
|
|
if (json_root == nullptr)
|
2018-05-09 09:24:15 +02:00
|
|
|
return -1;
|
2018-08-20 18:30:24 +02:00
|
|
|
#if 0
|
2018-05-26 01:23:57 +02:00
|
|
|
#ifdef JSON_RESERVE_INTEGER_TARGET
|
2018-08-09 08:39:27 +02:00
|
|
|
if (io->out.node) {
|
2018-05-26 01:23:57 +02:00
|
|
|
char *endptr;
|
2018-08-09 08:39:27 +02:00
|
|
|
char *id_str = strrchr(io->out.node->name, '_');
|
2018-05-26 01:23:57 +02:00
|
|
|
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));
|
2018-05-26 01:23:57 +02:00
|
|
|
}
|
|
|
|
#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));
|
2018-08-20 18:30:24 +02:00
|
|
|
#endif
|
2018-05-26 01:23:57 +02:00
|
|
|
#endif
|
2018-05-23 16:41:06 +02:00
|
|
|
|
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
|
|
|
{
|
2018-05-09 09:24:15 +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;
|
|
|
|
|
2018-05-26 01:23:57 +02:00
|
|
|
ret = json_unpack_ex(json_smp, &err, 0, "{ s?: o, s?: o, s?: o, s?: o }",
|
|
|
|
"origin", &json_origin,
|
|
|
|
"target", &json_target,
|
2018-05-09 09:24:15 +02:00
|
|
|
"measurements", &json_data,
|
|
|
|
"setpoints", &json_data
|
|
|
|
);
|
2018-04-09 08:28:21 +02:00
|
|
|
if (ret)
|
2018-05-09 09:24:15 +02:00
|
|
|
return -1;
|
2018-04-09 08:28:21 +02:00
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
#if 0
|
2018-05-26 01:23:57 +02:00
|
|
|
#ifdef JSON_RESERVE_INTEGER_TARGET
|
2018-08-09 08:39:27 +02:00
|
|
|
if (json_target && io->in.node) {
|
2018-05-26 01:23:57 +02:00
|
|
|
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, '_');
|
2018-05-26 01:23:57 +02:00
|
|
|
if (!id_str)
|
2018-05-24 09:07:18 +02:00
|
|
|
return -1;
|
2018-05-26 01:23:57 +02:00
|
|
|
|
|
|
|
int id = strtoul(id_str+1, &endptr, 10);
|
|
|
|
if (endptr[0] != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (id != json_integer_value(json_target))
|
2018-05-26 02:53:20 +02:00
|
|
|
return 0;
|
2018-05-26 01:23:57 +02:00
|
|
|
}
|
|
|
|
#else
|
2018-08-09 08:39:27 +02:00
|
|
|
if (json_target && io->in.node) {
|
2018-05-26 01:23:57 +02:00
|
|
|
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))
|
2018-05-26 02:53:20 +02:00
|
|
|
return 0;
|
2018-05-24 09:07:18 +02:00
|
|
|
}
|
2018-05-26 01:23:57 +02:00
|
|
|
#endif
|
2018-08-20 18:30:24 +02:00
|
|
|
#endif
|
2018-05-09 09:24:15 +02:00
|
|
|
if (!json_data || !json_is_array(json_data))
|
2018-04-09 08:28:21 +02:00
|
|
|
return -1;
|
|
|
|
|
2018-05-09 09:24:15 +02:00
|
|
|
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;
|
2018-05-09 09:24:15 +02:00
|
|
|
double value;
|
|
|
|
|
2018-05-26 02:23:00 +02:00
|
|
|
ret = json_unpack_ex(json_value, &err, 0, "{ s: s, s?: s, s: F, s?: F }",
|
2018-05-09 09:24:15 +02:00
|
|
|
"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
|
|
|
|
2018-05-09 09:24:15 +02:00
|
|
|
if (created > 0) {
|
2018-05-26 02:23:00 +02:00
|
|
|
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-09 09:24:15 +02:00
|
|
|
}
|
|
|
|
|
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;
|