diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2016-11-28 14:15:56 +0000 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2016-11-28 14:15:56 +0000 |
commit | 78f856596ddd97870c979b7aa0c28db911d520f5 (patch) | |
tree | 81b6580185aa3fda3674fe11894b316a8297dd40 | |
parent | 0ead25e3bc380af870ec34381b346771122c9d4e (diff) |
Finalize 'set' method
Change-Id: I34f07b65a70b07ff5afc993efb1f2fad6d90d0cb
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r-- | hvac-hybrid-qml-binding.c | 79 |
1 files changed, 43 insertions, 36 deletions
diff --git a/hvac-hybrid-qml-binding.c b/hvac-hybrid-qml-binding.c index 369fb1e..b813d8a 100644 --- a/hvac-hybrid-qml-binding.c +++ b/hvac-hybrid-qml-binding.c @@ -262,60 +262,67 @@ static void get_temp_left_zone(struct afb_req request) } /* - * @brief Set a component value using a json object retrieved from request + * @brief Read all values * * @param struct afb_req : an afb request structure * */ -static void set(struct afb_req request) +static void get_all(struct afb_req request) { - struct can_handler ch; - const char *comp, *val; + json_object *ret_json; - comp = afb_req_value(request, "component"); - val = afb_req_value(request, "value"); + ret_json = json_object_new_object(); + json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(read_temp_left_zone())); + json_object_object_add(ret_json, "RightTemperature", json_object_new_int(read_temp_right_zone())); + json_object_object_add(ret_json, "FanSpeed", json_object_new_int(read_fanspeed())); - if (comp == NULL || val == NULL) - { - afb_req_fail_f(request, "failure", "ERROR: You have to send a json object with attribute 'component' and 'value'.\nSend again please choosing between following components: LeftTemperature, RightTemperature, FanSpeed\ncomponent : %s\n val: %s", comp, val); - return; - } + afb_req_success(request, ret_json, NULL); +} +/* + * @brief Set a component value using a json object retrieved from request + * + * @param struct afb_req : an afb request structure + * + */ +static void set(struct afb_req request) +{ + struct can_handler ch; + const char *val; + struct json_object *query; + struct json_object_iterator iter; + struct json_object_iterator iter_end; + + query = afb_req_json(request); + iter = json_object_iter_begin(query); + iter_end = json_object_iter_end(query); + + /* + * Loop over the json object that will set every component + * which it will find in it. + */ int i; - for (i=0;i<8;i++) + while(!json_object_iter_equal(&iter, &iter_end)) { - if(strcmp(can_hvac_components[i], comp) == 0) + const char *key = json_object_iter_peek_name(&iter); + for (i=0;i<8;i++) { - uint8_t comp_val = make_atoi(val); - can_hvac_values[i] = comp_val; - break; + if(strcmp(can_hvac_components[i], key) == 0) + { + val = afb_req_value(request, key); + uint8_t comp_val = make_atoi(val); + can_hvac_values[i] = comp_val; + break; + } } + json_object_iter_next(&iter); } ch = open_can_dev(); write_can(ch); close_can_dev(ch); - json_object *query = afb_req_json(request); - afb_req_success_f(request, NULL, "Set the %s=%d query=%s", can_hvac_components[i], can_hvac_values[i], json_object_to_json_string(query)); -} - -/* - * @brief Read all values - * - * @param struct afb_req : an afb request structure - * - */ -static void get_all(struct afb_req request) -{ - json_object *ret_json; - - ret_json = json_object_new_object(); - json_object_object_add(ret_json, "LeftTemperature", json_object_new_int(read_temp_left_zone())); - json_object_object_add(ret_json, "RightTemperature", json_object_new_int(read_temp_right_zone())); - json_object_object_add(ret_json, "FanSpeed", json_object_new_int(read_fanspeed())); - - afb_req_success_f(request, ret_json, NULL); + afb_req_success(request, query, "HVAC settings updated"); } // TODO: Have to change session management flag to AFB_SESSION_CHECK to use token auth |