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" : [ "timers" : [
{ {
"id": 1,
"active":false, "active":false,
"id": "Sprinklers/7", "device_id": "Sprinklers/7",
"start_time": "17:30", "start_time": "17:30",
"recurrence": 1, "recurrence": 1,
"duration": 30, "duration": 30,
"action": "start" "action": "start"
}, },
{ {
"id": 2,
"active": true, "active": true,
"id": "Sprinklers/7", "device_id": "Sprinklers/7",
"start_time": "15:50", "start_time": "21:48",
"recurrence": 0, "recurrence": 0,
"duration": 1, "duration": 1,
"action": "stop" "action": "stop"

View File

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

View File

@@ -39,6 +39,7 @@ Event::Event (void):
mActive(false), mActive(false),
mID(0), mID(0),
mDeviceID(0),
mHour(0), mHour(0),
mMinute(0), mMinute(0),
mRecurrence(0), mRecurrence(0),
@@ -68,13 +69,15 @@ int Event::load_from_json (Json::Value anElem)
std::size_t theSeparator; std::size_t theSeparator;
std::string::size_type sz; // alias of size_t std::string::size_type sz; // alias of size_t
mID = anElem["id"].asInt();
mActive = anElem["active"].asBool(); mActive = anElem["active"].asBool();
std::string theIDElem = anElem["id"].asString(); std::string theIDElem = anElem["id"].asString();
theSeparator = theIDElem.find_last_of("/"); theSeparator = theIDElem.find_last_of("/");
mCapability = theIDElem.substr(0, theSeparator); 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(); std::string theStarTime = anElem["start_time"].asString();
theSeparator = theStarTime.find_last_of(":"); theSeparator = theStarTime.find_last_of(":");
@@ -100,8 +103,10 @@ Json::Value Event::to_json (void) const
{ {
Json::Value aResult(Json::objectValue); Json::Value aResult(Json::objectValue);
aResult ["id"] = mID;
aResult ["active"] = mActive; aResult ["active"] = mActive;
aResult ["id"] = mCapability + "/" + std::to_string(mID); aResult ["device_id"] = mCapability + "/" + std::to_string(mDeviceID);
aResult ["action"] = mAction; aResult ["action"] = mAction;
aResult["recurrence"] = mRecurrence; aResult["recurrence"] = mRecurrence;
@@ -122,7 +127,7 @@ int Event::start (Devices *aDevices)
{ {
setInProgress (true); 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); 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, " - active: %s\n", (mActive?"true":"false"));
fprintf (stdout, " - capability: %s\n", mCapability.c_str()); fprintf (stdout, " - capability: %s\n", mCapability.c_str());
fprintf (stdout, " - id: %d\n", mID); fprintf (stdout, " - id: %d\n", mID);
fprintf (stdout, " - device_id: %d\n", mDeviceID);
fprintf (stdout, " - Time: %2d:%2d\n", mHour, mMinute); fprintf (stdout, " - Time: %2d:%2d\n", mHour, mMinute);
fprintf (stdout, " - recurrence: %d\n", mRecurrence); fprintf (stdout, " - recurrence: %d\n", mRecurrence);
fprintf (stdout, " - duration: %d\n", mDuration); 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 * @fn getHour
* *

View File

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

View File

@@ -104,6 +104,40 @@ int Timers::load (void)
int Timers::save (void) int Timers::save (void)
{ {
Json::StyledStreamWriter theWriter; 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; std::vector<Event>::iterator timer_evt;
Json::Value theRoot(Json::objectValue), timers_json(Json::arrayValue); Json::Value theRoot(Json::objectValue), timers_json(Json::arrayValue);
@@ -114,11 +148,7 @@ int Timers::save (void)
theRoot["timers"] = timers_json; theRoot["timers"] = timers_json;
/* Save to the File. */ return theRoot;
std::ofstream theOutput (mTimersPath.c_str());
theWriter.write (theOutput, theRoot);
return 0;
} }

View File

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

View File

@@ -25,10 +25,17 @@
/*-------------------------------- INCLUDES ---------------------------------*/ /*-------------------------------- INCLUDES ---------------------------------*/
#include <string.h>
extern "C" { extern "C" {
#include <libubox/blobmsg_json.h> #include <libubox/blobmsg_json.h>
} }
#include <jsoncpp/json.h>
#include "timers/Timers.h"
#include "timers.h" #include "timers.h"
namespace { namespace {
@@ -47,8 +54,9 @@ static ObjectType gTimersModelUbus_types(
* *
* @brief Constructor of the UBus Mixer Volume. * @brief Constructor of the UBus Mixer Volume.
*/ */
TimersModel::TimersModel (void) : TimersModel::TimersModel (Timers *aTimers) :
UBusObject (gTimersModelUbus_types, "domo.timers") 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) struct blob_attr *aMsg)
{ {
int theResult = 0; int theResult = 0;
printf ("Get the TimersModel\n"); int theID = -1;
#if 0 Json::Reader theReader;
Json::StyledWriter theWriter;
Json::Value theRoot;
struct blob_buf theBuf = {0}; struct blob_buf theBuf = {0};
char *theString = blobmsg_format_json (aMsg, true);
std::ifstream theCapFile ("./rsc/capabilities.json"); printf ("Get the TimersModel.\n");
std::stringstream theBuffer;
theBuffer << theCapFile.rdbuf();
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); 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); theResult = ubus_send_reply (aCtx, aReq, theBuf.head);
blob_buf_free (&theBuf); blob_buf_free (&theBuf);
#endif
return theResult; return theResult;
} }

View File

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