diff --git a/src/prog/domod/src/sequences/clock.cpp b/src/prog/domod/src/sequences/clock.cpp index c63c9a9b..efb684fa 100644 --- a/src/prog/domod/src/sequences/clock.cpp +++ b/src/prog/domod/src/sequences/clock.cpp @@ -57,13 +57,14 @@ bool Clock::is_equal_to_timer(const Timer &a_timer) { struct tm the_compared_time; double the_diff; - + uint8_t the_wday; time_t the_time = time(NULL); 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(), - the_compared_time.tm_hour, the_compared_time.tm_min); + 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_wday); the_compared_time.tm_hour = a_timer.get_hour(); 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)); // printf ("%.f seconds of diff between current time and Timer time.\n", the_diff); - if (the_diff == 0) - return true; + if (the_diff != 0) + return false; - return false; + // the Clock is OK check the recurrence + return a_timer.is_day_active(the_wday); } /*! ---------------------------------------------------------------------------- * @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; struct tm the_compared_time; @@ -91,14 +93,17 @@ bool Clock::is_expired(uint16_t a_duration) time_t the_time = time(NULL); the_compared_time = *localtime(&the_time); - // the_compared_time.tm_hour = m_hour; - // the_compared_time.tm_min = m_minute; + printf ("=>Now: (%.2d h %.2d)\n", the_compared_time.tm_hour, the_compared_time.tm_min); - 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); - //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) return true; diff --git a/src/prog/domod/src/sequences/clock.h b/src/prog/domod/src/sequences/clock.h index 67f15965..e3736267 100644 --- a/src/prog/domod/src/sequences/clock.h +++ b/src/prog/domod/src/sequences/clock.h @@ -33,6 +33,7 @@ #include #include "timer.h" +#include "sequence.h" /*---------------------------------- Deps -----------------------------------*/ @@ -45,7 +46,7 @@ class Clock ~Clock(void); 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 */ diff --git a/src/prog/domod/src/sequences/sequence.cpp b/src/prog/domod/src/sequences/sequence.cpp index 5b5426d2..f556fe39 100644 --- a/src/prog/domod/src/sequences/sequence.cpp +++ b/src/prog/domod/src/sequences/sequence.cpp @@ -37,7 +37,9 @@ * @brief Constructor of the Sequence Object. */ 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; } +/*! ---------------------------------------------------------------------------- + * @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 @@ -180,3 +192,45 @@ bool Sequence::is_in_progress(void) { 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; +} diff --git a/src/prog/domod/src/sequences/sequence.h b/src/prog/domod/src/sequences/sequence.h index 1bb1099d..41b8bf02 100644 --- a/src/prog/domod/src/sequences/sequence.h +++ b/src/prog/domod/src/sequences/sequence.h @@ -38,6 +38,7 @@ /*---------------------------------- Deps -----------------------------------*/ struct json_object; +class DevicesManager; /*--------------------------------- CLASS ----------------------------------*/ @@ -53,16 +54,25 @@ public: uint16_t get_id(void) const; const std::string &get_name(void) const; const Timer &get_timer(void) const; + const Action &get_action(void) const; bool is_active(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: bool m_is_in_progress; uint16_t m_id; std::string m_name; Timer m_timer; std::vector m_actions; + uint16_t m_current_action; + uint16_t m_current_duration; }; #endif /* _SEQUENCE_H */ diff --git a/src/prog/domod/src/sequences/sequences-manager.cpp b/src/prog/domod/src/sequences/sequences-manager.cpp index 89397049..e4427b5f 100644 --- a/src/prog/domod/src/sequences/sequences-manager.cpp +++ b/src/prog/domod/src/sequences/sequences-manager.cpp @@ -206,12 +206,25 @@ int SequencesManager::expire(void) printf("sequence %d active.\n", (*the_seq_it).get_id()); if (!(*the_seq_it).is_in_progress()) { + // should the sequence start ? if (the_clock.is_equal_to_timer((*the_seq_it).get_timer())) { // Action START. 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(); + } + } + } } } diff --git a/src/prog/domod/src/sequences/timer.cpp b/src/prog/domod/src/sequences/timer.cpp index 1a57d4e6..36dee5ae 100644 --- a/src/prog/domod/src/sequences/timer.cpp +++ b/src/prog/domod/src/sequences/timer.cpp @@ -149,9 +149,34 @@ uint8_t Timer::get_minute(void) const /*! ---------------------------------------------------------------------------- * @fn get_recurrence * - * @brief Return the Recurrency of the Timer. + * @brief Return the Recurrence of the Timer. */ uint16_t Timer::get_recurrence(void) const { 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)); +} diff --git a/src/prog/domod/src/sequences/timer.h b/src/prog/domod/src/sequences/timer.h index a19b9447..900014f8 100644 --- a/src/prog/domod/src/sequences/timer.h +++ b/src/prog/domod/src/sequences/timer.h @@ -50,6 +50,8 @@ public: uint8_t get_minute(void) const; uint16_t get_recurrence(void) const; + bool is_day_active(uint8_t a_wday) const; + private: bool m_active; uint8_t m_hour;