From 3534ff1107ae0227bafe7adb445c701098b9ee9f Mon Sep 17 00:00:00 2001 From: jbnadal Date: Mon, 7 May 2018 16:16:04 +0200 Subject: [PATCH] add delete action on device --- docs/api/tests-api.md | 6 +- .../domod/src/devices/devices-collection.cpp | 91 +++++++++++++++---- .../domod/src/devices/devices-collection.h | 16 +++- .../domod/src/devices/devices-manager.cpp | 59 +++++++----- src/prog/domod/src/devices/devices-manager.h | 2 +- .../domod/src/ubus/outlets-controller.cpp | 27 +++++- 6 files changed, 146 insertions(+), 55 deletions(-) diff --git a/docs/api/tests-api.md b/docs/api/tests-api.md index 3ab850ae..398f6421 100644 --- a/docs/api/tests-api.md +++ b/docs/api/tests-api.md @@ -111,16 +111,12 @@ Name is missing. ./usr/bin/ubus call domo.outlets update "{\"id\": 4, \"name\":\"test1\", \"sender\": 1, \"switch\": 2}" ``` -TODO - ### delete ``` -./usr/bin/ubus call domo.outlets delete +./usr/bin/ubus call domo.outlets delete "{\"id\": 4}" ``` -TODO - ## domo.sequences ### create diff --git a/src/prog/domod/src/devices/devices-collection.cpp b/src/prog/domod/src/devices/devices-collection.cpp index 06d5c9a2..22e3a13c 100644 --- a/src/prog/domod/src/devices/devices-collection.cpp +++ b/src/prog/domod/src/devices/devices-collection.cpp @@ -54,9 +54,17 @@ DeviceCollection::~DeviceCollection(void) * * @brief to access to a specific light object. */ -Device *DeviceCollection::operator[](int a_pos) +Device *DeviceCollection::operator[](int an_id) { - return m_devices[a_pos].get(); + DeviceIterator the_it; + + the_it = m_devices.find(an_id); + if (the_it == m_devices.end()) + { + return NULL; + } + + return the_it->second.get(); } /*! ---------------------------------------------------------------------------- @@ -66,7 +74,17 @@ Device *DeviceCollection::operator[](int a_pos) */ int DeviceCollection::add(Device *a_device) { - m_devices.push_back(std::unique_ptr(a_device)); + DeviceIterator the_it; + + // Sanity Checks. + the_it = m_devices.find(a_device->get_id()); + if (the_it != m_devices.end()) + { + return -1; + } + + // Add the Device. + m_devices[a_device->get_id()] = std::unique_ptr(a_device); if (a_device->get_id() > m_max_id) { @@ -77,29 +95,47 @@ int DeviceCollection::add(Device *a_device) } /*! ---------------------------------------------------------------------------- - * @fn find + * @fn add * - * @brief find a device in the collection. + * @brief remove a device to the collection. */ -int DeviceCollection::find(int a_pos) +int DeviceCollection::remove(int32_t an_id) { - std::vector >::iterator the_it; - int the_status = -1; - int the_pos = 0; + DeviceIterator the_it; - for (the_it = m_devices.begin(); the_it != m_devices.end(); the_it++) + // Sanity Checks. + the_it = m_devices.find(an_id); + if (the_it == m_devices.end()) { - if ((*the_it).get()->get_id() == a_pos) - { - return the_pos; - } + return -1; + } + m_devices.erase(the_it); - the_pos++; + update_max_id(); + + return 0; +} + +/*! ---------------------------------------------------------------------------- + * @fn update + * + * @brief update a device to the collection. + */ +int DeviceCollection::update(int32_t an_id, struct json_object *a_node) +{ + DeviceIterator the_it; + + // Check if the key exist. + the_it = m_devices.find(an_id); + if (the_it == m_devices.end()) + { + return -2; } - return the_status; + return the_it->second.get()->from_json(a_node); } + /*! ---------------------------------------------------------------------------- * @fn get_new_id * @@ -118,14 +154,33 @@ uint16_t DeviceCollection::get_new_id(void) struct json_object *DeviceCollection::to_json(void) { struct json_object *the_root_node; - std::vector >::iterator the_it; + DeviceIterator the_it; the_root_node = json_object_new_array(); for (the_it = m_devices.begin(); the_it != m_devices.end(); the_it++) { - json_object_array_add(the_root_node, (*the_it).get()->to_json()); + json_object_array_add(the_root_node, the_it->second.get()->to_json()); } return the_root_node; } + +/*! ---------------------------------------------------------------------------- + * @fn to_json + * + * @brief Export the device collection as JSON Object. + */ +void DeviceCollection::update_max_id(void) +{ + DeviceIterator the_it; + m_max_id = 0; + + for (the_it = m_devices.begin(); the_it != m_devices.end(); the_it++) + { + if (m_max_id < the_it->second.get()->get_id()) + { + m_max_id = the_it->second.get()->get_id(); + } + } +} diff --git a/src/prog/domod/src/devices/devices-collection.h b/src/prog/domod/src/devices/devices-collection.h index 02c185ae..ebfef49f 100644 --- a/src/prog/domod/src/devices/devices-collection.h +++ b/src/prog/domod/src/devices/devices-collection.h @@ -28,8 +28,8 @@ /*------------------------------- INCLUDES ----------------------------------*/ +#include #include -#include /*------------------------------ DEPENDENCIES -------------------------------*/ @@ -43,18 +43,24 @@ class DeviceCollection DeviceCollection(void); ~DeviceCollection(void); - Device *operator[](int a_pos); +public: + typedef std::map> DeviceContainer; + typedef DeviceContainer::iterator DeviceIterator; + + Device *operator[](int a_an_id); int add(Device *a_device); - - int find(int a_pos); + int remove(int32_t an_id); + int update(int32_t an_id, struct json_object *a_node); uint16_t get_new_id(void); struct json_object *to_json(void); private: - std::vector > m_devices; + void update_max_id(void); + + DeviceContainer m_devices; uint16_t m_max_id; }; diff --git a/src/prog/domod/src/devices/devices-manager.cpp b/src/prog/domod/src/devices/devices-manager.cpp index 830bd2d1..8421b71b 100644 --- a/src/prog/domod/src/devices/devices-manager.cpp +++ b/src/prog/domod/src/devices/devices-manager.cpp @@ -162,15 +162,16 @@ std::string DevicesManager::get(const std::string &a_capability) std::string DevicesManager::get(const std::string &a_capability, uint32_t an_id) { std::string the_output; - int the_pos; + Device *the_device = NULL; if (a_capability == kOutletEntry) { - the_pos = m_outlets.find(an_id); - if (the_pos != -1) - { - the_output = json_object_to_json_string(m_outlets[the_pos]->to_json()); - } + the_device = m_outlets[an_id]; + } + + if (the_device != NULL) + { + the_output = json_object_to_json_string(the_device->to_json()); } return the_output; @@ -271,7 +272,35 @@ int DevicesManager::update(const std::string &a_capability, struct json_object * if (a_capability == kOutletEntry) { - return update_outlet(the_id, a_node); + return m_outlets.update(the_id, a_node); + } + + return 0; +} + +/*! ---------------------------------------------------------------------------- + * @fn del + * + * @brief delete the device with the id from a specific capability. + */ +int DevicesManager::del(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 m_outlets.remove(the_id); } return 0; @@ -328,22 +357,6 @@ 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 f0ef142b..c3d26d1f 100644 --- a/src/prog/domod/src/devices/devices-manager.h +++ b/src/prog/domod/src/devices/devices-manager.h @@ -59,11 +59,11 @@ class DevicesManager int create(const std::string &a_capability, struct json_object *a_node); int update(const std::string &a_capability, struct json_object *a_node); + int del(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 5e09ad39..fe70604f 100644 --- a/src/prog/domod/src/ubus/outlets-controller.cpp +++ b/src/prog/domod/src/ubus/outlets-controller.cpp @@ -162,7 +162,6 @@ 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) { - int16_t the_id = -1; char *the_string; struct json_object *the_root_node; printf("%s\n", __PRETTY_FUNCTION__); @@ -180,7 +179,7 @@ int OutletsController::update(struct ubus_context *a_ctx, struct ubus_request_da if (m_devices_manager->update(kOutletEntry, the_root_node) < 0) { - fprintf(stderr, "Failed to update the state (%d).\n", the_id); + fprintf(stderr, "Failed to update the state.\n"); return UBUS_STATUS_INVALID_ARGUMENT; } @@ -194,8 +193,30 @@ int OutletsController::update(struct ubus_context *a_ctx, struct ubus_request_da * * @brief delete a specific outlet. */ -int OutletsController::del(struct ubus_context *, struct ubus_request_data *, struct blob_attr *) +int OutletsController::del(struct ubus_context *, struct ubus_request_data *, struct blob_attr *a_msg) { + char *the_string; + struct json_object *the_root_node; printf("%s\n", __PRETTY_FUNCTION__); + + the_string = blobmsg_format_json(a_msg, true); + + the_root_node = json_tokener_parse(the_string); + free(the_string); + + if (the_root_node == NULL) + { + fprintf(stderr, "Failed parse the parameters.\n"); + return UBUS_STATUS_INVALID_ARGUMENT; + } + + if (m_devices_manager->del(kOutletEntry, the_root_node) < 0) + { + fprintf(stderr, "Failed to update the state.\n"); + return UBUS_STATUS_INVALID_ARGUMENT; + } + + json_object_put(the_root_node); + return 0; }