From 58f2d68238464f8fb3451b3c8245e62914b4bb14 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Thu, 21 Sep 2017 18:17:45 +0200 Subject: Batch low-can subscription Change-Id: Ic1561e34d01be2b15618536fe6ef01066c184520 Signed-off-by: Romain Forlot --- plugins/low-can.cpp | 77 ++++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 36 deletions(-) (limited to 'plugins') diff --git a/plugins/low-can.cpp b/plugins/low-can.cpp index 7fc5020..c1fbd60 100644 --- a/plugins/low-can.cpp +++ b/plugins/low-can.cpp @@ -47,6 +47,7 @@ typedef struct { typedef struct { struct pluginCBT* pluginHandle; + json_object *subscriptionBatch; allDoorsCtxT allDoorsCtx; } lowCANCtxT; @@ -58,14 +59,12 @@ void setDoor(doorT* aDoor, const char* eventName, int eventStatus) } // Call at initialisation time -CTLP_ONLOAD(plugin, bAppHandle) +CTLP_ONLOAD(plugin, composerHandle) { lowCANCtxT *pluginCtx= (lowCANCtxT*)calloc (1, sizeof(lowCANCtxT)); - allDoorsCtxT allDoorsCtx = allDoorsCtxT(); - ::memset(&allDoorsCtx, 0, sizeof(allDoorsCtxT)); - pluginCtx->allDoorsCtx = allDoorsCtx; - pluginCtx->pluginHandle = (struct pluginCBT*)bAppHandle; + pluginCtx->pluginHandle = (struct pluginCBT*)composerHandle; + pluginCtx->subscriptionBatch = json_object_new_array(); AFB_NOTICE ("Low-can plugin: label='%s' version='%s' info='%s'", plugin->label, @@ -76,46 +75,52 @@ CTLP_ONLOAD(plugin, bAppHandle) } CTLP_CAPI (subscribeToLow, source, argsJ, eventJ, context) { + lowCANCtxT *pluginCtx = (lowCANCtxT*)source->context; json_object* dependsArrayJ = nullptr, *subscribeArgsJ = nullptr, *subscribeFilterJ = nullptr, *responseJ = nullptr, *filterJ = nullptr; const char *id = nullptr, *event = nullptr, *unit = nullptr; double frequency = 0; int err = 0; - err = wrap_json_unpack(eventJ, "{ss,s?s,s?o,s?s,s?F,s?o !}", - "id", &id, - "event", &event, - "depends", &dependsArrayJ, - "unit", &unit, - "frequency", &frequency, - "getSignalsArgs", &filterJ); - if(err) + if(eventJ) { - AFB_ERROR("Problem to unpack JSON object eventJ: %s", - json_object_to_json_string(eventJ)); - return err; + err = wrap_json_unpack(eventJ, "{ss,s?s,s?o,s?s,s?F,s?o !}", + "id", &id, + "event", &event, + "depends", &dependsArrayJ, + "unit", &unit, + "frequency", &frequency, + "getSignalsArgs", &filterJ); + if(err) + { + AFB_ERROR("Problem to unpack JSON object eventJ: %s", + json_object_to_json_string(eventJ)); + return err; + } + + if(frequency > 0 && !filterJ) + {wrap_json_pack(&subscribeFilterJ, "{sf}", "frequency", frequency);} + else + {subscribeFilterJ = filterJ;} + + std::string eventStr = std::string(event); + std::string lowEvent = eventStr.substr(eventStr.find("/")+1); + err = wrap_json_pack(&subscribeArgsJ, "{ss, so*}", + "event", lowEvent.c_str(), + "filter", subscribeFilterJ); + if(err) + { + AFB_ERROR("Error building subscription query object"); + return err; + } + + json_object_array_add(pluginCtx->subscriptionBatch, subscribeArgsJ); } - - if(frequency > 0 && !filterJ) - {wrap_json_pack(&subscribeFilterJ, "{sf}", "frequency", frequency);} else - {subscribeFilterJ = filterJ;} - - std::string eventStr = std::string(event); - std::string lowEvent = eventStr.substr(eventStr.find("/")+1); - err = wrap_json_pack(&subscribeArgsJ, "{ss, so*}", - "event", lowEvent.c_str(), - "filter", subscribeFilterJ); - if(err) - { - AFB_ERROR("Error building subscription query object"); - return err; - } - AFB_DEBUG("Calling subscribe with %s", json_object_to_json_string_ext(subscribeArgsJ, JSON_C_TO_STRING_PRETTY)); - err = afb_service_call_sync("low-can", "subscribe", subscribeArgsJ, &responseJ); - if(err) { - AFB_ERROR("Subscribe to 'low-can/%s' %s", lowEvent.c_str(), json_object_to_json_string(responseJ)); - return err; + AFB_DEBUG("Calling subscribe with %s", json_object_to_json_string_ext(pluginCtx->subscriptionBatch, JSON_C_TO_STRING_PRETTY)); + err = afb_service_call_sync("low-can", "subscribe", pluginCtx->subscriptionBatch, &responseJ); + if(err) + {AFB_ERROR("Subscribe to '%s' responseJ:%s", json_object_to_json_string_ext(pluginCtx->subscriptionBatch, JSON_C_TO_STRING_PRETTY), json_object_to_json_string(responseJ));} } return err; -- cgit 1.2.3-korg