@@ -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+
195204unsigned 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
0 commit comments