From 0769ea71247396bfdac0dfbb56477c83b9e60de6 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Mon, 9 Apr 2018 18:42:49 +0200 Subject: Add objects directly from JSON argument There was only possible to add objects at runtime using another JSON file located somewhere in the filesystem. Now, it is also possible to load directly from a JSON passed in the request. Change-Id: I77ab639593ecb3d73239244d11ee3da599caf1b4 Signed-off-by: Romain Forlot --- .../signal-composer-binding.cpp | 25 ++++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'signal-composer-binding') diff --git a/signal-composer-binding/signal-composer-binding.cpp b/signal-composer-binding/signal-composer-binding.cpp index c7ca567..fb99c89 100644 --- a/signal-composer-binding/signal-composer-binding.cpp +++ b/signal-composer-binding/signal-composer-binding.cpp @@ -168,31 +168,38 @@ void unsubscribe(afb_req request) } /// @brief verb that loads JSON configuration (old SigComp.json file now) + void addObjects(afb_req request) { Composer& composer = Composer::instance(); json_object *sourcesJ = nullptr, - *signalsJ = nullptr; + *signalsJ = nullptr, + *objectsJ = nullptr; const char* filepath = afb_req_value(request, "file"); if(filepath) { - json_object *fileJ = json_object_from_file(filepath); - if(!fileJ) + objectsJ = json_object_from_file(filepath); + if(!objectsJ) { json_object* responseJ = ScanForConfig(CONTROL_CONFIG_PATH, CTL_SCAN_RECURSIVE, filepath, ".json"); filepath = ConfigSearch(nullptr, responseJ); if(filepath) - {fileJ = json_object_from_file(filepath);} + {objectsJ = json_object_from_file(filepath);} } + } + else + {objectsJ = afb_req_json(request);} - json_object_object_get_ex(fileJ, "sources", &sourcesJ); - json_object_object_get_ex(fileJ, "signals", &signalsJ); + if(objectsJ) + { + json_object_object_get_ex(objectsJ, "sources", &sourcesJ); + json_object_object_get_ex(objectsJ, "signals", &signalsJ); if( sourcesJ && composer.loadSources(sourcesJ)) { afb_req_fail_f(request, "Loading 'sources' configuration or subscription error", "Error code: -2"); - json_object_put(fileJ); + json_object_put(objectsJ); return; } if(signalsJ) @@ -202,11 +209,11 @@ void addObjects(afb_req request) else { afb_req_fail_f(request, "Loading 'signals' configuration or subscription error", "Error code: -2"); - json_object_put(fileJ); + json_object_put(objectsJ); return; } } - json_object_put(fileJ); + json_object_put(objectsJ); afb_req_success(request, NULL, NULL); } else -- cgit 1.2.3-korg