summaryrefslogtreecommitdiffstats
path: root/signal-composer-binding
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-09-28 20:06:08 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2017-12-14 11:00:25 +0100
commit7b5a679d0ae117eb6b5bbcf516bf9e5051afd7dd (patch)
tree9b66692a0d25abba64a00bc8f5267a46007d70f1 /signal-composer-binding
parent0ea61ebe60b1be3b33c400bf1a8ebf9cc6644349 (diff)
Inheritance of some signals parameters from source
Stores sourceAPI as shared_ptr too. Change-Id: I2dcd919b3053f29812a9bece14feb4f620f23ce3 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'signal-composer-binding')
-rw-r--r--signal-composer-binding/signal-composer.cpp100
-rw-r--r--signal-composer-binding/signal-composer.hpp6
-rw-r--r--signal-composer-binding/signal.cpp4
-rw-r--r--signal-composer-binding/signal.hpp2
-rw-r--r--signal-composer-binding/source.cpp12
-rw-r--r--signal-composer-binding/source.hpp10
6 files changed, 88 insertions, 46 deletions
diff --git a/signal-composer-binding/signal-composer.cpp b/signal-composer-binding/signal-composer.cpp
index 2247ba9..a340aa6 100644
--- a/signal-composer-binding/signal-composer.cpp
+++ b/signal-composer-binding/signal-composer.cpp
@@ -135,15 +135,23 @@ CtlActionT* Composer::convert2Action(const std::string& name, json_object* actio
int Composer::loadOneSourceAPI(json_object* sourceJ)
{
- json_object *initJ = nullptr, *getSignalsJ = nullptr;
- CtlActionT *initCtl = nullptr, *getSignalsCtl = nullptr;
+ json_object *initJ = nullptr,
+ *getSignalsJ = nullptr,
+ *onReceivedJ = nullptr;
+ CtlActionT *initCtl = nullptr,
+ *getSignalsCtl = nullptr,
+ *onReceivedCtl = nullptr;
const char *api, *info;
+ int retention = 0;
- int err = wrap_json_unpack(sourceJ, "{ss,s?s,s?o,s?o !}",
+ int err = wrap_json_unpack(sourceJ, "{ss,s?s,s?o,s?o,s?o, s?i !}",
"api", &api,
"info", &info,
"init", &initJ,
- "getSignals", &getSignalsJ);
+ "getSignals", &getSignalsJ,
+ // Signals field to make signals conf by sources
+ "onReceived", &onReceivedJ,
+ "retention", &retention);
if (err)
{
AFB_ERROR("Missing something api|[info]|[init]|[getSignals] in %s", json_object_get_string(sourceJ));
@@ -157,7 +165,10 @@ int Composer::loadOneSourceAPI(json_object* sourceJ)
{AFB_WARNING("Caution! You don't specify the API source as required in the metadata section. This API '%s' may not be initialized", api);}
}
- if(initJ) {initCtl = convert2Action("init", initJ);}
+ initCtl = initJ ? convert2Action("init", initJ) : nullptr;
+
+ // Define default action to take to subscibe souce's signals if none
+ // defined.
if(!getSignalsJ)
{
std::string function = "api://" + std::string(api) + "/subscribe";
@@ -165,7 +176,9 @@ int Composer::loadOneSourceAPI(json_object* sourceJ)
}
getSignalsCtl = convert2Action("getSignals", getSignalsJ);
- sourcesListV_.push_back(SourceAPI(api, info, initCtl, getSignalsCtl));
+ onReceivedCtl = onReceivedJ ? convert2Action("onReceived", onReceivedJ) : nullptr;
+
+ sourcesListV_.push_back(std::make_shared<SourceAPI>(api, info, initCtl, getSignalsCtl, onReceivedCtl, retention));
return err;
}
@@ -210,16 +223,18 @@ int Composer::loadSourcesAPI(CtlSectionT* section, json_object *sourcesJ)
int Composer::loadOneSignal(json_object* signalJ)
{
- json_object *onReceivedJ = nullptr, *dependsJ = nullptr, *getSignalsArgs = nullptr;
+ json_object *onReceivedJ = nullptr,
+ *dependsJ = nullptr,
+ *getSignalsArgs = nullptr;
CtlActionT* onReceivedCtl;
const char *id = nullptr,
*event = nullptr,
*unit = nullptr;
int retention;
double frequency=0.0;
- std::string api;
std::vector<std::string> dependsV;
ssize_t sep;
+ std::shared_ptr<SourceAPI> src = nullptr;
int err = wrap_json_unpack(signalJ, "{ss,s?s,s?o,s?o,s?F,s?s,s?F,s?o !}",
"id", &id,
@@ -236,13 +251,6 @@ int Composer::loadOneSignal(json_object* signalJ)
return err;
}
- // Set default retention is not specified
- retention = !retention ? 30 : retention;
- unit = !unit ? "" : unit;
-
- // Get an action handler
- onReceivedCtl = onReceivedJ ? convert2Action("onReceived", onReceivedJ) : nullptr;
-
// event or depends field manadatory
if( (!event && !dependsJ) || (event && dependsJ) )
{
@@ -250,6 +258,22 @@ int Composer::loadOneSignal(json_object* signalJ)
return -1;
}
+ // Declare a raw signal
+ if(event)
+ {
+ std::string eventStr = std::string(event);
+ if( (sep = eventStr.find("/")) == std::string::npos)
+ {
+ AFB_ERROR("Missing something in event declaration. Has to be like: <api>/<event>");
+ return -1;
+ }
+ std::string api = eventStr.substr(0, sep);
+ src = getSourceAPI(api);
+ }
+ else
+ {
+ event = "";
+ }
// Process depends JSON object to declare virtual signal dependencies
if (dependsJ)
{
@@ -266,7 +290,6 @@ int Composer::loadOneSignal(json_object* signalJ)
}
dependsV.push_back(sourceStr);
}
- api = sourcesListV_.rbegin()->api();
}
else
{
@@ -277,27 +300,28 @@ int Composer::loadOneSignal(json_object* signalJ)
return -1;
}
dependsV.push_back(sourceStr);
- api = sourcesListV_.rbegin()->api();
}
+ if(!src) {src=*sourcesListV_.rbegin();}
}
- // Declare a raw signal
- if(event)
+ // Set default retention if not specified
+ if(!retention)
{
- std::string eventStr = std::string(event);
- if( (sep = eventStr.find("/")) == std::string::npos)
- {
- AFB_ERROR("Missing something in event declaration. Has to be like: <api>/<event>");
- return -1;
- }
- api = eventStr.substr(0, sep);
+ retention = src->signalsDefault().retention ?
+ src->signalsDefault().retention :
+ 30;
}
- else
+ unit = !unit ? "" : unit;
+
+ // Set default onReceived action if not specified
+ if(!onReceivedJ)
{
- event = "";
+ onReceivedCtl = src->signalsDefault().onReceived ?
+ src->signalsDefault().onReceived :
+ nullptr;
}
+ else {onReceivedCtl = convert2Action("onReceived", onReceivedJ);}
- SourceAPI* src = getSourceAPI(api) ? getSourceAPI(api):getSourceAPI("signal-composer");
if(src != nullptr)
{src->addSignal(id, event, dependsV, retention, unit, frequency, onReceivedCtl, getSignalsArgs);}
else
@@ -459,17 +483,17 @@ int Composer::initSourcesAPI()
int err = 0;
for(auto& src: sourcesListV_)
{
- err += src.init();
+ err += src->init();
}
return err;
}
-SourceAPI* Composer::getSourceAPI(const std::string& api)
+std::shared_ptr<SourceAPI> Composer::getSourceAPI(const std::string& api)
{
for(auto& source: sourcesListV_)
{
- if (source.api() == api)
- {return &source;}
+ if (source->api() == api)
+ {return source;}
}
return nullptr;
}
@@ -479,7 +503,7 @@ std::vector<std::shared_ptr<Signal>> Composer::getAllSignals()
std::vector<std::shared_ptr<Signal>> allSignals;
for( auto& source : sourcesListV_)
{
- std::vector<std::shared_ptr<Signal>> srcSignals = source.getSignals();
+ std::vector<std::shared_ptr<Signal>> srcSignals = source->getSignals();
allSignals.insert(allSignals.end(), srcSignals.begin(), srcSignals.end());
}
@@ -494,7 +518,7 @@ std::vector<std::shared_ptr<Signal>> Composer::searchSignals(const std::string&
if(sep != std::string::npos)
{
api = aName.substr(0, sep);
- SourceAPI* source = getSourceAPI(api);
+ std::shared_ptr<SourceAPI> source = getSourceAPI(api);
return source->searchSignals(aName);
}
else
@@ -563,11 +587,11 @@ json_object* Composer::getsignalValue(const std::string& sig, json_object* optio
int Composer::execSignalsSubscription()
{
int err = 0;
- for(SourceAPI& srcAPI: sourcesListV_)
+ for(std::shared_ptr<SourceAPI> srcAPI: sourcesListV_)
{
- if (srcAPI.api() != std::string(ctlConfig_->api))
+ if (srcAPI->api() != std::string(ctlConfig_->api))
{
- err = srcAPI.makeSubscription();
+ err = srcAPI->makeSubscription();
}
}
return err;
diff --git a/signal-composer-binding/signal-composer.hpp b/signal-composer-binding/signal-composer.hpp
index de7a799..f472c4f 100644
--- a/signal-composer-binding/signal-composer.hpp
+++ b/signal-composer-binding/signal-composer.hpp
@@ -28,7 +28,7 @@ private:
CtlConfigT* ctlConfig_;
static CtlSectionT ctlSections_[]; ///< Config Section definition (note: controls section index should match handle retrieval in)
- std::vector<SourceAPI> sourcesListV_;
+ std::vector<std::shared_ptr<SourceAPI>> sourcesListV_;
explicit Composer(const std::string& filepath);
Composer();
@@ -43,8 +43,8 @@ private:
static int loadSignals(CtlSectionT* section, json_object *signalsJ);
int initSourcesAPI();
- SourceAPI* getSourceAPI(const std::string& api);
- void processOptions(const char** opts, std::shared_ptr<Signal> sig, json_object* response) const;
+ std::shared_ptr<SourceAPI> getSourceAPI(const std::string& api);
+ void processOptions(const std::map<std::string, int>& opts, std::shared_ptr<Signal> sig, json_object* response) const;
public:
static Composer& instance();
static void* createContext(void* ctx);
diff --git a/signal-composer-binding/signal.cpp b/signal-composer-binding/signal.cpp
index 2ec9908..c7220d6 100644
--- a/signal-composer-binding/signal.cpp
+++ b/signal-composer-binding/signal.cpp
@@ -16,11 +16,12 @@
*/
#include <float.h>
+#include <string.h>
#include "signal.hpp"
#include "signal-composer.hpp"
-#define MICRO 1000000000
+#define USEC_TIMESTAMP_FLAG 1506514324881224
Signal::Signal()
:id_(""),
@@ -138,7 +139,6 @@ json_object* Signal::toJSON() const
///
/// @param[in] timestamp - timestamp of occured signal
/// @param[in] value - value of change
-/// @param[in] unit - optionnal string to change the signal unit
void Signal::set(uint64_t timestamp, struct signalValue& value)
{
uint64_t diff = retention_+1;
diff --git a/signal-composer-binding/signal.hpp b/signal-composer-binding/signal.hpp
index 36479f6..66b14e5 100644
--- a/signal-composer-binding/signal.hpp
+++ b/signal-composer-binding/signal.hpp
@@ -24,6 +24,8 @@
#include "observer-pattern.hpp"
+#define MICRO 1000000
+
class Composer;
/// @brief Structure holding a possible value of a Signal
diff --git a/signal-composer-binding/source.cpp b/signal-composer-binding/source.cpp
index 5989dac..d1b2120 100644
--- a/signal-composer-binding/source.cpp
+++ b/signal-composer-binding/source.cpp
@@ -21,8 +21,11 @@
SourceAPI::SourceAPI()
{}
-SourceAPI::SourceAPI(const std::string& api, const std::string& info, CtlActionT* init, CtlActionT* getSignals):
- api_(api), info_(info), init_(init), getSignals_(getSignals)
+SourceAPI::SourceAPI(const std::string& api, const std::string& info, CtlActionT* init, CtlActionT* getSignals, CtlActionT* onReceived, int retention):api_(api),
+ info_(info),
+ init_(init),
+ getSignals_(getSignals),
+ signalsDefault_({onReceived, retention})
{}
int SourceAPI::init()
@@ -40,6 +43,11 @@ std::string SourceAPI::api() const
return api_;
}
+const struct signalsDefault& SourceAPI::signalsDefault() const
+{
+ return signalsDefault_;
+}
+
void SourceAPI::addSignal(const std::string& id, const std::string& event, std::vector<std::string>& depends, int retention, const std::string& unit, double frequency, CtlActionT* onReceived, json_object* getSignalsArgs)
{
std::shared_ptr<Signal> sig = std::make_shared<Signal>(id, event, depends, unit, retention, frequency, onReceived, getSignalsArgs);
diff --git a/signal-composer-binding/source.hpp b/signal-composer-binding/source.hpp
index 01bb08d..6aacf4f 100644
--- a/signal-composer-binding/source.hpp
+++ b/signal-composer-binding/source.hpp
@@ -19,21 +19,29 @@
#include <memory>
#include "signal.hpp"
+struct signalsDefault {
+ CtlActionT* onReceived;
+ int retention;
+};
+
class SourceAPI {
private:
std::string api_;
std::string info_;
CtlActionT* init_;
CtlActionT* getSignals_;
+ // Parameters inherited by source's signals if none defined for it
+ struct signalsDefault signalsDefault_;
std::map<std::string, std::shared_ptr<Signal>> signalsMap_;
public:
SourceAPI();
- SourceAPI(const std::string& api, const std::string& info, CtlActionT* init, CtlActionT* getSignal);
+ SourceAPI(const std::string& api, const std::string& info, CtlActionT* init, CtlActionT* getSignal, CtlActionT* onReceived, int retention);
int init();
std::string api() const;
+ const struct signalsDefault& signalsDefault() const;
void addSignal(const std::string& id, const std::string& event, std::vector<std::string>& sources, int retention, const std::string& unit, double frequency, CtlActionT* onReceived, json_object* getSignalsArgs);
std::vector<std::shared_ptr<Signal>> getSignals() const;