add update outlet

This commit is contained in:
jbnadal
2018-05-07 14:47:01 +02:00
parent 45b6ed9d73
commit 194e654d66
4 changed files with 53 additions and 25 deletions

View File

@@ -47,10 +47,6 @@ Name is missing.
./usr/bin/ubus call domo.outlets create "{\"name\":\"test\", \"sender\": 12797322}" ./usr/bin/ubus call domo.outlets create "{\"name\":\"test\", \"sender\": 12797322}"
``` ```
TODO
### list ### list
``` ```
@@ -112,7 +108,7 @@ TODO
### update ### update
``` ```
./usr/bin/ubus call domo.outlets update ./usr/bin/ubus call domo.outlets update "{\"id\": 4, \"name\":\"test1\", \"sender\": 1, \"switch\": 2}"
``` ```
TODO TODO

View File

@@ -249,6 +249,34 @@ int DevicesManager::create(const std::string &a_capability, struct json_object *
return 0; 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 * @fn load_outlets
* *
@@ -300,6 +328,22 @@ int DevicesManager::create_outlet(struct json_object *a_node)
return 0; 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 * @fn load_shutters
* *

View File

@@ -58,10 +58,12 @@ class DevicesManager
int set_state(const std::string &a_capability, int an_id, bool a_state); 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 create(const std::string &a_capability, struct json_object *a_node);
int update(const std::string &a_capability, struct json_object *a_node);
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 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_shutters(struct json_object *a_node);
int load_sprinklers(struct json_object *a_node); int load_sprinklers(struct json_object *a_node);

View File

@@ -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 OutletsController::update(struct ubus_context *a_ctx, struct ubus_request_data *a_req, struct blob_attr *a_msg)
{ {
int the_result = 0; int16_t the_id = -1;
struct blob_buf the_buf = {0};
char *the_string; char *the_string;
struct json_object *the_root_node, *the_output_node; struct json_object *the_root_node;
printf("%s\n", __PRETTY_FUNCTION__); printf("%s\n", __PRETTY_FUNCTION__);
the_string = blobmsg_format_json(a_msg, true); 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; 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; 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_root_node);
json_object_put(the_output_node);
return the_result; return 0;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------