From 956de5a52bbb92a6e3d6b34e6cbd72b4ad28b571 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 24 May 2022 11:20:07 +0200 Subject: [PATCH 1/2] TimeService: Add connected() method to check connection status --- src/utility/time/TimeService.cpp | 9 +++++++++ src/utility/time/TimeService.h | 1 + 2 files changed, 10 insertions(+) diff --git a/src/utility/time/TimeService.cpp b/src/utility/time/TimeService.cpp index 112745d06..a229de29b 100644 --- a/src/utility/time/TimeService.cpp +++ b/src/utility/time/TimeService.cpp @@ -192,6 +192,15 @@ unsigned long TimeService::getTimeFromString(const String& input) * PRIVATE MEMBER FUNCTIONS **************************************************************************************/ +bool TimeService::connected() +{ + if(_con_hdl == nullptr) { + return false; + } else { + return _con_hdl->getStatus() == NetworkConnectionState::CONNECTED; + } +} + unsigned long TimeService::getRemoteTime() { #include "../../AIoTC_Config.h" diff --git a/src/utility/time/TimeService.h b/src/utility/time/TimeService.h index fbe81986d..8d8862287 100644 --- a/src/utility/time/TimeService.h +++ b/src/utility/time/TimeService.h @@ -64,6 +64,7 @@ class TimeService unsigned long _timezone_dst_until; unsigned long getRemoteTime(); + bool connected(); static bool isTimeValid(unsigned long const time); }; From 398bef817d477e79cea3eb207bbe1eb3df9c60b4 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 24 May 2022 11:23:21 +0200 Subject: [PATCH 2/2] Check if board is connected before trying to get time from network/NTP --- src/utility/time/TimeService.cpp | 35 ++++++++++++++++---------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/utility/time/TimeService.cpp b/src/utility/time/TimeService.cpp index a229de29b..88c60a1a2 100644 --- a/src/utility/time/TimeService.cpp +++ b/src/utility/time/TimeService.cpp @@ -206,27 +206,26 @@ unsigned long TimeService::getRemoteTime() #include "../../AIoTC_Config.h" #ifndef HAS_LORA - if(_con_hdl == nullptr) - return EPOCH_AT_COMPILE_TIME; - - /* At first try to see if a valid time can be obtained - * using the network time available via the connection - * handler. - */ - unsigned long const connection_time = _con_hdl->getTime(); - if(isTimeValid(connection_time)) { - return connection_time; - } + if(connected()) { + /* At first try to see if a valid time can be obtained + * using the network time available via the connection + * handler. + */ + unsigned long const connection_time = _con_hdl->getTime(); + if(isTimeValid(connection_time)) { + return connection_time; + } #ifndef __AVR__ - /* If no valid network time is available try to obtain the - * time via NTP next. - */ - unsigned long const ntp_time = NTPUtils::getTime(_con_hdl->getUDP()); - if(isTimeValid(ntp_time)) { - return ntp_time; - } + /* If no valid network time is available try to obtain the + * time via NTP next. + */ + unsigned long const ntp_time = NTPUtils::getTime(_con_hdl->getUDP()); + if(isTimeValid(ntp_time)) { + return ntp_time; + } #endif + } #endif /* ifndef HAS_LORA */