diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2017-08-30 18:13:58 +0200 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2017-09-02 12:18:23 +0200 |
commit | aaa16600841d71c3d2f7788d82752d0dcd83e251 (patch) | |
tree | 0692649815042b7dd17a9beb19f7a9900b87e22e | |
parent | a23e9412da1961e60b671bd26fdb6bdf8dd7a2f3 (diff) |
WIP loadconfig from client
Change-Id: I8f56cf5992dbc21c7637f20acbb05fa9436d647d
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r-- | high-can-binding/high-can-binding-hat.cpp | 1 | ||||
-rw-r--r-- | high-can-binding/high-can-binding-hat.hpp | 1 | ||||
-rw-r--r-- | high-can-binding/high-can-binding.cpp | 14 | ||||
-rw-r--r-- | high-can-binding/high.cpp | 28 | ||||
-rw-r--r-- | high-can-binding/high.hpp | 2 |
5 files changed, 42 insertions, 4 deletions
diff --git a/high-can-binding/high-can-binding-hat.cpp b/high-can-binding/high-can-binding-hat.cpp index 4a5e348..8c747d1 100644 --- a/high-can-binding/high-can-binding-hat.cpp +++ b/high-can-binding/high-can-binding-hat.cpp @@ -8,6 +8,7 @@ static const struct afb_verb_v2 verbs[]= { { .verb= "subscribe", .callback= subscribe, .auth = NULL, .info = "subscribe to an ViWi object", .session = 0 }, { .verb= "unsubscribe", .callback= unsubscribe, .auth = NULL, .info = "unsubscribe to a ViWi object", .session = 0 }, + { .verb= "load", .callback= load, .auth = NULL, .info = "Load Viwi service Configuration", .session = 0 }, { .verb= "get", .callback= get, .auth = NULL, .info = "Get informations about a resource or element", .session = 0 }, { .verb= NULL, .callback=NULL, .auth = NULL, .info = NULL, .session = 0 } }; diff --git a/high-can-binding/high-can-binding-hat.hpp b/high-can-binding/high-can-binding-hat.hpp index 28e8306..bb912c1 100644 --- a/high-can-binding/high-can-binding-hat.hpp +++ b/high-can-binding/high-can-binding-hat.hpp @@ -10,6 +10,7 @@ extern "C" void onEvent(const char *event, struct json_object *object); void subscribe(afb_req request); void unsubscribe(afb_req request); + void load(afb_req request); void get(afb_req request); void initHigh(); int ticked(sd_event_source *source, uint64_t t, void *data); diff --git a/high-can-binding/high-can-binding.cpp b/high-can-binding/high-can-binding.cpp index 4891f6c..27a49f2 100644 --- a/high-can-binding/high-can-binding.cpp +++ b/high-can-binding/high-can-binding.cpp @@ -44,11 +44,22 @@ void unsubscribe(afb_req request) afb_req_fail(request, "error", NULL); } +/// @brief verb that loads JSON configuration (old high.json file now) +void load(afb_req request) +{ + json_object* args = afb_req_json(request); + const char* confd; + + wrap_json_unpack(args, "{s:s}", "path", &confd); + high.parseConfigAndSubscribe(confd); +} + /// @brief entry point for get requests. Treatment itself is made in High class. void get(afb_req request) { json_object *jobj; - if(high.get(request, &jobj)) { + if(high.get(request, &jobj)) + { afb_req_success(request, jobj, NULL); } else { afb_req_fail(request, "error", NULL); @@ -68,7 +79,6 @@ int ticked(sd_event_source *source, uint64_t t, void* data) /// @param[in] service Structure which represent the Application Framework Binder. void initHigh() { - high.parseConfigAndSubscribe(); } diff --git a/high-can-binding/high.cpp b/high-can-binding/high.cpp index 63c9b49..9991059 100644 --- a/high-can-binding/high.cpp +++ b/high-can-binding/high.cpp @@ -48,8 +48,34 @@ High::High() /// @brief Reads the json configuration and generates accordingly the resources container. An UID is generated for each resource. /// Makes necessary subscriptions to low-level, eventually with a frequency. /// -void High::parseConfigAndSubscribe() +/// @param[in] confd - path to configuration directory which holds the binding configuration to load +/// +void High::parseConfigAndSubscribe(const std::string* confd) { + char *filename; + char*fullpath; + std::vector<std::string> conf_files_path; + struct json_object* conf_filesJ = ScanForConfig(confd, CTL_SCAN_FLAT, "viwi", "json"); + if (!conf_filesJ || json_object_array_length(conf_filesJ) == 0) + { + AFB_ERROR("No JSON config files found in %s", confd); + return; + } + + for(int i=0; i < json_object_array_length(conf_filesJ); i++) + { + json_object *entryJ=json_object_array_get_idx(conf_filesJ, i); + + err= wrap_json_unpack (entryJ, "{s:s, s:s !}", "fullpath", &fullpath,"filename", &filename); + if (err) { + AFB_ERROR ("OOOPs invalid config file path = %s", json_object_get_string(entryJ)); + return; + } + std::string filepath = fullpath; + filepath += filename; + conf_files_path.push_back(filepath); + } + json_object *config = json_object_from_file("high.json"); json_object *jvalue, *jarray1, *jarray2, *obj; std::map<std::string, std::map<std::string, Property>> properties; diff --git a/high-can-binding/high.hpp b/high-can-binding/high.hpp index a4c6683..7ccc87d 100644 --- a/high-can-binding/high.hpp +++ b/high-can-binding/high.hpp @@ -45,7 +45,7 @@ public: void tick(sd_event_source *source, const long &now, void *interv); void startTimer(const int &t); ~High(); - void parseConfigAndSubscribe(); + void parseConfigAndSubscribe(const std::string& confd); static bool startsWith(const std::string &s, const std::string &val); static void callBackFromSubscribe(void *handle, int iserror, json_object *result); private: |