From a299086f88d47488fc454e6043b85eabc4f90c97 Mon Sep 17 00:00:00 2001 From: jbnadal Date: Fri, 20 May 2016 19:03:12 +0200 Subject: [PATCH] Timer WIP: Add Clock comparaison. --- src/domod/builders/cmake/CMakeLists.txt | 5 +- src/domod/src/timers/Clock.cpp | 82 +++++++++++++++++++++++++ src/domod/src/timers/Clock.h | 45 ++++++++++++++ src/domod/src/timers/Event.cpp | 4 +- src/domod/src/timers/Event.h | 12 ++-- src/domod/src/timers/Timers.cpp | 43 ++++++------- 6 files changed, 157 insertions(+), 34 deletions(-) create mode 100644 src/domod/src/timers/Clock.cpp create mode 100644 src/domod/src/timers/Clock.h diff --git a/src/domod/builders/cmake/CMakeLists.txt b/src/domod/builders/cmake/CMakeLists.txt index 1436f1d6..b967e39d 100644 --- a/src/domod/builders/cmake/CMakeLists.txt +++ b/src/domod/builders/cmake/CMakeLists.txt @@ -23,8 +23,9 @@ file( ../../src/models/Devices.cpp ../../src/helpers/Tokenizer.cpp ../../src/helpers/Strings.cpp - ../../src/Timers.cpp - ../../src/Event.cpp + ../../src/timers/Timers.cpp + ../../src/timers/Event.cpp + ../../src/timers/Clock.cpp ) add_executable ( diff --git a/src/domod/src/timers/Clock.cpp b/src/domod/src/timers/Clock.cpp new file mode 100644 index 00000000..d926cdaf --- /dev/null +++ b/src/domod/src/timers/Clock.cpp @@ -0,0 +1,82 @@ +/*! + * Clock.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: 20/05/2016 + * + */ +/*------------------------------- INCLUDES ----------------------------------*/ + +#include +#include + +#include "timers/Clock.h" + + +/*! ---------------------------------------------------------------------------- + * @fn Clock + * + * @brief Constructor of the Clock Object. + */ +Clock::Clock (void) +{ +} + + +/*! ---------------------------------------------------------------------------- + * @fn ~Clock + * + * @brief Destructor of the Clock Object. + */ +Clock::~Clock (void) +{ +} + + +/*! ---------------------------------------------------------------------------- + * @fn isEqual + * + * @brief Constructor of the Clock Object. + */ +bool Clock::isCurrentTimeEqualTo (const Event &anEvent) +{ + time_t theNow; + struct tm *theNow_tm; + struct tm theComparedTime; + int theHour, theMinute; + double theDiff; + + theNow = time (NULL); + + theNow_tm = localtime (&theNow); + theComparedTime = *localtime (&theNow); + + theComparedTime.tm_hour = anEvent.getHour(); + theComparedTime.tm_min = anEvent.getMinute(); + + theDiff = difftime (theNow, mktime (&theComparedTime)); + + //printf ("%.f seconds of diff between current time and Timer time.\n", theDiff); + + if (theDiff == 0) + return true; + + return false; +} diff --git a/src/domod/src/timers/Clock.h b/src/domod/src/timers/Clock.h new file mode 100644 index 00000000..b80b9851 --- /dev/null +++ b/src/domod/src/timers/Clock.h @@ -0,0 +1,45 @@ +/*! + * Clock.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: 20/05/2016 + * + */ + +#ifndef _CLOCK_H +#define _CLOCK_H + +/*------------------------------- INCLUDES ----------------------------------*/ + +#include "timers/Event.h" + +/*---------------------------------- Deps -----------------------------------*/ + + +/*--------------------------------- CLASS ----------------------------------*/ + +class Clock { +public: + Clock (void); + ~Clock (void); + bool isCurrentTimeEqualTo (const Event &anEvent); +}; + +#endif /* _CLOCK_H */ diff --git a/src/domod/src/timers/Event.cpp b/src/domod/src/timers/Event.cpp index 2a074141..ccbb2ef9 100644 --- a/src/domod/src/timers/Event.cpp +++ b/src/domod/src/timers/Event.cpp @@ -162,7 +162,7 @@ uint16_t Event::getID (void) * * @brief Return the Hour of the Event. */ -uint8_t Event::getHour (void) +uint8_t Event::getHour (void) const { return mHour; } @@ -173,7 +173,7 @@ uint8_t Event::getHour (void) * * @brief Return the Minute of the Event. */ -uint8_t Event::getMinute (void) +uint8_t Event::getMinute (void) const { return mMinute; } diff --git a/src/domod/src/timers/Event.h b/src/domod/src/timers/Event.h index 05054a3b..f4fe8911 100644 --- a/src/domod/src/timers/Event.h +++ b/src/domod/src/timers/Event.h @@ -51,13 +51,13 @@ public: /* Getter */ bool isActive (void); - std::string getCapability(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); + uint8_t getHour (void) const; + uint8_t getMinute (void) const; + uint16_t getRecurrence (void); + uint16_t getDuration (void); + std::string getAction (void); private: bool mActive; diff --git a/src/domod/src/timers/Timers.cpp b/src/domod/src/timers/Timers.cpp index f1c917d5..1938f6fe 100644 --- a/src/domod/src/timers/Timers.cpp +++ b/src/domod/src/timers/Timers.cpp @@ -26,17 +26,22 @@ /*------------------------------- INCLUDES ----------------------------------*/ #include +#include + #include #include #include + #include "models/Devices.h" #include "timers/Timers.h" +#include "timers/Clock.h" //#define k60s 60000 #define k60s 2000 + /*! ---------------------------------------------------------------------------- * @fn Timers * @@ -110,9 +115,12 @@ int Timers::Save (void) } theRoot["timers"] = timers_json; + /* Save to the File. */ std::ofstream theOutput (mTimersPath.c_str()); theWriter.write (theOutput, theRoot); + + return 0; } @@ -124,35 +132,22 @@ int Timers::Save (void) int Timers::Expire (void) { std::vector::iterator timer_evt; + Clock theClock; fprintf (stderr, "**** >> Manage timers....\n"); for(timer_evt = mTimers.begin(); timer_evt != mTimers.end(); timer_evt++) { - (*timer_evt).Dump(); + if ((*timer_evt).isActive()) { + + if (theClock.isCurrentTimeEqualTo ((*timer_evt))) { + + fprintf (stderr, "Time identique\n"); + } + else { + fprintf (stderr, "Time PAS identique\n"); + } + } } 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