WIP: Get Devices.

This commit is contained in:
jbnadal
2016-05-24 18:20:37 +02:00
parent a53d90e29e
commit ef6abd7f02
8 changed files with 91 additions and 11 deletions

View File

@@ -109,3 +109,80 @@ int Devices::save (void)
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn get
*
* @brief get the list of devices of a specific capabilities.
*/
std::string Devices::get (const std::string &aCapability)
{
Json::StyledWriter theWriter;
Json::Value theOutput;
fprintf (stderr, "Devices::get...\n");
if (aCapability == "Lights") {
theOutput[aCapability] = mLights.to_json(true);
}
else if (aCapability == "Shutters") {
}
else if (aCapability == "Sprinklers") {
}
return theWriter.write (theOutput).c_str();
}
/*! ----------------------------------------------------------------------------
* @fn get
*
* @brief get the device of a specific capabilities, with the ID.
*/
Json::Value Devices::get (const std::string &aCapability, int anId)
{
}
#if 0
/*! ----------------------------------------------------------------------------
* @fn get
*
* @brief get the list of devices of a specific capabilities.
*/
std::string Devices::get (const std::string &aCapability)
{
Json::StyledWriter theWriter;
Json::Value theOutput;
printf ("Get aCapability: %s\n", aCapability.c_str());
theOutput[aCapability] = mRoot[aCapability]["data"];
return theWriter.write (theOutput).c_str();
}
/*! ----------------------------------------------------------------------------
* @fn get
*
* @brief get the device of a specific capabilities, with the ID.
*/
Json::Value Devices::get (const std::string &aCapability, int anId)
{
Json::Value theResult;
for (const Json::Value& theElement : mRoot[aCapability]["data"]) {
if (theElement["id"].asInt() == anId) {
theResult = theElement;
}
}
return theResult;
}
#endif

View File

@@ -50,6 +50,9 @@ public:
int load (void);
int save (void);
std::string get (const std::string &aCapability);
Json::Value get (const std::string &aCapability, int anId);
private:
std::string mFilePath;

View File

@@ -82,7 +82,7 @@ int LightDevice::load_from_json (Json::Value anElem)
*
* @brief Export LightDevice Object as JSON Object.
*/
Json::Value LightDevice::to_json (void)
Json::Value LightDevice::to_json (bool bDataOnly)
{
Json::Value theResult;
Json::Value data_json(Json::arrayValue);
@@ -98,5 +98,8 @@ Json::Value LightDevice::to_json (void)
theResult["speach_name"] = mspeach_name;
theResult["data"] = data_json;
if (bDataOnly == true)
return data_json;
return theResult;
}

View File

@@ -46,7 +46,7 @@ public:
~LightDevice (void);
int load_from_json (Json::Value anElem);
Json::Value to_json (void);
Json::Value to_json (bool bDataOnly = false);
private:
std::vector <Light> mLights;

View File

@@ -114,8 +114,6 @@ int main (void)
return -1;
}
theDevices.save();
Capabilities theCapabilities;
CapabilitiesLights theCapLights (&theDevices);
CapabilitiesShutters theCapShutters (&theDevices);

View File

@@ -86,11 +86,11 @@ int CapabilitiesLights::Get (struct ubus_context *aCtx, struct ubus_request_data
struct blob_buf theBuf = {0};
blob_buf_init (&theBuf, 0);
/* TODO
blobmsg_add_json_from_string (&theBuf, mDevices->get("Lights").c_str());
theResult = ubus_send_reply (aCtx, aReq, theBuf.head);
*/
blob_buf_free (&theBuf);
return theResult;

View File

@@ -92,9 +92,9 @@ int CapabilitiesShutters::Get (struct ubus_context *aCtx, struct ubus_request_da
struct blob_buf theBuf = {0};
blob_buf_init (&theBuf, 0);
/* TODO
blobmsg_add_json_from_string (&theBuf, mDevices->get("Shutters").c_str());
*/
theResult = ubus_send_reply (aCtx, aReq, theBuf.head);
blob_buf_free (&theBuf);

View File

@@ -70,7 +70,6 @@ CapabilitiesSprinklers::CapabilitiesSprinklers (Devices *aDevice) :
*/
CapabilitiesSprinklers::~CapabilitiesSprinklers (void)
{
}
@@ -86,9 +85,9 @@ int CapabilitiesSprinklers::Get (struct ubus_context *aCtx, struct ubus_request_
struct blob_buf theBuf = {0};
blob_buf_init (&theBuf, 0);
/*TODO
blobmsg_add_json_from_string (&theBuf, mDevices->get("Sprinklers").c_str());
*/
theResult = ubus_send_reply (aCtx, aReq, theBuf.head);
blob_buf_free (&theBuf);