3
3
#include " ConfigManager.h"
4
4
#include " config_helper.h"
5
5
#include < ArduinoJson.h>
6
+ #include < ArduinoLog.h>
6
7
7
8
GlobalTime *GlobalTime::m_instance = nullptr ;
8
9
@@ -13,7 +14,7 @@ GlobalTime::GlobalTime() {
13
14
m_ntpServer = cm->getConfigString (" ntpServer" , m_ntpServer); // config added in MainHelper
14
15
Serial.printf (" GlobalTime initialized, tzLoc=%s, clockFormat=%d, ntpServer=%s\n " , m_timezoneLocation.c_str (), clockFormat, m_ntpServer.c_str ());
15
16
m_format24hour = (clockFormat == CLOCK_FORMAT_24_HOUR);
16
- m_timeClient = new NTPClient (m_udp, m_ntpServer.c_str ());
17
+ m_timeClient = new NTPClient (m_udp, m_ntpServer.c_str (), 0 , m_updateInterval );
17
18
m_timeClient->begin ();
18
19
}
19
20
@@ -33,8 +34,17 @@ void GlobalTime::updateTime(bool force) {
33
34
if (m_timeZoneOffset == -1 || (m_nextTimeZoneUpdate > 0 && m_unixEpoch > m_nextTimeZoneUpdate)) {
34
35
getTimeZoneOffsetFromAPI ();
35
36
}
36
- m_timeClient->update ();
37
+ bool updated = m_timeClient->update ();
37
38
m_unixEpoch = m_timeClient->getEpochTime ();
39
+ if (updated) {
40
+ if (year (m_unixEpoch) < m_lowYearTest || year (m_unixEpoch) > m_highYearTest) {
41
+ Log.warningln (" NTP updated but year is not in acceptable range, skipping update, trying again..." );
42
+ m_timeClient->setUpdateInterval (3000 ); // try again in 3 seconds
43
+ return ;
44
+ }
45
+ Log.noticeln (" NTP updated successful - Year is: %d" , year (m_unixEpoch));
46
+ m_timeClient->setUpdateInterval (m_updateInterval); // reset update interval
47
+ }
38
48
m_updateTimer = millis ();
39
49
m_minute = minute (m_unixEpoch);
40
50
if (m_format24hour) {
@@ -173,4 +183,4 @@ bool GlobalTime::getFormat24Hour() {
173
183
bool GlobalTime::setFormat24Hour (bool format24hour) {
174
184
m_format24hour = format24hour;
175
185
return m_format24hour;
176
- }
186
+ }
0 commit comments