From 2125dcb7b82b2dee532a1344b0291a3d3cbabdd4 Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Tue, 24 May 2016 23:10:17 +0200 Subject: [PATCH] Timer Event now could change the devices state. --- src/domod/src/devices/Devices.cpp | 29 +++++++++++++++++++++++++ src/domod/src/devices/Devices.h | 3 ++- src/domod/src/devices/ShutterDevice.cpp | 11 ++++++++++ src/domod/src/devices/ShutterDevice.h | 3 ++- src/domod/src/timers/Event.cpp | 28 ++++++++++++++++++++++++ src/domod/src/timers/Event.h | 4 ++++ src/domod/src/timers/Timers.cpp | 8 +++---- 7 files changed, 80 insertions(+), 6 deletions(-) diff --git a/src/domod/src/devices/Devices.cpp b/src/domod/src/devices/Devices.cpp index bcba3981..0456084e 100644 --- a/src/domod/src/devices/Devices.cpp +++ b/src/domod/src/devices/Devices.cpp @@ -166,3 +166,32 @@ int Devices::set (const std::string &aCapability, Json::Value anElement) return theResult; } + + +/*! ---------------------------------------------------------------------------- + * @fn set + * + * @brief get the device of a specific capabilities, with the ID. + */ +int Devices::set (const std::string &aCapability, int anID, bool aState) +{ + int theResult = -1; + + if (aCapability == "Lights") { + + theResult = mLights.set (anID, aState); + } + else if (aCapability == "Shutters") { + + theResult = mShutters.set (anID, aState); + } + else if (aCapability == "Sprinklers") { + + theResult = mSprinklers.set (anID, aState); + } + + if (theResult == 0) + save (); + + return theResult; +} diff --git a/src/domod/src/devices/Devices.h b/src/domod/src/devices/Devices.h index da5ce85a..e3c6741a 100644 --- a/src/domod/src/devices/Devices.h +++ b/src/domod/src/devices/Devices.h @@ -52,7 +52,8 @@ public: std::string get (const std::string &aCapability); int set (const std::string &aCapability, Json::Value anElement); - + int set (const std::string &aCapability, int anID, bool aState); + private: std::string mFilePath; diff --git a/src/domod/src/devices/ShutterDevice.cpp b/src/domod/src/devices/ShutterDevice.cpp index 10c81597..fe5800d0 100644 --- a/src/domod/src/devices/ShutterDevice.cpp +++ b/src/domod/src/devices/ShutterDevice.cpp @@ -112,3 +112,14 @@ int ShutterDevice::set (Json::Value anElement) { return 0; } + + +/*! ---------------------------------------------------------------------------- + * @fn set + * + * @brief set the new Shutter state. + */ +int ShutterDevice::set (int anID, bool aState) +{ + return 0; +} diff --git a/src/domod/src/devices/ShutterDevice.h b/src/domod/src/devices/ShutterDevice.h index 3c01b8d4..e9af98d8 100644 --- a/src/domod/src/devices/ShutterDevice.h +++ b/src/domod/src/devices/ShutterDevice.h @@ -46,8 +46,9 @@ public: int load_from_json (Json::Value anElem); Json::Value to_json (bool bDataOnly = false); - + int set (Json::Value anElement); + int set (int anID, bool aState); private: std::vector mShutter; diff --git a/src/domod/src/timers/Event.cpp b/src/domod/src/timers/Event.cpp index fa6a2f56..1cd3d7a8 100644 --- a/src/domod/src/timers/Event.cpp +++ b/src/domod/src/timers/Event.cpp @@ -26,6 +26,8 @@ /*------------------------------- INCLUDES ----------------------------------*/ +#include "devices/Devices.h" + #include "timers/Event.h" /*! ---------------------------------------------------------------------------- @@ -111,6 +113,32 @@ Json::Value Event::to_json (void) const } +/*! ---------------------------------------------------------------------------- + * @fn start + * + * @brief Stop the Event. + */ +int Event::start (Devices *aDevices) +{ + setInProgress (true); + + return aDevices->set (mCapability.c_str(), mID, true); +} + + +/*! ---------------------------------------------------------------------------- + * @fn stop + * + * @brief Stop the Event. + */ +int Event::stop (Devices *aDevices) +{ + setInProgress (false); + + return aDevices->set (mCapability.c_str(), mID, false); +} + + /*! ---------------------------------------------------------------------------- * @fn dump * diff --git a/src/domod/src/timers/Event.h b/src/domod/src/timers/Event.h index 2e01ccba..37379279 100644 --- a/src/domod/src/timers/Event.h +++ b/src/domod/src/timers/Event.h @@ -35,6 +35,7 @@ /*---------------------------------- Deps -----------------------------------*/ +class Devices; /*--------------------------------- CLASS ----------------------------------*/ @@ -46,6 +47,9 @@ public: int load_from_json (Json::Value anElem); Json::Value to_json (void) const; + + int start (Devices *aDevices); + int stop (Devices *aDevices); void dump (void); diff --git a/src/domod/src/timers/Timers.cpp b/src/domod/src/timers/Timers.cpp index 90ceb150..50d4297e 100644 --- a/src/domod/src/timers/Timers.cpp +++ b/src/domod/src/timers/Timers.cpp @@ -141,10 +141,10 @@ int Timers::Expire (void) if ((*timer_evt).isInProgress()) { if (theClock.isExpired()) { - // - (*timer_evt).setInProgress (false); - //TODO Action Stop. + + // Action Stop. fprintf (stdout, "Timer Stop\n"); + (*timer_evt).stop (mDevices); } else { fprintf (stdout, "Not expired yet.\n"); @@ -154,9 +154,9 @@ int Timers::Expire (void) if (theClock.isEqualToCurrentTime ()) { - (*timer_evt).setInProgress (true); // Action START. fprintf (stdout, "Timer Start\n"); + (*timer_evt).start (mDevices); } } }