From 45b6ed9d73ebed58f4600846b5bf8e818c83a4ee Mon Sep 17 00:00:00 2001 From: jbnadal Date: Mon, 7 May 2018 12:49:27 +0200 Subject: [PATCH] add create outlet --- docs/api/tests-api.md | 12 +++++++++ src/prog/domod/src/devices/device.cpp | 27 ++++++++++++++++--- src/prog/domod/src/devices/device.h | 1 + .../domod/src/devices/devices-collection.cpp | 10 +++++++ .../domod/src/devices/devices-collection.h | 2 ++ .../domod/src/devices/devices-manager.cpp | 27 +++++++++++++++++++ src/prog/domod/src/devices/devices-manager.h | 2 ++ src/prog/domod/src/devices/outlet-dio.cpp | 16 ++++++++--- 8 files changed, 91 insertions(+), 6 deletions(-) diff --git a/docs/api/tests-api.md b/docs/api/tests-api.md index b46adbc3..57ee4458 100644 --- a/docs/api/tests-api.md +++ b/docs/api/tests-api.md @@ -37,6 +37,18 @@ ./usr/bin/ubus call domo.outlets create "{\"name\":\"test\", \"sender\": 12797322, \"switch\": 4}" ``` +#### create failed 1 + +Name is missing. + +``` +./usr/bin/ubus call domo.outlets create "{\"sender\": 12797322, \"switch\": 4}" +./usr/bin/ubus call domo.outlets create "{\"name\":\"test\", \"switch\": 4}" +./usr/bin/ubus call domo.outlets create "{\"name\":\"test\", \"sender\": 12797322}" +``` + + + TODO ### list diff --git a/src/prog/domod/src/devices/device.cpp b/src/prog/domod/src/devices/device.cpp index 1ad4144f..701dc1e6 100644 --- a/src/prog/domod/src/devices/device.cpp +++ b/src/prog/domod/src/devices/device.cpp @@ -49,6 +49,17 @@ Device::~Device(void) { } +/*! ---------------------------------------------------------------------------- + * @fn set_id + * + * @brief Set the ID of the device. + */ + +void Device::set_id(uint16_t an_id) +{ + m_id = an_id; +} + /*! ---------------------------------------------------------------------------- * @fn get_id * @@ -64,15 +75,18 @@ uint16_t Device::get_id(void) * @fn from_json * * @brief Load Device Object From a Json Object. + * return if all the mandatory name is available. */ int Device::from_json(struct json_object *a_node) { + int16_t the_id = -1; struct json_object *the_value_node; // TODO: Should shared with Light. // id if (json_object_object_get_ex(a_node, k_entry_id, &the_value_node)) { - m_id = json_object_get_int(the_value_node); + the_id = json_object_get_int(the_value_node); + m_id = the_id; } // name if (json_object_object_get_ex(a_node, k_entry_name, &the_value_node)) @@ -90,6 +104,12 @@ int Device::from_json(struct json_object *a_node) m_state = json_object_get_boolean(the_value_node); } + // Sanity checks + if (m_name.empty()) + { + return -1; + } + return 0; } @@ -120,8 +140,9 @@ int Device::to_json(struct json_object *a_node) const json_object_object_add(a_node, k_entry_id, json_object_new_int(m_id)); // name json_object_object_add(a_node, k_entry_name, json_object_new_string(m_name.c_str())); - // speach_name - json_object_object_add(a_node, k_entry_speech_name, json_object_new_string(m_speech_name.c_str())); + // speech_name + if (!m_speech_name.empty()) + json_object_object_add(a_node, k_entry_speech_name, json_object_new_string(m_speech_name.c_str())); // state json_object_object_add(a_node, k_entry_state, json_object_new_boolean(m_state)); diff --git a/src/prog/domod/src/devices/device.h b/src/prog/domod/src/devices/device.h index 5eedeb95..97271fee 100644 --- a/src/prog/domod/src/devices/device.h +++ b/src/prog/domod/src/devices/device.h @@ -42,6 +42,7 @@ class Device Device(void); virtual ~Device(void); + void set_id(uint16_t an_id); uint16_t get_id(void); virtual int from_json(struct json_object *a_node); diff --git a/src/prog/domod/src/devices/devices-collection.cpp b/src/prog/domod/src/devices/devices-collection.cpp index 0d859d4c..06d5c9a2 100644 --- a/src/prog/domod/src/devices/devices-collection.cpp +++ b/src/prog/domod/src/devices/devices-collection.cpp @@ -100,6 +100,16 @@ int DeviceCollection::find(int a_pos) return the_status; } +/*! ---------------------------------------------------------------------------- + * @fn get_new_id + * + * @brief return a new id for the collection. + */ +uint16_t DeviceCollection::get_new_id(void) +{ + return m_max_id + 1; +} + /*! ---------------------------------------------------------------------------- * @fn to_json * diff --git a/src/prog/domod/src/devices/devices-collection.h b/src/prog/domod/src/devices/devices-collection.h index 79fb91bd..02c185ae 100644 --- a/src/prog/domod/src/devices/devices-collection.h +++ b/src/prog/domod/src/devices/devices-collection.h @@ -49,6 +49,8 @@ class DeviceCollection int find(int a_pos); + uint16_t get_new_id(void); + struct json_object *to_json(void); private: diff --git a/src/prog/domod/src/devices/devices-manager.cpp b/src/prog/domod/src/devices/devices-manager.cpp index e6182d8e..a12deeba 100644 --- a/src/prog/domod/src/devices/devices-manager.cpp +++ b/src/prog/domod/src/devices/devices-manager.cpp @@ -241,6 +241,11 @@ int DevicesManager::set_state(const std::string &a_capability, int an_id, bool a */ int DevicesManager::create(const std::string &a_capability, struct json_object *a_node) { + if (a_capability == kOutletEntry) + { + return create_outlet(a_node); + } + return 0; } @@ -273,6 +278,28 @@ int DevicesManager::load_outlets(struct json_object *a_node) return 0; } +/*! ---------------------------------------------------------------------------- + * @fn create_outlet + * + * @brief create a new outlet Object From a Json Node. + */ +int DevicesManager::create_outlet(struct json_object *a_node) +{ + OutletDio* the_outlet = new OutletDio; + + if (the_outlet->from_json(a_node) != 0) + { + delete the_outlet; + return -1; + } + + the_outlet->set_id(m_outlets.get_new_id()); + + m_outlets.add(the_outlet); + + return 0; +} + /*! ---------------------------------------------------------------------------- * @fn load_shutters * diff --git a/src/prog/domod/src/devices/devices-manager.h b/src/prog/domod/src/devices/devices-manager.h index e7c91430..04d80962 100644 --- a/src/prog/domod/src/devices/devices-manager.h +++ b/src/prog/domod/src/devices/devices-manager.h @@ -61,6 +61,8 @@ class DevicesManager private: int load_outlets(struct json_object *a_node); + int create_outlet(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/devices/outlet-dio.cpp b/src/prog/domod/src/devices/outlet-dio.cpp index f028335f..f9a20ce1 100644 --- a/src/prog/domod/src/devices/outlet-dio.cpp +++ b/src/prog/domod/src/devices/outlet-dio.cpp @@ -63,6 +63,8 @@ int OutletDio::from_json(struct json_object *a_node) { int the_result; struct json_object *the_value_node; + int32_t the_sender = -1; + int32_t the_switch = -1; the_result = Device::from_json(a_node); if (the_result < 0) @@ -76,14 +78,20 @@ int OutletDio::from_json(struct json_object *a_node) // sender if (json_object_object_get_ex(a_node, k_entry_sender, &the_value_node)) { - m_sender = json_object_get_int(the_value_node); + the_sender = json_object_get_int(the_value_node); + m_sender = the_sender; } // switch if (json_object_object_get_ex(a_node, k_entry_switch, &the_value_node)) { - m_switch = json_object_get_int(the_value_node); + the_switch = json_object_get_int(the_value_node); + m_switch = the_switch; } + // Sanity checks. + if ((the_switch == -1) || (the_sender == -1)) + return -1; + return 0; } @@ -114,7 +122,9 @@ int OutletDio::to_json(struct json_object *a_node) const Device::to_json(a_node); // zone - json_object_object_add(a_node, k_entry_zone, json_object_new_string(m_zone.c_str())); + if (!m_zone.empty()) + json_object_object_add(a_node, k_entry_zone, json_object_new_string(m_zone.c_str())); + // sender json_object_object_add(a_node, k_entry_sender, json_object_new_int(m_sender)); // switch