wip on the sequence management

This commit is contained in:
2018-04-19 22:47:41 +02:00
parent 0eafedabe8
commit 265fb457ac
7 changed files with 126 additions and 16 deletions

View File

@@ -57,13 +57,14 @@ bool Clock::is_equal_to_timer(const Timer &a_timer)
{ {
struct tm the_compared_time; struct tm the_compared_time;
double the_diff; double the_diff;
uint8_t the_wday;
time_t the_time = time(NULL); time_t the_time = time(NULL);
the_compared_time = *localtime(&the_time); the_compared_time = *localtime(&the_time);
the_wday = the_compared_time.tm_wday;
printf ("=>Timer: (%.2d h %.2d) Now: (%.2d h %.2d)\n", a_timer.get_hour(),a_timer.get_minute(), printf ("=>Timer: (%.2d h %.2d) Now: (%.2d h %.2d) week day:%d\n", a_timer.get_hour(),a_timer.get_minute(),
the_compared_time.tm_hour, the_compared_time.tm_min); the_compared_time.tm_hour, the_compared_time.tm_min, the_compared_time.tm_wday);
the_compared_time.tm_hour = a_timer.get_hour(); the_compared_time.tm_hour = a_timer.get_hour();
the_compared_time.tm_min = a_timer.get_minute(); the_compared_time.tm_min = a_timer.get_minute();
@@ -71,18 +72,19 @@ bool Clock::is_equal_to_timer(const Timer &a_timer)
the_diff = difftime(the_time, mktime(&the_compared_time)); the_diff = difftime(the_time, mktime(&the_compared_time));
// printf ("%.f seconds of diff between current time and Timer time.\n", the_diff); // printf ("%.f seconds of diff between current time and Timer time.\n", the_diff);
if (the_diff == 0) if (the_diff != 0)
return true; return false;
return false; // the Clock is OK check the recurrence
return a_timer.is_day_active(the_wday);
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn is_expired * @fn is_expired
* *
* @brief Return true if the curent time has expired. * @brief Return true if the curent action has expired.
*/ */
bool Clock::is_expired(uint16_t a_duration) bool Clock::is_expired(const Sequence &a_sequence)
{ {
time_t the_expired; time_t the_expired;
struct tm the_compared_time; struct tm the_compared_time;
@@ -91,14 +93,17 @@ bool Clock::is_expired(uint16_t a_duration)
time_t the_time = time(NULL); time_t the_time = time(NULL);
the_compared_time = *localtime(&the_time); the_compared_time = *localtime(&the_time);
// the_compared_time.tm_hour = m_hour; printf ("=>Now: (%.2d h %.2d)\n", the_compared_time.tm_hour, the_compared_time.tm_min);
// the_compared_time.tm_min = m_minute;
the_expired = mktime(&the_compared_time) + (a_duration * 60);
the_compared_time.tm_hour = a_sequence.get_timer().get_hour();
the_compared_time.tm_min = a_sequence.get_timer().get_minute();
the_expired = mktime(&the_compared_time) + (a_sequence.get_duration() * 60);
the_diff = difftime(the_time, the_expired); the_diff = difftime(the_time, the_expired);
//printf ("%.f seconds of diff between %lld and %lld.\n", theDiff, mNow, theExpired); printf ("%.f seconds of diff between current time and Timer time.\n", the_diff);
if (the_diff == 0) if (the_diff == 0)
return true; return true;

View File

@@ -33,6 +33,7 @@
#include <ctime> #include <ctime>
#include "timer.h" #include "timer.h"
#include "sequence.h"
/*---------------------------------- Deps -----------------------------------*/ /*---------------------------------- Deps -----------------------------------*/
@@ -45,7 +46,7 @@ class Clock
~Clock(void); ~Clock(void);
static bool is_equal_to_timer(const Timer &a_timer); static bool is_equal_to_timer(const Timer &a_timer);
static bool is_expired(uint16_t a_duration); static bool is_expired(const Sequence &a_sequence);
}; };
#endif /* _CLOCK_H */ #endif /* _CLOCK_H */

View File

@@ -37,7 +37,9 @@
* @brief Constructor of the Sequence Object. * @brief Constructor of the Sequence Object.
*/ */
Sequence::Sequence(void) : m_is_in_progress(false), Sequence::Sequence(void) : m_is_in_progress(false),
m_id(0) m_id(0),
m_current_action(0),
m_current_duration(0)
{ {
} }
@@ -160,6 +162,16 @@ const Timer &Sequence::get_timer(void) const
return m_timer; return m_timer;
} }
/*! ----------------------------------------------------------------------------
* @fn get_action
*
* @brief return a reference on the current action.
*/
const Action &Sequence::get_action(void) const
{
return m_actions[m_current_action];
}
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn is_active * @fn is_active
@@ -180,3 +192,45 @@ bool Sequence::is_in_progress(void)
{ {
return m_is_in_progress; return m_is_in_progress;
} }
/*! ----------------------------------------------------------------------------
* @fn get_duration
*
* @brief return current duration of the sequence.
*/
uint16_t Sequence::get_duration(void) const
{
return m_current_duration + get_action().get_duration();
}
/*! ----------------------------------------------------------------------------
* @fn is_in_progress
*
* @brief start the sequence.
*/
void Sequence::start(DevicesManager *a_device_manager)
{
m_is_in_progress = true;
m_current_action = 0;
m_current_duration = 0;
}
/*! ----------------------------------------------------------------------------
* @fn stop
*
* @brief stop the sequence.
*/
void Sequence::stop(void)
{
m_is_in_progress = false;
}
/*! ----------------------------------------------------------------------------
* @fn switch_to_next_action
*
* @brief switch to the next action of the sequence.
*/
bool Sequence::switch_to_next_action(DevicesManager *a_device_manager)
{
return true;
}

View File

@@ -38,6 +38,7 @@
/*---------------------------------- Deps -----------------------------------*/ /*---------------------------------- Deps -----------------------------------*/
struct json_object; struct json_object;
class DevicesManager;
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
@@ -53,16 +54,25 @@ public:
uint16_t get_id(void) const; uint16_t get_id(void) const;
const std::string &get_name(void) const; const std::string &get_name(void) const;
const Timer &get_timer(void) const; const Timer &get_timer(void) const;
const Action &get_action(void) const;
bool is_active(void); bool is_active(void);
bool is_in_progress(void); bool is_in_progress(void);
uint16_t get_duration(void) const;
void start(DevicesManager *a_device_manager);
void stop(void);
bool switch_to_next_action(DevicesManager *a_device_manager);
private: private:
bool m_is_in_progress; bool m_is_in_progress;
uint16_t m_id; uint16_t m_id;
std::string m_name; std::string m_name;
Timer m_timer; Timer m_timer;
std::vector<Action> m_actions; std::vector<Action> m_actions;
uint16_t m_current_action;
uint16_t m_current_duration;
}; };
#endif /* _SEQUENCE_H */ #endif /* _SEQUENCE_H */

View File

@@ -206,13 +206,26 @@ int SequencesManager::expire(void)
printf("sequence %d active.\n", (*the_seq_it).get_id()); printf("sequence %d active.\n", (*the_seq_it).get_id());
if (!(*the_seq_it).is_in_progress()) if (!(*the_seq_it).is_in_progress())
{ {
// should the sequence start ?
if (the_clock.is_equal_to_timer((*the_seq_it).get_timer())) if (the_clock.is_equal_to_timer((*the_seq_it).get_timer()))
{ {
// Action START. // Action START.
fprintf(stdout, "Sequence Start\n"); fprintf(stdout, "Sequence Start\n");
//(*the_seq_it).start(m_devices_manager); (*the_seq_it).start(m_devices_manager);
} }
} }
else
{
// The action is in progress.
if (the_clock.is_expired((*the_seq_it)))
{
if (!(*the_seq_it).switch_to_next_action(m_devices_manager)) {
fprintf(stdout, "Sequence Stopt\n");
(*the_seq_it).stop();
}
}
}
} }
} }

View File

@@ -149,9 +149,34 @@ uint8_t Timer::get_minute(void) const
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn get_recurrence * @fn get_recurrence
* *
* @brief Return the Recurrency of the Timer. * @brief Return the Recurrence of the Timer.
*/ */
uint16_t Timer::get_recurrence(void) const uint16_t Timer::get_recurrence(void) const
{ {
return m_recurrence; return m_recurrence;
} }
/*! ----------------------------------------------------------------------------
* @fn is_day_active
*
* @brief Return true if the current day is active on the timer.
* day mask will be:
* - 1 Sunday
* - 2 Monday
* - 4 Tuesday
* - 8 Wednesday
* -16 Thursday
* -32 Friday
* -64 Saturday
*/
bool Timer::is_day_active(uint8_t a_wday) const
{
printf("debug: mask: %d day active: %s\n", (1 << a_wday), (m_recurrence & (1 << a_wday))?"true":"false");
if (m_recurrence == 0)
{
// No recurrence
return true;
}
return (m_recurrence & (1 << a_wday));
}

View File

@@ -50,6 +50,8 @@ public:
uint8_t get_minute(void) const; uint8_t get_minute(void) const;
uint16_t get_recurrence(void) const; uint16_t get_recurrence(void) const;
bool is_day_active(uint8_t a_wday) const;
private: private:
bool m_active; bool m_active;
uint8_t m_hour; uint8_t m_hour;