diff --git a/src/domod/builders/cmake/CMakeLists.txt b/src/domod/builders/cmake/CMakeLists.txt index 88db6cef..1436f1d6 100644 --- a/src/domod/builders/cmake/CMakeLists.txt +++ b/src/domod/builders/cmake/CMakeLists.txt @@ -24,6 +24,7 @@ file( ../../src/helpers/Tokenizer.cpp ../../src/helpers/Strings.cpp ../../src/Timers.cpp + ../../src/Event.cpp ) add_executable ( diff --git a/src/domod/src/main.cpp b/src/domod/src/main.cpp index 1f9f88d5..e85f4b69 100644 --- a/src/domod/src/main.cpp +++ b/src/domod/src/main.cpp @@ -87,8 +87,7 @@ int main (void) { int theRet = 0; char *theWritePath; - std::string thePath; - Timers theTimers; + std::string theDevicePath, theTimerPath; struct ubus_context *theCtx = NULL; theWritePath = getenv ("DOMO_WRITE_PATH"); @@ -98,18 +97,23 @@ int main (void) return -1; } - thePath = theWritePath; - thePath += "/Devices.json"; + // Create Configs Paths... + + theDevicePath = theWritePath; + theDevicePath += "/Devices.json"; + theTimerPath = theWritePath; + theTimerPath += "/Timers.json"; + //printf ("DOMO PATH: %s\n", thePath.c_str()); - Devices theDevices (thePath); + Devices theDevices (theDevicePath); - if (theDevices.Setup() != 0) { + if (theDevices.Load() != 0) { return -1; } - + Capabilities theCapabilities; CapabilitiesLights theCapLights (&theDevices); CapabilitiesShutters theCapShutters (&theDevices); @@ -124,7 +128,12 @@ int main (void) } /* Setup the Timers. */ - theTimers.Setup (); + Timers theTimers (theTimerPath, &theDevices); + + if (theTimers.Load() != 0) { + + return -1; + } /* Add the UBus to the model exposed. */ ubus_add_object (theCtx, &theCapabilities); diff --git a/src/domod/src/models/Devices.cpp b/src/domod/src/models/Devices.cpp index eadba445..8d6aa7af 100644 --- a/src/domod/src/models/Devices.cpp +++ b/src/domod/src/models/Devices.cpp @@ -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()); if (!theReader.parse (theDevicesFile, mRoot)) { - fprintf (stderr, "Failed to parse the Devices File (%s).\n", - mDevicesPath.c_str()); - return -1; - } + fprintf (stderr, "Failed to parse the Devices File (%s).\n", + mDevicesPath.c_str()); + return -1; + } 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, const std::string &aDeviceName, bool aState) { - bool theStateChange = false; fprintf (stdout, "setDevice cap:%s deviceName: %s\n", aCapability.c_str(), aDeviceName.c_str()); @@ -181,7 +180,7 @@ bool Devices::setDevice (const std::string &aCapability, } } } - + return false; } diff --git a/src/domod/src/models/Devices.h b/src/domod/src/models/Devices.h index a8f2af87..5230568c 100644 --- a/src/domod/src/models/Devices.h +++ b/src/domod/src/models/Devices.h @@ -37,7 +37,7 @@ public: Devices (std::string aPath); ~Devices (void); - int Setup (void); + int Load (void); std::string get (const std::string &aCapability); Json::Value get (const std::string &aCapability, int anId); diff --git a/src/domod/src/timers/Event.cpp b/src/domod/src/timers/Event.cpp new file mode 100644 index 00000000..0d04477e --- /dev/null +++ b/src/domod/src/timers/Event.cpp @@ -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()); +} diff --git a/src/domod/src/timers/Event.h b/src/domod/src/timers/Event.h new file mode 100644 index 00000000..690db4fa --- /dev/null +++ b/src/domod/src/timers/Event.h @@ -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 +#include + +#include + +/*---------------------------------- 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 */ diff --git a/src/domod/src/timers/Timers.cpp b/src/domod/src/timers/Timers.cpp index acd0a06b..91c0df4d 100644 --- a/src/domod/src/timers/Timers.cpp +++ b/src/domod/src/timers/Timers.cpp @@ -26,40 +26,57 @@ /*------------------------------- INCLUDES ----------------------------------*/ #include +#include +#include +#include + +#include "models/Devices.h" #include "timers/Timers.h" +//#define k60s 60000 +#define k60s 2000 /*! ---------------------------------------------------------------------------- * @fn Timers * * @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. */ 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; } @@ -72,7 +89,56 @@ int Timers::Setup (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; } + +/* +{ + "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" + } + ] +} +*/ \ No newline at end of file diff --git a/src/domod/src/timers/Timers.h b/src/domod/src/timers/Timers.h index e64867cb..0fb47fff 100644 --- a/src/domod/src/timers/Timers.h +++ b/src/domod/src/timers/Timers.h @@ -28,20 +28,31 @@ /*------------------------------- INCLUDES ----------------------------------*/ +#include +#include + #include "ubuscpp/UBusTimer.h" +/*---------------------------------- Deps -----------------------------------*/ + +class Devices; + +/*--------------------------------- CLASS ----------------------------------*/ + class Timers : public UBusTimer { public: - Timers (void); + Timers (std::string aPath, Devices *aDevice); ~Timers (void); int Expire (void); - int Setup (void); + int Load (void); private: - UBusTimer mTest; + Devices *mDevices; + std::string mTimersPath; + Json::Value mRoot; };