diff --git a/clock.cpp b/clock.cpp index e98857a..881cd71 100644 --- a/clock.cpp +++ b/clock.cpp @@ -10,7 +10,7 @@ Clock::Clock(ClockSettings const& settings): m_settings(settings), - m_last_NTP_check(0), + m_next_NTP_check(0), m_NTP_waiting_for_response(false), m_UDP(), m_NTP_ip(), @@ -65,14 +65,8 @@ void Clock::ntp_update() bool need_to_ask = false; if(timeStatus() != timeSet) need_to_ask = !m_NTP_waiting_for_response; - else if(m_NTP_waiting_for_response) - { - //5 minutes timeout for the NTP response - if((now() - m_last_NTP_check) > 5*60) - m_NTP_waiting_for_response = false; - } else - need_to_ask = (now() - m_last_NTP_check) > m_settings.cooldown(); + need_to_ask = now() > m_next_NTP_check; if(need_to_ask) ntp_ask(); ntp_checkResponse(); @@ -100,7 +94,7 @@ void Clock::ntp_ask() m_UDP.write(m_NTP_buffer, NTP_BUFFER_SIZE); m_UDP.endPacket(); - m_last_NTP_check = now(); + m_next_NTP_check = now() + NTP_RETRY_COOLDOWN; m_NTP_waiting_for_response = true; } @@ -121,7 +115,7 @@ void Clock::ntp_checkResponse() time_t UNIX_time = NTP_time - 2208988800UL; - m_last_NTP_check = UNIX_time; + m_next_NTP_check = UNIX_time + m_settings.cooldown(); RTC.set(UNIX_time); setTime(UNIX_time); diff --git a/clock.h b/clock.h index 13a896e..4f711a9 100644 --- a/clock.h +++ b/clock.h @@ -7,6 +7,7 @@ unsigned int const NTP_BUFFER_SIZE = 48; unsigned int const NTP_PORT = 123; +unsigned int const NTP_RETRY_COOLDOWN = 5*60; unsigned int const TZ_RETRY_COOLDOWN = 60; class Clock @@ -31,7 +32,7 @@ class Clock protected: ClockSettings const& m_settings; - time_t m_last_NTP_check; + time_t m_next_NTP_check; bool m_NTP_waiting_for_response; WiFiUDP m_UDP;