From 12097251ec058b1fa9d9202998c829b27ee5554f Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Sun, 17 Sep 2017 19:20:40 +0200 Subject: Get CPP controller plugin works Context passing variables not working well Change-Id: Ibc67bef353e5cc2e53ef9e5579d106baab92a604 Signed-off-by: Romain Forlot --- signal-composer-binding/signal-composer.cpp | 40 +++++++++++++--- signal-composer-binding/signal.cpp | 72 +++++++++++++++++++++++++---- signal-composer-binding/signal.hpp | 19 ++++++-- 3 files changed, 110 insertions(+), 21 deletions(-) (limited to 'signal-composer-binding') diff --git a/signal-composer-binding/signal-composer.cpp b/signal-composer-binding/signal-composer.cpp index 9288e56..8ad8c3b 100644 --- a/signal-composer-binding/signal-composer.cpp +++ b/signal-composer-binding/signal-composer.cpp @@ -22,7 +22,7 @@ CtlSectionT bindingApp::ctlSections_[] = { [0]={.key="plugins" ,.label = "plugins", .info=nullptr, .loadCB=PluginConfig, - .handle=nullptr}, + .handle=&bindingApp::instance()}, [1]={.key="sources" ,.label = "sources", .info=nullptr, .loadCB=loadSourcesAPI, .handle=nullptr}, @@ -363,17 +363,43 @@ json_object* bindingApp::getSignalValue(const std::string& sig, json_object* opt if (strcasestr(opts[idx], "last") && !last) { last = true; - double value = sigP->last(); - json_object_object_add(response, "last", - json_object_new_double(value)); + struct SignalValue value = sigP->last(); + if(value.hasBool) + { + json_object_object_add(response, "last", + json_object_new_boolean(value.boolVal)); + } + if(value.hasNum) + { + json_object_object_add(response, "last", + json_object_new_double(value.numVal)); + } + if(value.hasStr) + { + json_object_object_add(response, "last", + json_object_new_string(value.strVal.c_str())); + } } } if (!opts) { - double value = sigP->last(); - json_object_object_add(response, "last", - json_object_new_double(value)); + struct SignalValue value = sigP->last(); + if(value.hasBool) + { + json_object_object_add(response, "last", + json_object_new_boolean(value.boolVal)); + } + if(value.hasNum) + { + json_object_object_add(response, "last", + json_object_new_double(value.numVal)); + } + if(value.hasStr) + { + json_object_object_add(response, "last", + json_object_new_string(value.strVal.c_str())); + } } return response; diff --git a/signal-composer-binding/signal.cpp b/signal-composer-binding/signal.cpp index 147aef0..dfa3d5f 100644 --- a/signal-composer-binding/signal.cpp +++ b/signal-composer-binding/signal.cpp @@ -22,6 +22,8 @@ #include "signal.hpp" #include "signal-composer.hpp" +#define MICRO 1000000 + Signal::Signal(const std::string& id, std::vector& sources, const std::string& unit, @@ -29,6 +31,8 @@ Signal::Signal(const std::string& id, CtlActionT* onReceived) :id_(id), signalSigList_(sources), + timestamp_(0.0), + value_({0,0,0,0,0,""}), frequency_(frequency), unit_(unit), onReceived_(onReceived) @@ -90,14 +94,21 @@ json_object* Signal::toJSON() const return queryJ; } -void update(double timestamp, double value) +void Signal::update(long long int timestamp, struct SignalValue value) { - AFB_NOTICE("Got an update from observed signal"); + AFB_NOTICE("Got an update from observed signal. Timestamp: %lld, vb: %d, vn: %lf, vs: %s", timestamp, value.boolVal, value.numVal, value.strVal.c_str()); } +/// @brief Notify observers that there is a change and execute callback defined +/// when signal is received +/// +/// @param[in] queryJ - JSON query object to transmit to callback function +/// +/// @return 0 if OK, -1 or other if not. int Signal::onReceivedCB(json_object *queryJ) { - return ActionExecOne(onReceived_, queryJ); + notify(); + return onReceived_ ? ActionExecOne(onReceived_, queryJ) : 0; } void Signal::attach(Signal* obs) @@ -157,15 +168,49 @@ int Signal::recursionCheck() double Signal::average(int seconds) const { - return 0.0; + long long int begin = history_.begin()->first, + end = begin+(seconds*MICRO); + double total = 0.0; + int nbElt = 0; + + for (const auto& val: history_) + { + if(val.first >= end) + {break;} + if(val.second.hasNum) + { + total += val.second.numVal; + nbElt++; + } + else + { + AFB_ERROR("There isn't numerical value to compare with in that signal '%s'. Stored value : bool %d, num %lf, str: %s", + id_.c_str(), + val.second.boolVal, + val.second.numVal, + val.second.strVal.c_str()); + break; + } + } + + return total / nbElt; } double Signal::minimum() const { double min = DBL_MAX; for (auto& v : history_) { - double temp_min = v.second; - if(temp_min < min) { min = temp_min;} + if(v.second.hasNum && v.second.numVal < min) + {min = v.second.numVal;} + else + { + AFB_ERROR("There isn't numerical value to compare with in that signal '%s'. Stored value : bool %d, num %lf, str: %s", + id_.c_str(), + v.second.boolVal, + v.second.numVal, + v.second.strVal.c_str()); + break; + } } return min; } @@ -175,12 +220,21 @@ double Signal::maximum() const double max = 0.0; for (auto& v : history_) { - double temp_max = v.second; - if(temp_max > max) { max = temp_max;} + if(v.second.hasNum && v.second.hasNum > max) + {max = v.second.numVal;} + else + { + AFB_ERROR("There isn't numerical value to compare with in that signal '%s'. Stored value : bool %d, num %lf, str: %s", + id_.c_str(), + v.second.boolVal, + v.second.numVal, + v.second.strVal.c_str()); + break; + } } return max; } -double Signal::last() const +struct SignalValue Signal::last() const { return history_.rbegin()->second; } diff --git a/signal-composer-binding/signal.hpp b/signal-composer-binding/signal.hpp index ac3b678..34e6ab1 100644 --- a/signal-composer-binding/signal.hpp +++ b/signal-composer-binding/signal.hpp @@ -24,19 +24,29 @@ class bindingApp; +struct SignalValue { + bool hasBool = false; + bool boolVal; + bool hasNum = false; + double numVal; + bool hasStr = false; + std::string strVal; +}; + class Signal { private: std::string id_; std::vector signalSigList_; long long int timestamp_; - double value_; - std::map history_; ///< history_ - Hold signal value history in map with + struct SignalValue value_; + std::map history_; ///< history_ - Hold signal value history in map with double frequency_; std::string unit_; CtlActionT* onReceived_; std::vector Observers_; + void notify(); void attach(Signal *obs); int recursionCheck(const std::string& origId); @@ -49,14 +59,13 @@ public: const std::string id() const; json_object* toJSON() const; - void update(long long int timestamp, double value); + void update(long long int timestamp, struct SignalValue value); int onReceivedCB(json_object *queryJ); void attachToSourceSignals(bindingApp& bApp); - void notify(); double average(int seconds = 0) const; double minimum() const; double maximum() const; - double last() const; + struct SignalValue last() const; int recursionCheck(); }; -- cgit 1.2.3-korg