WIP Timers.

This commit is contained in:
jbnadal
2016-05-20 17:22:19 +02:00
parent 23c15828f4
commit 1b9f7bc893
4 changed files with 153 additions and 29 deletions

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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<Event>::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<Event>::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;

View File

@@ -30,8 +30,10 @@
#include <jsoncpp/json.h>
#include <string>
#include <vector>
#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 <Event> mTimers;
};