From 13dd7d432b4822ef7d01499a40aa1337f294295f Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Sat, 14 Apr 2018 00:02:50 +0200 Subject: [PATCH] alarm wip --- src/prog/domod/src/main.cpp | 2 - src/prog/domod/src/sequences/clock.cpp | 39 +++++++++---------- src/prog/domod/src/sequences/clock.h | 12 +++--- src/prog/domod/src/sequences/sequence.cpp | 34 +++++++++++++++- src/prog/domod/src/sequences/sequence.h | 5 +++ .../domod/src/sequences/sequences-manager.cpp | 27 +++++++++++-- 6 files changed, 88 insertions(+), 31 deletions(-) diff --git a/src/prog/domod/src/main.cpp b/src/prog/domod/src/main.cpp index 8a008ee9..01a18d06 100644 --- a/src/prog/domod/src/main.cpp +++ b/src/prog/domod/src/main.cpp @@ -155,8 +155,6 @@ int main(int argc, char *argv[]) CapabilitiesShuttersModel the_cap_shutters(&the_devices_manager); CapabilitiesSprinklersModel the_cap_sprinklers(&the_devices_manager); - return 0; - /* Setup the Ubus context. */ the_ctx = setup_ubus(); if (the_ctx == NULL) diff --git a/src/prog/domod/src/sequences/clock.cpp b/src/prog/domod/src/sequences/clock.cpp index 7ad371d2..fe1a6724 100644 --- a/src/prog/domod/src/sequences/clock.cpp +++ b/src/prog/domod/src/sequences/clock.cpp @@ -48,39 +48,38 @@ Clock::~Clock(void) } /*! ---------------------------------------------------------------------------- - * @fn isEqual + * @fn set * - * @brief Set an Event to the Clock. + * @brief Set a time (hour/minute) to the Clock. */ -#if 0 -int Clock::set(const Event &an_event) +int Clock::set(void) { - m_event = an_event; - m_now = time(NULL); return 0; } -#endif + /*! ---------------------------------------------------------------------------- - * @fn is_equal_to_current_time + * @fn is_equal_to_timer * * @brief return true if the clock is equal to current time. */ -bool Clock::is_equal_to_current_time(void) +bool Clock::is_equal_to_timer(const Timer &a_timer) { struct tm the_compared_time; double the_diff; the_compared_time = *localtime(&m_now); -#if 0 - the_compared_time.tm_hour = m_event.get_hour(); - the_compared_time.tm_min = m_event.get_minute(); -#endif + printf ("=>Hour: %.2d h %.2d\n", the_compared_time.tm_hour, the_compared_time.tm_min); + + the_compared_time.tm_hour = a_timer.get_hour(); + the_compared_time.tm_min = a_timer.get_minute(); + the_diff = difftime(m_now, mktime(&the_compared_time)); - //printf ("%.f seconds of diff between current time and Timer time.\n", theDiff); + printf ("=>Timer: %.2d h %.2d\n", a_timer.get_hour(),a_timer.get_minute()); + printf ("%.f seconds of diff between current time and Timer time.\n", the_diff); if (the_diff == 0) return true; @@ -93,19 +92,19 @@ bool Clock::is_equal_to_current_time(void) * * @brief Return true if the curent time has expired. */ -bool Clock::is_expired(void) +bool Clock::is_expired(uint16_t a_duration) { time_t the_expired; struct tm the_compared_time; double the_diff; the_compared_time = *localtime(&m_now); -#if 0 - the_compared_time.tm_hour = m_event.get_hour(); - the_compared_time.tm_min = m_event.get_minute(); - the_expired = mktime(&the_compared_time) + (m_event.get_duration() * 60); -#endif + // the_compared_time.tm_hour = m_hour; + // the_compared_time.tm_min = m_minute; + + the_expired = mktime(&the_compared_time) + (a_duration * 60); + the_diff = difftime(m_now, the_expired); //printf ("%.f seconds of diff between %lld and %lld.\n", theDiff, mNow, theExpired); diff --git a/src/prog/domod/src/sequences/clock.h b/src/prog/domod/src/sequences/clock.h index e1a60776..405ef05f 100644 --- a/src/prog/domod/src/sequences/clock.h +++ b/src/prog/domod/src/sequences/clock.h @@ -28,9 +28,11 @@ /*------------------------------- INCLUDES ----------------------------------*/ +#include + #include -//#include "timers/event.h" +#include "timer.h" /*---------------------------------- Deps -----------------------------------*/ @@ -42,12 +44,12 @@ class Clock Clock(void); ~Clock(void); - //int set(const Event &an_event); - bool is_equal_to_current_time(void); - bool is_expired(void); + int set(void); + + bool is_equal_to_timer(const Timer &a_timer); + bool is_expired(uint16_t a_duration); private: - //Event m_event; time_t m_now; }; diff --git a/src/prog/domod/src/sequences/sequence.cpp b/src/prog/domod/src/sequences/sequence.cpp index 92ead3f5..5b5426d2 100644 --- a/src/prog/domod/src/sequences/sequence.cpp +++ b/src/prog/domod/src/sequences/sequence.cpp @@ -36,7 +36,8 @@ * * @brief Constructor of the Sequence Object. */ -Sequence::Sequence(void) : m_id(0) +Sequence::Sequence(void) : m_is_in_progress(false), + m_id(0) { } @@ -148,3 +149,34 @@ const std::string &Sequence::get_name(void) const { return m_name; } + +/*! ---------------------------------------------------------------------------- + * @fn get_timer + * + * @brief return the timer of the Sequence. + */ +const Timer &Sequence::get_timer(void) const +{ + return m_timer; +} + + +/*! ---------------------------------------------------------------------------- + * @fn is_active + * + * @brief return true if the sequence has an active timer. false otherwise. + */ +bool Sequence::is_active(void) +{ + return m_timer.is_active(); +} + +/*! ---------------------------------------------------------------------------- + * @fn is_in_progress + * + * @brief return true if the sequence has been started.. + */ +bool Sequence::is_in_progress(void) +{ + return m_is_in_progress; +} diff --git a/src/prog/domod/src/sequences/sequence.h b/src/prog/domod/src/sequences/sequence.h index 413b99a5..1bb1099d 100644 --- a/src/prog/domod/src/sequences/sequence.h +++ b/src/prog/domod/src/sequences/sequence.h @@ -52,8 +52,13 @@ public: uint16_t get_id(void) const; const std::string &get_name(void) const; + const Timer &get_timer(void) const; + + bool is_active(void); + bool is_in_progress(void); private: + bool m_is_in_progress; uint16_t m_id; std::string m_name; Timer m_timer; diff --git a/src/prog/domod/src/sequences/sequences-manager.cpp b/src/prog/domod/src/sequences/sequences-manager.cpp index 841a2225..89397049 100644 --- a/src/prog/domod/src/sequences/sequences-manager.cpp +++ b/src/prog/domod/src/sequences/sequences-manager.cpp @@ -41,7 +41,6 @@ //#define k60s 60000 #define k60s 2000 - /*! ---------------------------------------------------------------------------- * @fn SequencesManager * @@ -96,6 +95,8 @@ int SequencesManager::load(void) /* Clean the json object. */ json_object_put(the_root_node); + tzset (); + start(k60s, true); return 0; @@ -114,7 +115,7 @@ int SequencesManager::save(void) the_root_node = to_json(); the_result = json_object_to_file(m_sequences_file_path.c_str(), the_root_node); - + /* Clean the json object. */ json_object_put(the_root_node); @@ -198,6 +199,26 @@ int SequencesManager::expire(void) Clock the_clock; fprintf(stderr, "**** >> Manage timers....\n"); + for (the_seq_it = m_sequences.begin(); the_seq_it != m_sequences.end(); the_seq_it++) + { + if ((*the_seq_it).is_active()) + { + printf("sequence %d active.\n", (*the_seq_it).get_id()); + if (!(*the_seq_it).is_in_progress()) + { + 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); + } + } + } + } + + return 0; +} + #if 0 for (the_seq_it = m_sequences.begin(); the_seq_it != m_sequences.end(); the_seq_it++) { @@ -231,6 +252,6 @@ int SequencesManager::expire(void) } } } -#endif return 0; } +#endif