WIP add light get and list rest apliant.

This commit is contained in:
jbnadal
2018-05-04 10:11:44 +02:00
parent d2613350fc
commit f6e57c671e
4 changed files with 68 additions and 11 deletions

View File

@@ -129,8 +129,7 @@ std::string DevicesManager::get(const std::string &a_capability)
{
std::string the_output;
struct json_object *the_output_node;
fprintf(stderr, "Devices::get...\n");
// fprintf(stderr, "Devices::get (%s)...\n", a_capability.c_str());
the_output_node = json_object_new_object();
if (a_capability == kLightEntry)
@@ -139,12 +138,10 @@ std::string DevicesManager::get(const std::string &a_capability)
}
else if (a_capability == kShutterEntry)
{
json_object_object_add(the_output_node, kShutterEntry, m_shutters.to_json());
}
else if (a_capability == kSprinklerEntry)
{
json_object_object_add(the_output_node, kSprinklerEntry, m_sprinklers.to_json());
}
@@ -162,9 +159,19 @@ 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_result;
#warning TODO
return the_result;
std::string the_output;
int the_pos;
if (a_capability == kLightEntry)
{
the_pos = m_lights.find(an_id);
if (the_pos != -1)
{
the_output = json_object_to_json_string(m_lights[the_pos].to_json());
}
}
return the_output;
}
/*! ----------------------------------------------------------------------------

View File

@@ -51,6 +51,51 @@ LightDevice::~LightDevice(void)
{
}
/*! ----------------------------------------------------------------------------
* @fn operator[]
*
* @brief to access to a specific light opbject.
*/
Light& LightDevice::operator[] (int a_pos)
{
return m_lights[a_pos];
}
/*! ----------------------------------------------------------------------------
* @fn size
*
* @brief return the size of the light devices.
*/
uint16_t LightDevice::size(void)
{
return m_lights.size();
}
/*! ----------------------------------------------------------------------------
* @fn size
*
* @brief return the size of the light devices.
*/
int LightDevice::find(int a_pos)
{
std::vector<Light>::iterator the_light_it;
int the_status = -1;
int the_pos = 0;
for (the_light_it = m_lights.begin(); the_light_it != m_lights.end(); the_light_it++)
{
if ((*the_light_it).get_id() == a_pos)
{
return the_pos;
}
the_pos++;
}
return the_status;
}
/*! ----------------------------------------------------------------------------
* @fn from_json
*

View File

@@ -41,6 +41,12 @@ class LightDevice : public Device
LightDevice(void);
~LightDevice(void);
Light& operator[] (int a_pos);
uint16_t size(void);
int find(int a_pos);
int from_json(struct json_object *a_node);
struct json_object *to_json(void);

View File

@@ -94,7 +94,7 @@ int LightsController::list(struct ubus_context *a_ctx, struct ubus_request_data
{
int the_result;
struct blob_buf the_buf = {0};
printf("%s\n", __PRETTY_FUNCTION__);
// printf("%s\n", __PRETTY_FUNCTION__);
blob_buf_init(&the_buf, 0);
@@ -116,15 +116,14 @@ int LightsController::read(struct ubus_context *a_ctx, struct ubus_request_data
{
uint32_t the_id, the_result;
struct blob_buf the_buf = {0};
printf("%s\n", __PRETTY_FUNCTION__);
// printf("%s\n", __PRETTY_FUNCTION__);
if (get_id_from_blob(a_msg, the_id) != 0)
{
return UBUS_STATUS_INVALID_ARGUMENT;
}
printf ("update id: %d\n", the_id);
// printf ("update id: %d\n", the_id);
blob_buf_init(&the_buf, 0);
blobmsg_add_json_from_string(&the_buf, m_devices_manager->get(kLightEntry, the_id).c_str());