Functional sprinkler screen.
This commit is contained in:
@@ -53,13 +53,13 @@ extern "C" {
|
||||
static struct ubus_context *setupUbus (void)
|
||||
{
|
||||
ubus_context *theCtx;
|
||||
|
||||
|
||||
if (uloop_init () !=0) {
|
||||
|
||||
|
||||
fprintf (stderr, "Failed to init Uloop.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
signal (SIGPIPE, SIG_IGN);
|
||||
|
||||
theCtx = ubus_connect (NULL);
|
||||
@@ -71,7 +71,7 @@ static struct ubus_context *setupUbus (void)
|
||||
}
|
||||
|
||||
ubus_add_uloop (theCtx);
|
||||
|
||||
|
||||
return theCtx;
|
||||
}
|
||||
} // SetupUbus.
|
||||
@@ -91,11 +91,11 @@ int main (void)
|
||||
|
||||
thePath = getenv ("DOMO_WRITE_PATH");
|
||||
thePath += "/Devices.json";
|
||||
|
||||
|
||||
//printf ("DOMO PATH: %s\n", thePath.c_str());
|
||||
|
||||
|
||||
Devices theDevices (thePath);
|
||||
|
||||
|
||||
if (theDevices.Setup() != 0) {
|
||||
|
||||
return -1;
|
||||
|
||||
@@ -138,8 +138,9 @@ int CapabilitiesLights::Post (struct ubus_context *aCtx, struct ubus_request_dat
|
||||
Json::Value theOutput;
|
||||
Json::Value theElement;
|
||||
int theID;
|
||||
bool theState;
|
||||
|
||||
bool theState;
|
||||
fprintf (stderr,"CapabilitiesLights::Post \n");
|
||||
|
||||
if (!theReader.parse (theString, theRoot)) {
|
||||
|
||||
fprintf (stderr, "Failed parse the parameters.\n");
|
||||
|
||||
@@ -29,6 +29,8 @@ extern "C" {
|
||||
#include <libubox/blobmsg_json.h>
|
||||
}
|
||||
|
||||
#include <ubuscpp/UBusCall.h>
|
||||
|
||||
#include "models/Devices.h"
|
||||
|
||||
#include "capabilities_sprinklers.h"
|
||||
@@ -83,7 +85,7 @@ int CapabilitiesSprinklers::Get (struct ubus_context *aCtx, struct ubus_request_
|
||||
|
||||
blob_buf_init (&theBuf, 0);
|
||||
|
||||
blobmsg_add_json_from_string (&theBuf, mDevices->get("Sprinkers").c_str());
|
||||
blobmsg_add_json_from_string (&theBuf, mDevices->get("Sprinklers").c_str());
|
||||
|
||||
theResult = ubus_send_reply (aCtx, aReq, theBuf.head);
|
||||
|
||||
@@ -124,9 +126,66 @@ int CapabilitiesSprinklers::Put (struct ubus_context*, struct ubus_request_data*
|
||||
*
|
||||
* @brief Get the List of the Capabilities.
|
||||
*/
|
||||
int CapabilitiesSprinklers::Post (struct ubus_context*, struct ubus_request_data*, struct blob_attr*)
|
||||
int CapabilitiesSprinklers::Post (struct ubus_context *aCtx, struct ubus_request_data *aReq,
|
||||
struct blob_attr *aMsg)
|
||||
{
|
||||
int theResult = 0;
|
||||
int theResult;
|
||||
struct blob_buf theBuf = {0};
|
||||
char *theString = blobmsg_format_json (aMsg, true);
|
||||
Json::Reader theReader;
|
||||
Json::StyledWriter theWriter;
|
||||
Json::Value theRoot;
|
||||
Json::Value theOutput;
|
||||
Json::Value theElement;
|
||||
int theID;
|
||||
bool theState;
|
||||
fprintf (stderr,"CapabilitiesSprinklers::Post \n");
|
||||
|
||||
if (!theReader.parse (theString, theRoot)) {
|
||||
|
||||
fprintf (stderr, "Failed parse the parameters.\n");
|
||||
return UBUS_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
theID = theRoot["id"].asInt();
|
||||
theState = theRoot["state"].asBool();
|
||||
|
||||
theElement = mDevices->get("Sprinklers", theID);
|
||||
theElement["state"] = theState;
|
||||
|
||||
if (ChangeState(theElement) == 0) {
|
||||
|
||||
mDevices->set ("Sprinklers", theID, theElement);
|
||||
}
|
||||
|
||||
theOutput["Sprinklers"] = theElement;
|
||||
|
||||
blob_buf_init (&theBuf, 0);
|
||||
|
||||
blobmsg_add_json_from_string (&theBuf, theWriter.write(theOutput).c_str());
|
||||
|
||||
theResult = ubus_send_reply (aCtx, aReq, theBuf.head);
|
||||
|
||||
blob_buf_free (&theBuf);
|
||||
|
||||
return theResult;
|
||||
}
|
||||
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn ChangeState
|
||||
*
|
||||
* @brief Change the State of an station.
|
||||
*/
|
||||
int CapabilitiesSprinklers::ChangeState (const Json::Value &aStation)
|
||||
{
|
||||
UBusCall theCmd;
|
||||
Json::Value theParam;
|
||||
Json::StyledWriter theWriter;
|
||||
std::string theResult;
|
||||
|
||||
theParam["station"] = aStation["id"];
|
||||
theParam["state"] = aStation["state"];
|
||||
|
||||
return theCmd.Exec ("sprinklers", "set", theWriter.write(theParam).c_str(),theResult);
|
||||
}
|
||||
|
||||
@@ -50,6 +50,7 @@ public:
|
||||
int Post (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
|
||||
|
||||
private:
|
||||
int ChangeState (const Json::Value &aStation);
|
||||
Devices *mDevices;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user