diff --git a/docs/api/tests-api.md b/docs/api/tests-api.md index 57ee4458..3ab850ae 100644 --- a/docs/api/tests-api.md +++ b/docs/api/tests-api.md @@ -47,10 +47,6 @@ Name is missing. ./usr/bin/ubus call domo.outlets create "{\"name\":\"test\", \"sender\": 12797322}" ``` - - -TODO - ### list ``` @@ -112,7 +108,7 @@ TODO ### update ``` -./usr/bin/ubus call domo.outlets update +./usr/bin/ubus call domo.outlets update "{\"id\": 4, \"name\":\"test1\", \"sender\": 1, \"switch\": 2}" ``` TODO diff --git a/src/prog/domod/src/devices/devices-manager.cpp b/src/prog/domod/src/devices/devices-manager.cpp index a12deeba..830bd2d1 100644 --- a/src/prog/domod/src/devices/devices-manager.cpp +++ b/src/prog/domod/src/devices/devices-manager.cpp @@ -249,6 +249,34 @@ int DevicesManager::create(const std::string &a_capability, struct json_object * return 0; } +/*! ---------------------------------------------------------------------------- + * @fn update + * + * @brief set the state for device with the id from a specific capability. + */ +int DevicesManager::update(const std::string &a_capability, struct json_object *a_node) +{ + int32_t the_id = -1; + struct json_object *the_value_node; + + // id + if (json_object_object_get_ex(a_node, k_entry_id, &the_value_node)) + { + the_id = json_object_get_int(the_value_node); + } + + // Sanity checks. + if (the_id == -1) + return -1; + + if (a_capability == kOutletEntry) + { + return update_outlet(the_id, a_node); + } + + return 0; +} + /*! ---------------------------------------------------------------------------- * @fn load_outlets * @@ -300,6 +328,22 @@ int DevicesManager::create_outlet(struct json_object *a_node) return 0; } +/*! ---------------------------------------------------------------------------- + * @fn update_outlet + * + * @brief update a new outlet Object From a Json Node. + */ +int DevicesManager::update_outlet(int32_t an_id, struct json_object *a_node) +{ + int the_pos; + + the_pos = m_outlets.find(an_id); + if (the_pos == -1) + return -2; + + return m_outlets[the_pos]->from_json(a_node); +} + /*! ---------------------------------------------------------------------------- * @fn load_shutters * diff --git a/src/prog/domod/src/devices/devices-manager.h b/src/prog/domod/src/devices/devices-manager.h index 04d80962..f0ef142b 100644 --- a/src/prog/domod/src/devices/devices-manager.h +++ b/src/prog/domod/src/devices/devices-manager.h @@ -58,11 +58,13 @@ class DevicesManager int set_state(const std::string &a_capability, int an_id, bool a_state); int create(const std::string &a_capability, struct json_object *a_node); + int update(const std::string &a_capability, struct json_object *a_node); private: int load_outlets(struct json_object *a_node); int create_outlet(struct json_object *a_node); - + int update_outlet(int32_t an_id, struct json_object *a_node); + int load_shutters(struct json_object *a_node); int load_sprinklers(struct json_object *a_node); diff --git a/src/prog/domod/src/ubus/outlets-controller.cpp b/src/prog/domod/src/ubus/outlets-controller.cpp index 4d54cb34..5e09ad39 100644 --- a/src/prog/domod/src/ubus/outlets-controller.cpp +++ b/src/prog/domod/src/ubus/outlets-controller.cpp @@ -162,10 +162,9 @@ int OutletsController::read(struct ubus_context *a_ctx, struct ubus_request_data */ int OutletsController::update(struct ubus_context *a_ctx, struct ubus_request_data *a_req, struct blob_attr *a_msg) { - int the_result = 0; - struct blob_buf the_buf = {0}; + int16_t the_id = -1; char *the_string; - struct json_object *the_root_node, *the_output_node; + struct json_object *the_root_node; printf("%s\n", __PRETTY_FUNCTION__); the_string = blobmsg_format_json(a_msg, true); @@ -179,28 +178,15 @@ int OutletsController::update(struct ubus_context *a_ctx, struct ubus_request_da return UBUS_STATUS_INVALID_ARGUMENT; } - if (m_devices_manager->set(kOutletEntry, the_root_node) < 0) + if (m_devices_manager->update(kOutletEntry, the_root_node) < 0) { - fprintf(stderr, "Failed to set the new state.\n"); + fprintf(stderr, "Failed to update the state (%d).\n", the_id); return UBUS_STATUS_INVALID_ARGUMENT; } - // Create the output node. - the_output_node = json_object_new_object(); - json_object_object_add(the_output_node, kOutletEntry, the_root_node); - - blob_buf_init(&the_buf, 0); - - blobmsg_add_json_from_string(&the_buf, json_object_to_json_string(the_output_node)); - - the_result = ubus_send_reply(a_ctx, a_req, the_buf.head); - - blob_buf_free(&the_buf); - json_object_put(the_root_node); - json_object_put(the_output_node); - return the_result; + return 0; } /*! ----------------------------------------------------------------------------