diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2018-04-25 11:00:12 +0200 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2018-07-05 15:43:09 +0200 |
commit | 872dd097432a3fb4fb91d24d227486a59be5e24a (patch) | |
tree | 3f3ae7cdcb6dd222f39370fff55bfa1912ba1aff /signal-composer-binding | |
parent | 7229918a064a862c51217f1294a320c7260da937 (diff) |
Improve signal handling
- Fix memleak: RRelease the Signal JSON representation once used
- Change equality condition between signals
- Use a REALTIME clock to retrieve the timestamp if not provided by
the signal. This is the most simple to date a signal and
use that time for further usage.
- Add a method to retrieve the last_timestamp value from
a signal
This with the commit from ctl-utilities submodule
"8ff0fe3c454ea32ba383a3cfa9c4e91237d6c494" fixes the
issue.
Change-Id: Ifed66796f92d789c80f10b161318f6bef788dda7
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'signal-composer-binding')
-rw-r--r-- | signal-composer-binding/clientApp.cpp | 10 | ||||
-rw-r--r-- | signal-composer-binding/signal-composer.cpp | 4 | ||||
-rw-r--r-- | signal-composer-binding/signal.cpp | 31 | ||||
-rw-r--r-- | signal-composer-binding/signal.hpp | 3 |
4 files changed, 32 insertions, 16 deletions
diff --git a/signal-composer-binding/clientApp.cpp b/signal-composer-binding/clientApp.cpp index 854f96e..45c43e6 100644 --- a/signal-composer-binding/clientApp.cpp +++ b/signal-composer-binding/clientApp.cpp @@ -25,9 +25,13 @@ clientAppCtx::clientAppCtx(const char* uuid) void clientAppCtx::update(Signal* sig) { json_object* sigJ = sig->toJSON(); - if(afb_event_push(event_, sigJ) == 0) - {sig->delObserver(this);} - return; + + if(afb_event_is_valid(event_)) { + if(!afb_event_push(event_, sigJ)) + {sig->delObserver(this);} + } + else + {json_object_put(sigJ);} } void clientAppCtx::appendSignals(std::vector<std::shared_ptr<Signal>>& sigV) diff --git a/signal-composer-binding/signal-composer.cpp b/signal-composer-binding/signal-composer.cpp index 0ee210b..1752164 100644 --- a/signal-composer-binding/signal-composer.cpp +++ b/signal-composer-binding/signal-composer.cpp @@ -520,7 +520,7 @@ void Composer::processOptions(const std::map<std::string, int>& opts, std::share else if (o.first.compare("last") && !last) { last = true; - struct signalValue value = sig->last(); + struct signalValue value = sig->last_value(); if(value.hasBool) { json_object_object_add(response, "value", @@ -744,7 +744,7 @@ json_object* Composer::getsignalValue(const std::string& sig, json_object* optio "signal", sig->id().c_str()); if (opts.empty()) { - struct signalValue value = sig->last(); + struct signalValue value = sig->last_value(); if(value.hasBool) { json_object_object_add(response, "value", diff --git a/signal-composer-binding/signal.cpp b/signal-composer-binding/signal.cpp index d10dda6..66c8b54 100644 --- a/signal-composer-binding/signal.cpp +++ b/signal-composer-binding/signal.cpp @@ -17,6 +17,7 @@ #include <float.h> #include <string.h> +#include <fnmatch.h> #include "signal.hpp" #include "signal-composer.hpp" @@ -94,8 +95,10 @@ bool Signal::operator ==(const Signal& other) const bool Signal::operator ==(const std::string& aName) const { - if(id_.find(aName) != std::string::npos) {return true;} - if(event_.find(aName) != std::string::npos) {return true;} + if(! ::fnmatch(aName.c_str(), id_.c_str(), FNM_CASEFOLD)) + {return true;} + if(! ::fnmatch(aName.c_str(), event_.c_str(), FNM_CASEFOLD)) + {return true;} return false; } @@ -129,6 +132,7 @@ json_object* Signal::toJSON() const wrap_json_pack(&queryJ, "{ss,so*}", "uid", id_.c_str(), "getSignalsArgs", getSignalsArgs_); + AFB_ApiDebug(nullptr, "========== %s", json_object_get_string(queryJ)); if (!event_.empty()) {json_object_object_add(queryJ, "event", json_object_new_string(event_.c_str()));} @@ -230,10 +234,11 @@ void Signal::defaultReceivedCB(json_object *eventJ) json_object_iterator iterEnd = json_object_iter_end(eventJ); while(!json_object_iter_equal(&iter, &iterEnd)) { - std::string name = json_object_iter_peek_name(&iter); - std::transform(name.begin(), name.end(), name.begin(), ::tolower); + std::string key = json_object_iter_peek_name(&iter); + std::transform(key.begin(), key.end(), key.begin(), ::tolower); json_object *value = json_object_iter_peek_value(&iter); - if (name.find("value") || name.find(id_)) + if (key.find("value") != std::string::npos || + key.find(id_) != std::string::npos) { if(json_object_is_type(value, json_type_double)) {sv = json_object_get_double(value);} @@ -242,7 +247,7 @@ void Signal::defaultReceivedCB(json_object *eventJ) else if(json_object_is_type(value, json_type_string)) {sv = json_object_get_string(value);} } - else if (name.find("timestamp")) + else if (key.find("timestamp") != std::string::npos) { ts = json_object_is_type(value, json_type_int) ? json_object_get_int64(value):ts; } @@ -256,9 +261,10 @@ void Signal::defaultReceivedCB(json_object *eventJ) } else if(ts == 0) { - struct timespec t_usec; - if(!::clock_gettime(CLOCK_MONOTONIC, &t_usec)) - ts = (t_usec.tv_nsec / 1000ll) + (t_usec.tv_sec* 1000000ll); + struct timespec t; + + if(!::clock_gettime(CLOCK_REALTIME, &t)) + ts = (uint64_t)(t.tv_sec) * (uint64_t)1000000 + ((uint64_t)(t.tv_nsec) / 1000); } set(ts, sv); @@ -426,12 +432,17 @@ double Signal::maximum(int seconds) const /// @brief Return last value recorded /// /// @return Last value -struct signalValue Signal::last() const +struct signalValue Signal::last_value() const { if(history_.empty()) {return signalValue();} return history_.rbegin()->second; } +uint64_t Signal::last_timestamp() const +{ + if(history_.empty()) {return 0;} + return history_.rbegin()->first; +} /// @brief Recursion check to ensure that there is no infinite loop /// in the Observers/Observables structure. diff --git a/signal-composer-binding/signal.hpp b/signal-composer-binding/signal.hpp index c825b37..ddae87d 100644 --- a/signal-composer-binding/signal.hpp +++ b/signal-composer-binding/signal.hpp @@ -115,7 +115,8 @@ public: double average(int seconds = 0) const; double minimum(int seconds = 0) const; double maximum(int seconds = 0) const; - struct signalValue last() const; + struct signalValue last_value() const; + uint64_t last_timestamp() const; int initialRecursionCheck(); int recursionCheck(Signal* obs); |