From 872dd097432a3fb4fb91d24d227486a59be5e24a Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Wed, 25 Apr 2018 11:00:12 +0200 Subject: 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 --- signal-composer-binding/signal.cpp | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'signal-composer-binding/signal.cpp') 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 #include +#include #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. -- cgit 1.2.3-korg