add create outlet

This commit is contained in:
jbnadal
2018-05-07 12:49:27 +02:00
parent 10bfa7ac93
commit 45b6ed9d73
8 changed files with 91 additions and 6 deletions

View File

@@ -37,6 +37,18 @@
./usr/bin/ubus call domo.outlets create "{\"name\":\"test\", \"sender\": 12797322, \"switch\": 4}" ./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 TODO
### list ### list

View File

@@ -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 * @fn get_id
* *
@@ -64,15 +75,18 @@ uint16_t Device::get_id(void)
* @fn from_json * @fn from_json
* *
* @brief Load Device Object From a Json Object. * @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) int Device::from_json(struct json_object *a_node)
{ {
int16_t the_id = -1;
struct json_object *the_value_node; struct json_object *the_value_node;
// TODO: Should shared with Light. // TODO: Should shared with Light.
// id // id
if (json_object_object_get_ex(a_node, k_entry_id, &the_value_node)) 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 // name
if (json_object_object_get_ex(a_node, k_entry_name, &the_value_node)) 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); m_state = json_object_get_boolean(the_value_node);
} }
// Sanity checks
if (m_name.empty())
{
return -1;
}
return 0; 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)); json_object_object_add(a_node, k_entry_id, json_object_new_int(m_id));
// name // name
json_object_object_add(a_node, k_entry_name, json_object_new_string(m_name.c_str())); json_object_object_add(a_node, k_entry_name, json_object_new_string(m_name.c_str()));
// speach_name // speech_name
json_object_object_add(a_node, k_entry_speech_name, json_object_new_string(m_speech_name.c_str())); 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 // state
json_object_object_add(a_node, k_entry_state, json_object_new_boolean(m_state)); json_object_object_add(a_node, k_entry_state, json_object_new_boolean(m_state));

View File

@@ -42,6 +42,7 @@ class Device
Device(void); Device(void);
virtual ~Device(void); virtual ~Device(void);
void set_id(uint16_t an_id);
uint16_t get_id(void); uint16_t get_id(void);
virtual int from_json(struct json_object *a_node); virtual int from_json(struct json_object *a_node);

View File

@@ -100,6 +100,16 @@ int DeviceCollection::find(int a_pos)
return the_status; 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 * @fn to_json
* *

View File

@@ -49,6 +49,8 @@ class DeviceCollection
int find(int a_pos); int find(int a_pos);
uint16_t get_new_id(void);
struct json_object *to_json(void); struct json_object *to_json(void);
private: private:

View File

@@ -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) int DevicesManager::create(const std::string &a_capability, struct json_object *a_node)
{ {
if (a_capability == kOutletEntry)
{
return create_outlet(a_node);
}
return 0; return 0;
} }
@@ -273,6 +278,28 @@ int DevicesManager::load_outlets(struct json_object *a_node)
return 0; 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 * @fn load_shutters
* *

View File

@@ -61,6 +61,8 @@ class DevicesManager
private: private:
int load_outlets(struct json_object *a_node); 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_shutters(struct json_object *a_node);
int load_sprinklers(struct json_object *a_node); int load_sprinklers(struct json_object *a_node);

View File

@@ -63,6 +63,8 @@ int OutletDio::from_json(struct json_object *a_node)
{ {
int the_result; int the_result;
struct json_object *the_value_node; struct json_object *the_value_node;
int32_t the_sender = -1;
int32_t the_switch = -1;
the_result = Device::from_json(a_node); the_result = Device::from_json(a_node);
if (the_result < 0) if (the_result < 0)
@@ -76,14 +78,20 @@ int OutletDio::from_json(struct json_object *a_node)
// sender // sender
if (json_object_object_get_ex(a_node, k_entry_sender, &the_value_node)) 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 // switch
if (json_object_object_get_ex(a_node, k_entry_switch, &the_value_node)) 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; return 0;
} }
@@ -114,7 +122,9 @@ int OutletDio::to_json(struct json_object *a_node) const
Device::to_json(a_node); Device::to_json(a_node);
// zone // 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 // sender
json_object_object_add(a_node, k_entry_sender, json_object_new_int(m_sender)); json_object_object_add(a_node, k_entry_sender, json_object_new_int(m_sender));
// switch // switch