From fd7fddcf15ab597e51241bf2160270aecf37100d Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Tue, 20 Jul 2021 12:23:31 +0200 Subject: [PATCH] json.edgeflex: add sample unpacking support --- include/villas/formats/json_edgeflex.hpp | 3 ++- lib/formats/json_edgeflex.cpp | 26 ++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/include/villas/formats/json_edgeflex.hpp b/include/villas/formats/json_edgeflex.hpp index 5a83f26aa..290edb109 100644 --- a/include/villas/formats/json_edgeflex.hpp +++ b/include/villas/formats/json_edgeflex.hpp @@ -31,7 +31,8 @@ class JsonEdgeflexFormat : public JsonFormat { protected: int packSample(json_t **j, const struct sample *smp); - + int unpackSample(json_t *json_smp, struct sample *smp); + public: using JsonFormat::JsonFormat; }; diff --git a/lib/formats/json_edgeflex.cpp b/lib/formats/json_edgeflex.cpp index 6e3c8cdab..4cfb9aeb9 100644 --- a/lib/formats/json_edgeflex.cpp +++ b/lib/formats/json_edgeflex.cpp @@ -49,10 +49,32 @@ int JsonEdgeflexFormat::packSample(json_t **json_smp, const struct sample *smp) if (json_created) json_object_set_new(json_value, "created", json_created); - if (json_value == nullptr) +int JsonEdgeflexFormat::unpackSample(json_t *json_smp, struct sample *smp) +{ + int ret; + json_int_t created = -1; + + if (smp->capacity < 1) return -1; - *json_smp = json_value; + struct signal *sig = (struct signal *) vlist_at_safe(signals, 0); + if (!sig) + return -1; + + if (sig->type != SignalType::FLOAT) + return -1; + + ret = json_unpack(json_smp, "{ s: f, s?: I }", + "value", &smp->data[0].f, + "created", &created + ); + if (ret) + return ret; + + if (created >= 0) { + smp->ts.origin = time_from_double(created / 1e3); + smp->flags |= (int) SampleFlags::HAS_TS_ORIGIN; + } return 0; }