Browse Source

NTP next timestamp instead of last timestamp

master
n0m1s 6 years ago
parent
commit
aa1434bdb8
2 changed files with 6 additions and 11 deletions
  1. +4
    -10
      clock.cpp
  2. +2
    -1
      clock.h

+ 4
- 10
clock.cpp View File

@ -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);


+ 2
- 1
clock.h View File

@ -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;


Loading…
Cancel
Save