@@ -116,14 +116,17 @@ static time_t const EPOCH = 0;
116
116
**************************************************************************************/
117
117
118
118
TimeServiceClass::TimeServiceClass ()
119
- : _con_hdl(nullptr )
120
- , _is_rtc_configured(false )
119
+ : _is_rtc_configured(false )
121
120
, _is_tz_configured(false )
122
121
, _timezone_offset(24 * 60 * 60 )
123
122
, _timezone_dst_until(0 )
124
123
, _last_sync_tick(0 )
125
124
, _sync_interval_ms(TIMESERVICE_NTP_SYNC_TIMEOUT_ms)
126
125
, _sync_func(nullptr )
126
+ #if CONNECTION_HANDLER_ENABLED
127
+ , _con_hdl(nullptr )
128
+ #endif
129
+ , _ntp_client(nullptr )
127
130
{
128
131
129
132
}
@@ -132,9 +135,26 @@ TimeServiceClass::TimeServiceClass()
132
135
* PUBLIC MEMBER FUNCTIONS
133
136
**************************************************************************************/
134
137
138
+ #if CONNECTION_HANDLER_ENABLED
135
139
void TimeServiceClass::begin (ConnectionHandler * con_hdl)
136
140
{
137
141
_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
+ {
138
158
initRTC ();
139
159
#ifdef HAS_LORA
140
160
setRTC (EPOCH_AT_COMPILE_TIME);
@@ -293,11 +313,12 @@ unsigned long TimeServiceClass::getTimeFromString(const String& input)
293
313
#if defined(HAS_NOTECARD) || defined(HAS_TCP)
294
314
bool TimeServiceClass::connected ()
295
315
{
296
- if (_con_hdl == nullptr ) {
297
- return false ;
298
- } else {
316
+ #if CONNECTION_HANDLER_ENABLED
317
+ if (_con_hdl != nullptr ) {
299
318
return _con_hdl->check () == NetworkConnectionState::CONNECTED;
300
319
}
320
+ #endif
321
+ return true ; // If no connection handler is used, assume we are connected
301
322
}
302
323
303
324
unsigned long TimeServiceClass::getRemoteTime ()
@@ -308,25 +329,34 @@ unsigned long TimeServiceClass::getRemoteTime()
308
329
* This is the most reliable time source and it will
309
330
* ensure a correct behaviour of the library.
310
331
*/
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);
313
340
if (isTimeValid (ntp_time)) {
314
341
return ntp_time;
315
342
}
316
343
}
317
344
DEBUG_WARNING (" TimeServiceClass::%s cannot get time from NTP, fallback on connection handler" , __FUNCTION__);
318
345
#endif /* HAS_TCP */
319
346
347
+ #if CONNECTION_HANDLER_ENABLED
320
348
/* As fallback if NTP request fails try to obtain the
321
349
* network time using the connection handler.
322
350
*/
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__);
326
357
}
327
- DEBUG_WARNING ( " TimeServiceClass::%s cannot get time from connection handler " , __FUNCTION__);
358
+ # endif
328
359
}
329
-
330
360
/* Return known invalid value because we are not connected */
331
361
return EPOCH;
332
362
}
0 commit comments