Add Device ID and connect the ubus method to get with and without parameters.

This commit is contained in:
2016-07-04 22:56:44 +02:00
parent d3680c10a5
commit 5cbe69d986
8 changed files with 106 additions and 26 deletions

View File

@@ -1,17 +1,19 @@
{
"timers" : [
{
"id": 1,
"active":false,
"id": "Sprinklers/7",
"device_id": "Sprinklers/7",
"start_time": "17:30",
"recurrence": 1,
"duration": 30,
"action": "start"
},
{
"id": 2,
"active": true,
"id": "Sprinklers/7",
"start_time": "15:50",
"device_id": "Sprinklers/7",
"start_time": "21:48",
"recurrence": 0,
"duration": 1,
"action": "stop"

View File

@@ -129,7 +129,7 @@ int main (void)
CapabilitiesShuttersModel theCapShutters (&theDevices);
CapabilitiesSprinklersModel theCapSprinklers (&theDevices);
SpeachCommandModel theSpeachCommand (&theDevices);
TimersModel theTimersModel;
TimersModel theTimersModel (&theTimers);
/* Setup the Ubus context. */
theCtx = setupUbus ();

View File

@@ -39,6 +39,7 @@ Event::Event (void):
mActive(false),
mID(0),
mDeviceID(0),
mHour(0),
mMinute(0),
mRecurrence(0),
@@ -68,13 +69,15 @@ int Event::load_from_json (Json::Value anElem)
std::size_t theSeparator;
std::string::size_type sz; // alias of size_t
mID = anElem["id"].asInt();
mActive = anElem["active"].asBool();
std::string theIDElem = anElem["id"].asString();
theSeparator = theIDElem.find_last_of("/");
mCapability = theIDElem.substr(0, theSeparator);
mID = std::stoi (theIDElem.substr (theSeparator + 1), &sz);
mDeviceID = std::stoi (theIDElem.substr (theSeparator + 1), &sz);
std::string theStarTime = anElem["start_time"].asString();
theSeparator = theStarTime.find_last_of(":");
@@ -100,8 +103,10 @@ Json::Value Event::to_json (void) const
{
Json::Value aResult(Json::objectValue);
aResult ["id"] = mID;
aResult ["active"] = mActive;
aResult ["id"] = mCapability + "/" + std::to_string(mID);
aResult ["device_id"] = mCapability + "/" + std::to_string(mDeviceID);
aResult ["action"] = mAction;
aResult["recurrence"] = mRecurrence;
@@ -122,7 +127,7 @@ int Event::start (Devices *aDevices)
{
setInProgress (true);
return aDevices->set (mCapability.c_str(), mID, true);
return aDevices->set (mCapability.c_str(), mDeviceID, true);
}
@@ -135,7 +140,7 @@ int Event::stop (Devices *aDevices)
{
setInProgress (false);
return aDevices->set (mCapability.c_str(), mID, false);
return aDevices->set (mCapability.c_str(), mDeviceID, false);
}
@@ -151,6 +156,7 @@ void Event::dump (void)
fprintf (stdout, " - active: %s\n", (mActive?"true":"false"));
fprintf (stdout, " - capability: %s\n", mCapability.c_str());
fprintf (stdout, " - id: %d\n", mID);
fprintf (stdout, " - device_id: %d\n", mDeviceID);
fprintf (stdout, " - Time: %2d:%2d\n", mHour, mMinute);
fprintf (stdout, " - recurrence: %d\n", mRecurrence);
fprintf (stdout, " - duration: %d\n", mDuration);
@@ -191,6 +197,17 @@ uint16_t Event::getID (void) const
}
/*! ----------------------------------------------------------------------------
* @fn getDeviceID
*
* @brief return the Device ID of the Event.
*/
uint16_t Event::getDeviceID (void) const
{
return mDeviceID;
}
/*! ----------------------------------------------------------------------------
* @fn getHour
*

View File

@@ -57,6 +57,7 @@ public:
bool isActive (void) const;
std::string getCapability (void) const;
uint16_t getID (void) const;
uint16_t getDeviceID (void) const;
uint8_t getHour (void) const;
uint8_t getMinute (void) const;
uint16_t getRecurrence (void) const;
@@ -71,6 +72,7 @@ private:
bool mActive;
std::string mCapability;
uint16_t mID;
uint16_t mDeviceID;
uint8_t mHour;
uint8_t mMinute;
uint16_t mRecurrence;

View File

@@ -104,6 +104,40 @@ int Timers::load (void)
int Timers::save (void)
{
Json::StyledStreamWriter theWriter;
/* Save to the File. */
std::ofstream theOutput (mTimersPath.c_str());
theWriter.write (theOutput, to_json());
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn get
*
* @brief Method to get a specific Timers Object.
*/
Event Timers::get (uint16_t anID)
{
std::vector<Event>::iterator timer_evt;
for (timer_evt = mTimers.begin(); timer_evt != mTimers.end(); timer_evt++) {
if ((*timer_evt).getID() == anID)
return (*timer_evt);
}
}
/*! ----------------------------------------------------------------------------
* @fn to_json
*
* @brief Method to return the Timers Object into a Json formar into a Json Object.
*/
Json::Value Timers::to_json (void)
{
std::vector<Event>::iterator timer_evt;
Json::Value theRoot(Json::objectValue), timers_json(Json::arrayValue);
@@ -114,11 +148,7 @@ int Timers::save (void)
theRoot["timers"] = timers_json;
/* Save to the File. */
std::ofstream theOutput (mTimersPath.c_str());
theWriter.write (theOutput, theRoot);
return 0;
return theRoot;
}

View File

@@ -52,6 +52,10 @@ public:
int load (void);
int save (void);
Event get (uint16_t anID);
Json::Value to_json (void);
private:
Devices *mDevices;

View File

@@ -25,10 +25,17 @@
/*-------------------------------- INCLUDES ---------------------------------*/
#include <string.h>
extern "C" {
#include <libubox/blobmsg_json.h>
}
#include <jsoncpp/json.h>
#include "timers/Timers.h"
#include "timers.h"
namespace {
@@ -47,8 +54,9 @@ static ObjectType gTimersModelUbus_types(
*
* @brief Constructor of the UBus Mixer Volume.
*/
TimersModel::TimersModel (void) :
UBusObject (gTimersModelUbus_types, "domo.timers")
TimersModel::TimersModel (Timers *aTimers) :
UBusObject (gTimersModelUbus_types, "domo.timers"),
mTimers (aTimers)
{
}
@@ -73,21 +81,35 @@ int TimersModel::Get (struct ubus_context *aCtx, struct ubus_request_data *aReq,
struct blob_attr *aMsg)
{
int theResult = 0;
printf ("Get the TimersModel\n");
#if 0
int theID = -1;
Json::Reader theReader;
Json::StyledWriter theWriter;
Json::Value theRoot;
struct blob_buf theBuf = {0};
char *theString = blobmsg_format_json (aMsg, true);
std::ifstream theCapFile ("./rsc/capabilities.json");
std::stringstream theBuffer;
theBuffer << theCapFile.rdbuf();
printf ("Get the TimersModel.\n");
if (!theReader.parse (theString, theRoot)) {
fprintf (stderr, "Failed parse the parameters.\n");
return UBUS_STATUS_INVALID_ARGUMENT;
}
theID = theRoot["id"].asInt();
blob_buf_init (&theBuf, 0);
blobmsg_add_json_from_string (&theBuf, theBuffer.str().c_str());
if (theID == 0)
blobmsg_add_json_from_string (&theBuf, theWriter.write(mTimers->to_json()).c_str());
else {
printf ("we want a specific id :%d\n", theID);
blobmsg_add_json_from_string (&theBuf, theWriter.write(mTimers->get(theID).to_json()).c_str());
}
theResult = ubus_send_reply (aCtx, aReq, theBuf.head);
blob_buf_free (&theBuf);
#endif
return theResult;
}

View File

@@ -34,17 +34,20 @@
/*--------------------------------- Define ----------------------------------*/
class Timers;
/*--------------------------------- CLASS ----------------------------------*/
class TimersModel : public UBusObject {
public:
TimersModel (void);
TimersModel (Timers *aTimers);
~TimersModel (void);
int Get (struct ubus_context*, struct ubus_request_data*, struct blob_attr*);
private:
Timers *mTimers;
};
#endif /* _UBUS_TIMERS_H */