Timer WIP: Add Clock comparaison.

This commit is contained in:
jbnadal
2016-05-20 19:03:12 +02:00
parent 1b9f7bc893
commit a299086f88
6 changed files with 157 additions and 34 deletions

View File

@@ -23,8 +23,9 @@ file(
../../src/models/Devices.cpp ../../src/models/Devices.cpp
../../src/helpers/Tokenizer.cpp ../../src/helpers/Tokenizer.cpp
../../src/helpers/Strings.cpp ../../src/helpers/Strings.cpp
../../src/Timers.cpp ../../src/timers/Timers.cpp
../../src/Event.cpp ../../src/timers/Event.cpp
../../src/timers/Clock.cpp
) )
add_executable ( add_executable (

View File

@@ -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 <cstdio>
#include <ctime>
#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;
}

View File

@@ -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 */

View File

@@ -162,7 +162,7 @@ uint16_t Event::getID (void)
* *
* @brief Return the Hour of the Event. * @brief Return the Hour of the Event.
*/ */
uint8_t Event::getHour (void) uint8_t Event::getHour (void) const
{ {
return mHour; return mHour;
} }
@@ -173,7 +173,7 @@ uint8_t Event::getHour (void)
* *
* @brief Return the Minute of the Event. * @brief Return the Minute of the Event.
*/ */
uint8_t Event::getMinute (void) uint8_t Event::getMinute (void) const
{ {
return mMinute; return mMinute;
} }

View File

@@ -51,13 +51,13 @@ public:
/* Getter */ /* Getter */
bool isActive (void); bool isActive (void);
std::string getCapability(void); std::string getCapability (void);
uint16_t getID (void); uint16_t getID (void);
uint8_t getHour(void); uint8_t getHour (void) const;
uint8_t getMinute(void); uint8_t getMinute (void) const;
uint16_t getRecurrence(void); uint16_t getRecurrence (void);
uint16_t getDuration(void); uint16_t getDuration (void);
std::string getAction(void); std::string getAction (void);
private: private:
bool mActive; bool mActive;

View File

@@ -26,17 +26,22 @@
/*------------------------------- INCLUDES ----------------------------------*/ /*------------------------------- INCLUDES ----------------------------------*/
#include <cstdio> #include <cstdio>
#include <ctime>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <vector> #include <vector>
#include "models/Devices.h" #include "models/Devices.h"
#include "timers/Timers.h" #include "timers/Timers.h"
#include "timers/Clock.h"
//#define k60s 60000 //#define k60s 60000
#define k60s 2000 #define k60s 2000
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn Timers * @fn Timers
* *
@@ -110,9 +115,12 @@ int Timers::Save (void)
} }
theRoot["timers"] = timers_json; theRoot["timers"] = timers_json;
/* Save to the File. */ /* Save to the File. */
std::ofstream theOutput (mTimersPath.c_str()); std::ofstream theOutput (mTimersPath.c_str());
theWriter.write (theOutput, theRoot); theWriter.write (theOutput, theRoot);
return 0;
} }
@@ -124,35 +132,22 @@ int Timers::Save (void)
int Timers::Expire (void) int Timers::Expire (void)
{ {
std::vector<Event>::iterator timer_evt; std::vector<Event>::iterator timer_evt;
Clock theClock;
fprintf (stderr, "**** >> Manage timers....\n"); fprintf (stderr, "**** >> Manage timers....\n");
for(timer_evt = mTimers.begin(); timer_evt != mTimers.end(); timer_evt++) { 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; 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"
}
]
}
*/