Work in Progress on the Timer Side.

This commit is contained in:
jbnadal
2016-05-19 19:04:34 +02:00
parent 418c856177
commit 23c15828f4
8 changed files with 295 additions and 31 deletions

View File

@@ -24,6 +24,7 @@ file(
../../src/helpers/Tokenizer.cpp ../../src/helpers/Tokenizer.cpp
../../src/helpers/Strings.cpp ../../src/helpers/Strings.cpp
../../src/Timers.cpp ../../src/Timers.cpp
../../src/Event.cpp
) )
add_executable ( add_executable (

View File

@@ -87,8 +87,7 @@ int main (void)
{ {
int theRet = 0; int theRet = 0;
char *theWritePath; char *theWritePath;
std::string thePath; std::string theDevicePath, theTimerPath;
Timers theTimers;
struct ubus_context *theCtx = NULL; struct ubus_context *theCtx = NULL;
theWritePath = getenv ("DOMO_WRITE_PATH"); theWritePath = getenv ("DOMO_WRITE_PATH");
@@ -98,18 +97,23 @@ int main (void)
return -1; return -1;
} }
thePath = theWritePath; // Create Configs Paths...
thePath += "/Devices.json";
theDevicePath = theWritePath;
theDevicePath += "/Devices.json";
theTimerPath = theWritePath;
theTimerPath += "/Timers.json";
//printf ("DOMO PATH: %s\n", thePath.c_str()); //printf ("DOMO PATH: %s\n", thePath.c_str());
Devices theDevices (thePath); Devices theDevices (theDevicePath);
if (theDevices.Setup() != 0) { if (theDevices.Load() != 0) {
return -1; return -1;
} }
Capabilities theCapabilities; Capabilities theCapabilities;
CapabilitiesLights theCapLights (&theDevices); CapabilitiesLights theCapLights (&theDevices);
CapabilitiesShutters theCapShutters (&theDevices); CapabilitiesShutters theCapShutters (&theDevices);
@@ -124,7 +128,12 @@ int main (void)
} }
/* Setup the Timers. */ /* Setup the Timers. */
theTimers.Setup (); Timers theTimers (theTimerPath, &theDevices);
if (theTimers.Load() != 0) {
return -1;
}
/* Add the UBus to the model exposed. */ /* Add the UBus to the model exposed. */
ubus_add_object (theCtx, &theCapabilities); ubus_add_object (theCtx, &theCapabilities);

View File

@@ -58,21 +58,21 @@ Devices::~Devices (void)
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn Setup * @fn Load
* *
* @brief Setup the Device object. * @brief Load the Device object.
*/ */
int Devices::Setup (void) int Devices::Load (void)
{ {
Json::Reader theReader; Json::Reader theReader;
std::ifstream theDevicesFile (mDevicesPath.c_str()); std::ifstream theDevicesFile (mDevicesPath.c_str());
if (!theReader.parse (theDevicesFile, mRoot)) { if (!theReader.parse (theDevicesFile, mRoot)) {
fprintf (stderr, "Failed to parse the Devices File (%s).\n", fprintf (stderr, "Failed to parse the Devices File (%s).\n",
mDevicesPath.c_str()); mDevicesPath.c_str());
return -1; return -1;
} }
return 0; return 0;
} }
@@ -157,7 +157,6 @@ int Devices::set (const std::string &aCapability, int anId, const Json::Value &a
bool Devices::setDevice (const std::string &aCapability, bool Devices::setDevice (const std::string &aCapability,
const std::string &aDeviceName, bool aState) const std::string &aDeviceName, bool aState)
{ {
bool theStateChange = false;
fprintf (stdout, "setDevice cap:%s deviceName: %s\n", aCapability.c_str(), fprintf (stdout, "setDevice cap:%s deviceName: %s\n", aCapability.c_str(),
aDeviceName.c_str()); aDeviceName.c_str());
@@ -181,7 +180,7 @@ bool Devices::setDevice (const std::string &aCapability,
} }
} }
} }
return false; return false;
} }

View File

@@ -37,7 +37,7 @@ public:
Devices (std::string aPath); Devices (std::string aPath);
~Devices (void); ~Devices (void);
int Setup (void); int Load (void);
std::string get (const std::string &aCapability); std::string get (const std::string &aCapability);
Json::Value get (const std::string &aCapability, int anId); Json::Value get (const std::string &aCapability, int anId);

View File

@@ -0,0 +1,115 @@
/*!
* Event.cpp
*
* Copyright (c) 2016, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 19/05/2016
*
*/
/*------------------------------- INCLUDES ----------------------------------*/
#include "timers/Event.h"
/*! ----------------------------------------------------------------------------
* @fn Event
*
* @brief Constructor of Timer Event Object.
*/
Event::Event (void)
{
}
/*! ----------------------------------------------------------------------------
* @fn ~Event
*
* @brief Destructor of Timer Event Object.
*/
Event::~Event (void)
{
}
/*! ----------------------------------------------------------------------------
* @fn Load
*
* @brief Load Event Object.
*/
int Event::Load (Json::Value anElem)
{
std::size_t theSeparator;
std::string::size_type sz; // alias of size_t
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);
std::string theStarTime = anElem["start_time"].asString();
theSeparator = theStarTime.find_last_of(":");
mHour = std::stoi (theStarTime.substr (0, theSeparator), &sz);
mMinute = std::stoi (theStarTime.substr (theSeparator + 1), &sz);
mRecurrence = anElem["recurrence"].asInt();
mDuration = anElem["duration"].asInt();
mAction = anElem["action"].asString();
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn Save
*
* @brief Save Event Object.
*/
Json::Value Event::Save (void)
{
Json::Value aResult;
return aResult;
}
/*! ----------------------------------------------------------------------------
* @fn Load
*
* @brief Load Event Object.
*/
void Event::Dump (void)
{
fprintf (stdout, "Timer Event:\n");
fprintf (stdout, "=========== \n");
fprintf (stdout, " - active: %s\n", (mActive?"true":"false"));
fprintf (stdout, " - capability: %s\n", mCapability.c_str());
fprintf (stdout, " - id: %d\n", mID);
fprintf (stdout, " - Time: %2d:%2d\n", mHour, mMinute);
fprintf (stdout, " - recurrence: %d\n", mRecurrence);
fprintf (stdout, " - duration: %d\n", mDuration);
fprintf (stdout, " - action: %s\n", mAction.c_str());
}

View File

@@ -0,0 +1,63 @@
/*!
* Event.h
*
* Copyright (c) 2016, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 19/05/2016
*
*/
#ifndef _EVENT_H
#define _EVENT_H
/*------------------------------- INCLUDES ----------------------------------*/
#include <stdint.h>
#include <string>
#include <jsoncpp/json.h>
/*---------------------------------- Deps -----------------------------------*/
/*--------------------------------- CLASS ----------------------------------*/
class Event {
public:
Event (void);
~Event (void);
int Load (Json::Value anElem);
Json::Value Save (void);
void Dump (void);
private:
bool mActive;
std::string mCapability;
uint16_t mID;
uint8_t mHour;
uint8_t mMinute;
uint16_t mRecurrence;
uint16_t mDuration;
std::string mAction;
};
#endif /* _EVENT_H */

View File

@@ -26,40 +26,57 @@
/*------------------------------- INCLUDES ----------------------------------*/ /*------------------------------- INCLUDES ----------------------------------*/
#include <cstdio> #include <cstdio>
#include <iostream>
#include <fstream>
#include <vector>
#include "models/Devices.h"
#include "timers/Timers.h" #include "timers/Timers.h"
//#define k60s 60000
#define k60s 2000
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn Timers * @fn Timers
* *
* @brief Constructor of the Timers Managagers. * @brief Constructor of the Timers Managagers.
*/ */
Timers::Timers (void) Timers::Timers (std::string aPath, Devices *aDevice):
mDevices (aDevice),
mTimersPath (aPath)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn Devices * @fn ~Timers
* *
* @brief Destructors of the Timer Managers. * @brief Destructors of the Timer Managers.
*/ */
Timers::~Timers (void) Timers::~Timers (void)
{ {
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn Devices * @fn Load
* *
* @brief Destructors of the Timer Managers. * @brief Load the Timer Managers.
*/ */
int Timers::Setup (void) int Timers::Load (void)
{ {
Start (2000, true); Json::Reader theReader;
std::ifstream theTimerFile (mTimersPath.c_str());
if (!theReader.parse (theTimerFile, mRoot)) {
fprintf (stderr, "Failed to parse the Timers File (%s).\n",
mTimersPath.c_str());
return -1;
}
Start (k60s, true);
return 0; return 0;
} }
@@ -72,7 +89,56 @@ int Timers::Setup (void)
*/ */
int Timers::Expire (void) int Timers::Expire (void)
{ {
fprintf (stderr, "Domo::Timers::Expire\n"); fprintf (stderr, "**** >> Manage timers....\n");
for (Json::Value& theElement : mRoot["timers"]) {
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);
}
}
return 0; return 0;
} }
/*
{
"timers" : [
{
"active":false,
"id": "Sprinklers/7",
"start_time": "17:30",
"recurrence": 1,
"duration": 30,
"action": "start"
},
{
"active": true,
"id": "Sprinklers/7",
"start_time": "17:30",
"recurrence": 0,
"duration": 30,
"action": "stop"
}
]
}
*/

View File

@@ -28,20 +28,31 @@
/*------------------------------- INCLUDES ----------------------------------*/ /*------------------------------- INCLUDES ----------------------------------*/
#include <jsoncpp/json.h>
#include <string>
#include "ubuscpp/UBusTimer.h" #include "ubuscpp/UBusTimer.h"
/*---------------------------------- Deps -----------------------------------*/
class Devices;
/*--------------------------------- CLASS ----------------------------------*/
class Timers : public UBusTimer { class Timers : public UBusTimer {
public: public:
Timers (void); Timers (std::string aPath, Devices *aDevice);
~Timers (void); ~Timers (void);
int Expire (void); int Expire (void);
int Setup (void); int Load (void);
private: private:
UBusTimer mTest; Devices *mDevices;
std::string mTimersPath;
Json::Value mRoot;
}; };