1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/lib/io/json_reserve.c

265 lines
5.4 KiB
C
Raw Normal View History

2018-04-09 08:28:21 +02:00
/** JSON serializtion for RESERVE project.
*
* @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/>.
*********************************************************************************/
#include <villas/plugin.h>
#include <villas/sample.h>
#include <villas/node.h>
#include <villas/signal.h>
2018-04-09 08:28:21 +02:00
#include <villas/compat.h>
#include <villas/timing.h>
2018-04-09 08:28:21 +02:00
#include <villas/io/json.h>
int json_reserve_pack_sample(json_t **j, struct sample *smp, int flags)
{
json_error_t err;
json_t *json_data, *json_name, *json_unit, *json_value;
json_t *json_created = NULL, *json_sequence = NULL;
struct signal *sig;
2018-04-09 08:28:21 +02:00
if (smp->flags & SAMPLE_HAS_ORIGIN)
json_created = json_real(time_to_double(&smp->ts.origin));
2018-04-09 08:28:21 +02:00
if (smp->flags & SAMPLE_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
for (int i = 0; i < smp->length; i++) {
if (smp->destination)
sig = (struct signal *) list_at(&smp->destination->signals, i);
else
sig = NULL;
2018-04-09 08:28:21 +02:00
if (sig) {
json_name = json_string(sig->name);
json_unit = json_string(sig->unit);
}
else {
char name[32];
snprintf(name, 32, "signal_%d", i);
2018-04-09 08:28:21 +02:00
json_name = json_string(name);
json_unit = NULL;
2018-04-09 08:28:21 +02:00
}
json_value = json_pack_ex(&err, 0, "{ s: o, s: o*, s: f, s: o*, s: o* }",
"name", json_name,
"unit", json_unit,
"value", smp->data[i].f,
"created", json_created,
"sequence", json_sequence
);
if (!json_value)
continue;
json_array_append(json_data, json_value);
2018-04-09 08:28:21 +02:00
}
char *origin, *target;
origin = smp->source
? smp->source->name
: NULL;
target = smp->destination
? smp->destination->name
: NULL;
*j = json_pack_ex(&err, 0, "{ s: s*, s: s*, s: o }",
"origin", origin,
"target", target,
"measurements", json_data
);
if (*j == NULL)
return -1;
2018-04-09 08:28:21 +02:00
return 0;
}
int json_reserve_unpack_sample(json_t *json_smp, struct sample *smp, int flags)
{
int ret, idx;
double created = -1;
json_error_t err;
json_t *json_value, *json_data = NULL;
2018-04-09 08:28:21 +02:00
size_t i;
const char *origin;
const char *target;
2018-04-09 08:28:21 +02:00
ret = json_unpack_ex(json_smp, &err, 0, "{ s?: s, s?: s, s?: o, s?: o }",
"origin", &origin,
"target", &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 (!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) {
const char *name, *unit = NULL;
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
idx = signal_get_offset(name, smp->source);
if (idx < 0)
return -1;
2018-04-09 08:28:21 +02:00
if (idx >= smp->capacity)
continue;
2018-04-09 08:28:21 +02:00
if (idx >= smp->length)
smp->length = idx;
2018-04-09 08:28:21 +02:00
smp->data[idx].f = value;
2018-04-09 08:28:21 +02:00
}
if (smp->length > 0)
smp->flags |= SAMPLE_HAS_VALUES;
if (created > 0) {
smp->ts.origin = time_from_double(created);
smp->flags |= SAMPLE_HAS_ORIGIN;
}
2018-04-09 08:28:21 +02:00
return 0;
}
/*
* Note: The following functions are the same as io/json.c !!!
*/
int json_reserve_sprint(char *buf, size_t len, size_t *wbytes, struct sample *smps[], unsigned cnt, int flags)
{
int ret;
json_t *json;
size_t wr;
assert(cnt == 1);
ret = json_reserve_pack_sample(&json, smps[0], flags);
2018-04-09 08:28:21 +02:00
if (ret < 0)
return ret;
wr = json_dumpb(json, buf, len, 0);
json_decref(json);
if (wbytes)
*wbytes = wr;
return ret;
}
int json_reserve_sscan(char *buf, size_t len, size_t *rbytes, struct sample *smps[], unsigned cnt, int flags)
{
int ret;
json_t *json;
json_error_t err;
assert(cnt == 1);
json = json_loadb(buf, len, 0, &err);
if (!json)
return -1;
ret = json_reserve_unpack_sample(json, smps[0], flags);
2018-04-09 08:28:21 +02:00
json_decref(json);
if (ret < 0)
return ret;
2018-04-09 08:28:21 +02:00
if (rbytes)
*rbytes = err.position;
return ret;
}
int json_reserve_fprint(FILE *f, struct sample *smps[], unsigned cnt, int flags)
{
int ret, i;
json_t *json;
for (i = 0; i < cnt; i++) {
ret = json_reserve_pack_sample(&json, smps[i], flags);
2018-04-09 08:28:21 +02:00
if (ret)
return ret;
ret = json_dumpf(json, f, 0);
fputc('\n', f);
json_decref(json);
}
return i;
}
int json_reserve_fscan(FILE *f, struct sample *smps[], unsigned cnt, int flags)
{
int i, ret;
json_t *json;
json_error_t err;
for (i = 0; i < cnt; i++) {
skip: json = json_loadf(f, JSON_DISABLE_EOF_CHECK, &err);
if (!json)
break;
ret = json_reserve_unpack_sample(json, smps[i], flags);
2018-04-09 08:28:21 +02:00
if (ret)
goto skip;
json_decref(json);
}
return i;
}
static struct plugin p = {
.name = "json.reserve",
.description = "RESERVE JSON format",
.type = PLUGIN_TYPE_IO,
.io = {
.fscan = json_reserve_fscan,
.fprint = json_reserve_fprint,
.sscan = json_reserve_sscan,
.sprint = json_reserve_sprint,
.size = 0
},
};
REGISTER_PLUGIN(&p);