1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

io: obey io->flags and smp->flags during encoding/decoding of samples

This commit is contained in:
Steffen Vogel 2018-10-20 16:23:57 +02:00
parent 8e052eaa5e
commit 759d633967
4 changed files with 82 additions and 44 deletions

View file

@ -36,20 +36,26 @@ static size_t csv_sprint_single(struct io *io, char *buf, size_t len, const stru
size_t off = 0;
struct signal *sig;
if (io->flags & SAMPLE_HAS_TS_ORIGIN)
off += snprintf(buf + off, len - off, "%ld%c%09ld", smp->ts.origin.tv_sec, io->separator, smp->ts.origin.tv_nsec);
else
off += snprintf(buf + off, len - off, "nan%cnan", io->separator);
if (io->flags & SAMPLE_HAS_TS_ORIGIN) {
if (io->flags & SAMPLE_HAS_TS_ORIGIN)
off += snprintf(buf + off, len - off, "%ld%c%09ld", smp->ts.origin.tv_sec, io->separator, smp->ts.origin.tv_nsec);
else
off += snprintf(buf + off, len - off, "nan%cnan", io->separator);
}
if (io->flags & SAMPLE_HAS_TS_RECEIVED)
off += snprintf(buf + off, len - off, "%c%.09f", io->separator, time_delta(&smp->ts.origin, &smp->ts.received));
else
off += snprintf(buf + off, len - off, "%cnan", io->separator);
if (io->flags & SAMPLE_HAS_OFFSET) {
if (smp->flags & SAMPLE_HAS_TS_RECEIVED)
off += snprintf(buf + off, len - off, "%c%.09f", io->separator, time_delta(&smp->ts.origin, &smp->ts.received));
else
off += snprintf(buf + off, len - off, "%cnan", io->separator);
}
if (io->flags & SAMPLE_HAS_SEQUENCE)
off += snprintf(buf + off, len - off, "%c%" PRIu64, io->separator, smp->sequence);
else
off += snprintf(buf + off, len - off, "%cnan", io->separator);
if (io->flags & SAMPLE_HAS_SEQUENCE) {
if (smp->flags & SAMPLE_HAS_SEQUENCE)
off += snprintf(buf + off, len - off, "%c%" PRIu64, io->separator, smp->sequence);
else
off += snprintf(buf + off, len - off, "%cnan", io->separator);
}
if (io->flags & SAMPLE_HAS_DATA) {
for (int i = 0; i < smp->length; i++) {
@ -177,18 +183,31 @@ void csv_header(struct io *io, const struct sample *smp)
{
FILE *f = io_stream_output(io);
fprintf(f, "# secs%cnsecs%coffset%csequence", io->separator, io->separator, io->separator);
fprintf(f, "# ");
if (io->flags & SAMPLE_HAS_TS_ORIGIN)
fprintf(f, "secs%cnsecs%c", io->separator, io->separator);
for (int i = 0; i < smp->length; i++) {
struct signal *sig = (struct signal *) list_at(smp->signals, i);
if (io->flags & SAMPLE_HAS_OFFSET)
fprintf(f, "offset%c", io->separator);
if (sig->name)
fprintf(f, "%c%s", io->separator, sig->name);
else
fprintf(f, "%csignal%d", io->separator, i);
if (io->flags & SAMPLE_HAS_SEQUENCE)
fprintf(f, "sequence%c", io->separator);
if (sig->unit)
fprintf(f, "[%s]", sig->unit);
if (io->flags & SAMPLE_HAS_DATA) {
for (int i = 0; i < smp->length; i++) {
struct signal *sig = (struct signal *) list_at(smp->signals, i);
if (sig->name)
fprintf(f, "%s", sig->name);
else
fprintf(f, "signal%d", i);
if (sig->unit)
fprintf(f, "[%s]", sig->unit);
if (i+1 < smp->length)
fprintf(f, "%c", io->separator);
}
}
fprintf(f, "%c", io->delimiter);

View file

@ -50,14 +50,14 @@ static enum signal_type json_detect_format(json_t *val)
}
}
static json_t * json_pack_timestamps(struct sample *smp)
static json_t * json_pack_timestamps(struct io *io, struct sample *smp)
{
json_t *json_ts = json_object();
if (smp->flags & SAMPLE_HAS_TS_ORIGIN)
if (io->flags & smp->flags & SAMPLE_HAS_TS_ORIGIN)
json_object_set(json_ts, "origin", json_pack("[ I, I ]", smp->ts.origin.tv_sec, smp->ts.origin.tv_nsec));
if (smp->flags & SAMPLE_HAS_TS_RECEIVED)
if (io->flags & smp->flags & SAMPLE_HAS_TS_RECEIVED)
json_object_set(json_ts, "received", json_pack("[ I, I ]", smp->ts.received.tv_sec, smp->ts.received.tv_nsec));
return json_ts;
@ -98,15 +98,15 @@ static int json_pack_sample(struct io *io, json_t **j, struct sample *smp)
json_t *json_smp;
json_error_t err;
json_smp = json_pack_ex(&err, 0, "{ s: o }", "ts", json_pack_timestamps(smp));
json_smp = json_pack_ex(&err, 0, "{ s: o }", "ts", json_pack_timestamps(io, smp));
if (smp->flags & SAMPLE_HAS_SEQUENCE) {
if (io->flags & smp->flags & SAMPLE_HAS_SEQUENCE) {
json_t *json_sequence = json_integer(smp->sequence);
json_object_set(json_smp, "sequence", json_sequence);
}
if (smp->flags & SAMPLE_HAS_DATA) {
if (io->flags & smp->flags & SAMPLE_HAS_DATA) {
json_t *json_data = json_array();
for (int i = 0; i < smp->length; i++) {

View file

@ -68,12 +68,12 @@ int protobuf_sprint(struct io *io, char *buf, size_t len, size_t *wbytes, struct
pb_smp->type = VILLAS__NODE__SAMPLE__TYPE__DATA;
if (smp->flags & SAMPLE_HAS_SEQUENCE) {
if (io->flags & smp->flags & SAMPLE_HAS_SEQUENCE) {
pb_smp->has_sequence = 1;
pb_smp->sequence = smp->sequence;
}
if (smp->flags & SAMPLE_HAS_TS_ORIGIN) {
if (io->flags & smp->flags & SAMPLE_HAS_TS_ORIGIN) {
pb_smp->timestamp = alloc(sizeof(Villas__Node__Timestamp));
villas__node__timestamp__init(pb_smp->timestamp);

View file

@ -38,15 +38,23 @@ static size_t villas_human_sprint_single(struct io *io, char *buf, size_t len, c
struct signal *sig;
if (io->flags & SAMPLE_HAS_TS_ORIGIN) {
off += snprintf(buf + off, len - off, "%llu", (unsigned long long) smp->ts.origin.tv_sec);
off += snprintf(buf + off, len - off, ".%09llu", (unsigned long long) smp->ts.origin.tv_nsec);
if (smp->flags & SAMPLE_HAS_TS_ORIGIN) {
off += snprintf(buf + off, len - off, "%llu", (unsigned long long) smp->ts.origin.tv_sec);
off += snprintf(buf + off, len - off, ".%09llu", (unsigned long long) smp->ts.origin.tv_nsec);
}
else
off += snprintf(buf + off, len - off, "nan.nan");
}
if (io->flags & SAMPLE_HAS_TS_RECEIVED)
off += snprintf(buf + off, len - off, "%+e", time_delta(&smp->ts.origin, &smp->ts.received));
if (io->flags & SAMPLE_HAS_OFFSET) {
if (smp->flags & SAMPLE_HAS_TS_RECEIVED)
off += snprintf(buf + off, len - off, "%+e", time_delta(&smp->ts.origin, &smp->ts.received));
}
if (io->flags & SAMPLE_HAS_SEQUENCE)
off += snprintf(buf + off, len - off, "(%" PRIu64 ")", smp->sequence);
if (io->flags & SAMPLE_HAS_SEQUENCE) {
if (io->flags & SAMPLE_HAS_SEQUENCE)
off += snprintf(buf + off, len - off, "(%" PRIu64 ")", smp->sequence);
}
if (io->flags & SAMPLE_HAS_DATA) {
for (int i = 0; i < smp->length; i++) {
@ -207,18 +215,29 @@ void villas_human_header(struct io *io, const struct sample *smp)
{
FILE *f = io_stream_output(io);
fprintf(f, "# %-20s", "seconds.nanoseconds+offset(sequence)");
fprintf(f, "# ");
for (int i = 0; i < smp->length; i++) {
struct signal *sig = (struct signal *) list_at(smp->signals, i);
if (io->flags & SAMPLE_HAS_TS_ORIGIN)
fprintf(f, "seconds.nanoseconds");
if (sig->name)
fprintf(f, "%c%s", io->separator, sig->name);
else
fprintf(f, "%csignal%d", io->separator, i);
if (io->flags & SAMPLE_HAS_OFFSET)
fprintf(f, "+offset");
if (sig->unit)
fprintf(f, "[%s]", sig->unit);
if (io->flags & SAMPLE_HAS_SEQUENCE)
fprintf(f, "(sequence)");
if (io->flags & SAMPLE_HAS_DATA) {
for (int i = 0; i < smp->length; i++) {
struct signal *sig = (struct signal *) list_at(smp->signals, i);
if (sig->name)
fprintf(f, "%c%s", io->separator, sig->name);
else
fprintf(f, "%csignal%d", io->separator, i);
if (sig->unit)
fprintf(f, "[%s]", sig->unit);
}
}
fprintf(f, "%c", io->delimiter);