Skip to content

Commit a594d95

Browse files
authored
Merge pull request #317 from pennam/esp32_init
TimeService: check connection status before network/ntp time request
2 parents 451d57d + 398bef8 commit a594d95

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

src/utility/time/TimeService.cpp

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -192,32 +192,40 @@ unsigned long TimeService::getTimeFromString(const String& input)
192192
* PRIVATE MEMBER FUNCTIONS
193193
**************************************************************************************/
194194

195+
bool TimeService::connected()
196+
{
197+
if(_con_hdl == nullptr) {
198+
return false;
199+
} else {
200+
return _con_hdl->getStatus() == NetworkConnectionState::CONNECTED;
201+
}
202+
}
203+
195204
unsigned long TimeService::getRemoteTime()
196205
{
197206
#include "../../AIoTC_Config.h"
198207
#ifndef HAS_LORA
199208

200-
if(_con_hdl == nullptr)
201-
return EPOCH_AT_COMPILE_TIME;
202-
203-
/* At first try to see if a valid time can be obtained
204-
* using the network time available via the connection
205-
* handler.
206-
*/
207-
unsigned long const connection_time = _con_hdl->getTime();
208-
if(isTimeValid(connection_time)) {
209-
return connection_time;
210-
}
209+
if(connected()) {
210+
/* At first try to see if a valid time can be obtained
211+
* using the network time available via the connection
212+
* handler.
213+
*/
214+
unsigned long const connection_time = _con_hdl->getTime();
215+
if(isTimeValid(connection_time)) {
216+
return connection_time;
217+
}
211218

212219
#ifndef __AVR__
213-
/* If no valid network time is available try to obtain the
214-
* time via NTP next.
215-
*/
216-
unsigned long const ntp_time = NTPUtils::getTime(_con_hdl->getUDP());
217-
if(isTimeValid(ntp_time)) {
218-
return ntp_time;
219-
}
220+
/* If no valid network time is available try to obtain the
221+
* time via NTP next.
222+
*/
223+
unsigned long const ntp_time = NTPUtils::getTime(_con_hdl->getUDP());
224+
if(isTimeValid(ntp_time)) {
225+
return ntp_time;
226+
}
220227
#endif
228+
}
221229

222230
#endif /* ifndef HAS_LORA */
223231

src/utility/time/TimeService.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class TimeService
6464
unsigned long _timezone_dst_until;
6565

6666
unsigned long getRemoteTime();
67+
bool connected();
6768
static bool isTimeValid(unsigned long const time);
6869

6970
};

0 commit comments

Comments
 (0)