add delete action on device

This commit is contained in:
jbnadal
2018-05-07 16:16:04 +02:00
parent 194e654d66
commit 3534ff1107
6 changed files with 146 additions and 55 deletions

View File

@@ -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<Device>(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<Device>(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 <std::unique_ptr<Device>>::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 <std::unique_ptr<Device>>::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();
}
}
}

View File

@@ -28,8 +28,8 @@
/*------------------------------- INCLUDES ----------------------------------*/
#include <map>
#include <memory>
#include <vector>
/*------------------------------ DEPENDENCIES -------------------------------*/
@@ -43,18 +43,24 @@ class DeviceCollection
DeviceCollection(void);
~DeviceCollection(void);
Device *operator[](int a_pos);
public:
typedef std::map<uint32_t, std::unique_ptr<Device>> 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 <std::unique_ptr<Device>> m_devices;
void update_max_id(void);
DeviceContainer m_devices;
uint16_t m_max_id;
};

View File

@@ -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
*

View File

@@ -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);

View File

@@ -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;
}