@@ -116,14 +116,17 @@ static time_t const EPOCH = 0;
116116 **************************************************************************************/
117117
118118TimeServiceClass::TimeServiceClass ()
119- : _con_hdl(nullptr )
120- , _is_rtc_configured(false )
119+ : _is_rtc_configured(false )
121120, _is_tz_configured(false )
122121, _timezone_offset(24 * 60 * 60 )
123122, _timezone_dst_until(0 )
124123, _last_sync_tick(0 )
125124, _sync_interval_ms(TIMESERVICE_NTP_SYNC_TIMEOUT_ms)
126125, _sync_func(nullptr )
126+ #if CONNECTION_HANDLER_ENABLED
127+ , _con_hdl(nullptr )
128+ #endif
129+ , _ntp_client(nullptr )
127130{
128131
129132}
@@ -132,9 +135,26 @@ TimeServiceClass::TimeServiceClass()
132135 * PUBLIC MEMBER FUNCTIONS
133136 **************************************************************************************/
134137
138+ #if CONNECTION_HANDLER_ENABLED
135139void TimeServiceClass::begin (ConnectionHandler * con_hdl)
136140{
137141 _con_hdl = con_hdl;
142+ #ifdef HAS_TCP
143+ begin (&_con_hdl->getUDP ());
144+ #else
145+ begin ();
146+ #endif
147+ }
148+ #endif
149+
150+ void TimeServiceClass::begin (UDP * ntp_client)
151+ {
152+ _ntp_client = ntp_client;
153+ begin ();
154+ }
155+
156+ void TimeServiceClass::begin ()
157+ {
138158 initRTC ();
139159#ifdef HAS_LORA
140160 setRTC (EPOCH_AT_COMPILE_TIME);
@@ -293,11 +313,12 @@ unsigned long TimeServiceClass::getTimeFromString(const String& input)
293313#if defined(HAS_NOTECARD) || defined(HAS_TCP)
294314bool TimeServiceClass::connected ()
295315{
296- if (_con_hdl == nullptr ) {
297- return false ;
298- } else {
316+ #if CONNECTION_HANDLER_ENABLED
317+ if (_con_hdl != nullptr ) {
299318 return _con_hdl->check () == NetworkConnectionState::CONNECTED;
300319 }
320+ #endif
321+ return true ; // If no connection handler is used, assume we are connected
301322}
302323
303324unsigned long TimeServiceClass::getRemoteTime ()
@@ -308,25 +329,34 @@ unsigned long TimeServiceClass::getRemoteTime()
308329 * This is the most reliable time source and it will
309330 * ensure a correct behaviour of the library.
310331 */
311- if (_con_hdl->getInterface () != NetworkAdapter::CELL) {
312- unsigned long const ntp_time = NTPUtils::getTime (_con_hdl->getUDP ());
332+ bool use_ntp = true ;
333+ #if CONNECTION_HANDLER_ENABLED
334+ if ((_con_hdl != nullptr ) && (_con_hdl->getInterface () != NetworkAdapter::CELL)) {
335+ use_ntp = false ;
336+ }
337+ #endif
338+ if (use_ntp && (_ntp_client != nullptr )) {
339+ unsigned long const ntp_time = NTPUtils::getTime (*_ntp_client);
313340 if (isTimeValid (ntp_time)) {
314341 return ntp_time;
315342 }
316343 }
317344 DEBUG_WARNING (" TimeServiceClass::%s cannot get time from NTP, fallback on connection handler" , __FUNCTION__);
318345#endif /* HAS_TCP */
319346
347+ #if CONNECTION_HANDLER_ENABLED
320348 /* As fallback if NTP request fails try to obtain the
321349 * network time using the connection handler.
322350 */
323- unsigned long const connection_time = _con_hdl->getTime ();
324- if (isTimeValid (connection_time)) {
325- return connection_time;
351+ if (_con_hdl != nullptr ) {
352+ unsigned long const connection_time = _con_hdl->getTime ();
353+ if (isTimeValid (connection_time)) {
354+ return connection_time;
355+ }
356+ DEBUG_WARNING (" TimeServiceClass::%s cannot get time from connection handler" , __FUNCTION__);
326357 }
327- DEBUG_WARNING ( " TimeServiceClass::%s cannot get time from connection handler " , __FUNCTION__);
358+ # endif
328359 }
329-
330360 /* Return known invalid value because we are not connected */
331361 return EPOCH;
332362}
0 commit comments