From 1b9f7bc893ae3c11e897173e5e8957a76421a691 Mon Sep 17 00:00:00 2001 From: jbnadal Date: Fri, 20 May 2016 17:22:19 +0200 Subject: [PATCH] WIP Timers. --- src/domod/src/timers/Event.cpp | 101 +++++++++++++++++++++++++++++++- src/domod/src/timers/Event.h | 14 ++++- src/domod/src/timers/Timers.cpp | 62 ++++++++++++-------- src/domod/src/timers/Timers.h | 5 +- 4 files changed, 153 insertions(+), 29 deletions(-) diff --git a/src/domod/src/timers/Event.cpp b/src/domod/src/timers/Event.cpp index 0d04477e..2a074141 100644 --- a/src/domod/src/timers/Event.cpp +++ b/src/domod/src/timers/Event.cpp @@ -88,9 +88,18 @@ int Event::Load (Json::Value anElem) * * @brief Save Event Object. */ -Json::Value Event::Save (void) +Json::Value Event::Save (void) const { - Json::Value aResult; + Json::Value aResult(Json::objectValue); + + aResult ["active"] = mActive; + aResult ["id"] = mCapability + "/" + std::to_string(mID); + aResult ["action"] = mAction; + + aResult["recurrence"] = mRecurrence; + aResult["duration"] = mDuration; + + aResult["start_time"] = std::to_string(mHour) + ":" + std::to_string(mMinute); return aResult; } @@ -113,3 +122,91 @@ void Event::Dump (void) fprintf (stdout, " - duration: %d\n", mDuration); fprintf (stdout, " - action: %s\n", mAction.c_str()); } + + +/*! ---------------------------------------------------------------------------- + * @fn isActive + * + * @brief return true if the Timer event is active. + */ +bool Event::isActive (void) +{ + return mActive; +} + + +/*! ---------------------------------------------------------------------------- + * @fn getCapability + * + * @brief return the capability of the Event. + */ +std::string Event::getCapability (void) +{ + return mCapability; +} + + +/*! ---------------------------------------------------------------------------- + * @fn getID + * + * @brief return the ID of the Event. + */ +uint16_t Event::getID (void) +{ + return mID; +} + + +/*! ---------------------------------------------------------------------------- + * @fn getHour + * + * @brief Return the Hour of the Event. + */ +uint8_t Event::getHour (void) +{ + return mHour; +} + + +/*! ---------------------------------------------------------------------------- + * @fn getMinute + * + * @brief Return the Minute of the Event. + */ +uint8_t Event::getMinute (void) +{ + return mMinute; +} + + +/*! ---------------------------------------------------------------------------- + * @fn getRecurrence + * + * @brief Return the Recurrency of the Event. + */ +uint16_t Event::getRecurrence (void) +{ + return mRecurrence; +} + + +/*! ---------------------------------------------------------------------------- + * @fn getDuration + * + * @brief Return the Duration of the Event. + */ +uint16_t Event::getDuration (void) +{ + return mDuration; +} + + +/*! ---------------------------------------------------------------------------- + * @fn getAction + * + * @brief Return the Action of the Event. + */ +std::string Event::getAction (void) +{ + return mAction; +} diff --git a/src/domod/src/timers/Event.h b/src/domod/src/timers/Event.h index 690db4fa..05054a3b 100644 --- a/src/domod/src/timers/Event.h +++ b/src/domod/src/timers/Event.h @@ -45,10 +45,20 @@ public: ~Event (void); int Load (Json::Value anElem); - Json::Value Save (void); + Json::Value Save (void) const; void Dump (void); - + + /* Getter */ + bool isActive (void); + std::string getCapability(void); + uint16_t getID (void); + uint8_t getHour(void); + uint8_t getMinute(void); + uint16_t getRecurrence(void); + uint16_t getDuration(void); + std::string getAction(void); + private: bool mActive; std::string mCapability; diff --git a/src/domod/src/timers/Timers.cpp b/src/domod/src/timers/Timers.cpp index 91c0df4d..f1c917d5 100644 --- a/src/domod/src/timers/Timers.cpp +++ b/src/domod/src/timers/Timers.cpp @@ -67,21 +67,55 @@ Timers::~Timers (void) int Timers::Load (void) { Json::Reader theReader; + Json::Value theRoot; std::ifstream theTimerFile (mTimersPath.c_str()); - if (!theReader.parse (theTimerFile, mRoot)) { + /* Read and Parse the Json file. */ + if (!theReader.parse (theTimerFile, theRoot)) { fprintf (stderr, "Failed to parse the Timers File (%s).\n", mTimersPath.c_str()); return -1; } + /* Create the Objects.. */ + for (Json::Value& theElement : theRoot["timers"]) { + Event theEvent; + if (theEvent.Load (theElement) == 0) { + + mTimers.push_back (theEvent); + } + } + Start (k60s, true); return 0; } +/*! ---------------------------------------------------------------------------- + * @fn Expire + * + * @brief Method Call when the Timer has expired. + */ +int Timers::Save (void) +{ + Json::StyledStreamWriter theWriter; + std::vector::iterator timer_evt; + Json::Value theRoot(Json::objectValue), timers_json(Json::arrayValue); + + for(timer_evt = mTimers.begin(); timer_evt != mTimers.end(); timer_evt++) { + + timers_json.append ((*timer_evt).Save()); + } + + theRoot["timers"] = timers_json; + /* Save to the File. */ + std::ofstream theOutput (mTimersPath.c_str()); + theWriter.write (theOutput, theRoot); +} + + /*! ---------------------------------------------------------------------------- * @fn Expire * @@ -89,32 +123,12 @@ int Timers::Load (void) */ int Timers::Expire (void) { + std::vector::iterator timer_evt; fprintf (stderr, "**** >> Manage timers....\n"); - for (Json::Value& theElement : mRoot["timers"]) { + for(timer_evt = mTimers.begin(); timer_evt != mTimers.end(); timer_evt++) { - if (theElement["active"].asBool() == true) { - - std::size_t theSeparator; - std::string::size_type sz; // alias of size_t - std::string theCapability; - std::string theIDElem = theElement["id"].asString(); - theSeparator = theIDElem.find_last_of("/"); - - theCapability = theIDElem.substr(0, theSeparator); - - int theID = std::stoi (theIDElem.substr (theSeparator + 1), &sz); - - fprintf (stderr, "Cap: %s ID:%d\n", theCapability.c_str(), theID); - int theDuration = theElement["duration"].asInt(); - - int theHour, theMin; - std::string theStarTime = theElement["start_time"].asString(); - theSeparator = theStarTime.find_last_of(":"); - theHour = std::stoi (theStarTime.substr (0, theSeparator), &sz); - theMin = std::stoi (theStarTime.substr (theSeparator + 1), &sz); - fprintf (stderr, "Hour: %d Min:%d\n", theHour, theMin); - } + (*timer_evt).Dump(); } return 0; diff --git a/src/domod/src/timers/Timers.h b/src/domod/src/timers/Timers.h index 0fb47fff..efb8af00 100644 --- a/src/domod/src/timers/Timers.h +++ b/src/domod/src/timers/Timers.h @@ -30,8 +30,10 @@ #include #include +#include #include "ubuscpp/UBusTimer.h" +#include "timers/Event.h" /*---------------------------------- Deps -----------------------------------*/ @@ -48,11 +50,12 @@ public: int Expire (void); int Load (void); + int Save (void); private: Devices *mDevices; std::string mTimersPath; - Json::Value mRoot; + std::vector mTimers; };