Skip to content

Commit 2ea6f62

Browse files
committed
Make TimeService work without ConnectionHandler
- Removing fallback for getTime it only needs an UDP reference to run NTP requests
1 parent 49d58ae commit 2ea6f62

File tree

2 files changed

+22
-32
lines changed

2 files changed

+22
-32
lines changed

src/utility/time/TimeService.cpp

+19-29
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ static time_t const EPOCH = 0;
109109
**************************************************************************************/
110110

111111
TimeServiceClass::TimeServiceClass()
112-
: _con_hdl(nullptr)
112+
: _udp(nullptr)
113113
, _is_rtc_configured(false)
114114
, _is_tz_configured(false)
115115
, _timezone_offset(24 * 60 * 60)
@@ -125,13 +125,14 @@ TimeServiceClass::TimeServiceClass()
125125
* PUBLIC MEMBER FUNCTIONS
126126
**************************************************************************************/
127127

128-
void TimeServiceClass::begin(ConnectionHandler * con_hdl)
128+
void TimeServiceClass::begin(UDP & udp)
129129
{
130-
_con_hdl = con_hdl;
131130
initRTC();
132131
#ifdef HAS_LORA
133132
setRTC(EPOCH_AT_COMPILE_TIME);
134133
#endif
134+
135+
_udp = &udp;
135136
}
136137

137138
unsigned long TimeServiceClass::getTime()
@@ -179,6 +180,14 @@ bool TimeServiceClass::sync()
179180
return _is_rtc_configured;
180181
}
181182

183+
bool TimeServiceClass::isTimeValid()
184+
{
185+
if (_is_rtc_configured) {
186+
return isTimeValid(getRTC());
187+
}
188+
return false;
189+
}
190+
182191
void TimeServiceClass::setSyncInterval(unsigned long seconds)
183192
{
184193
_sync_interval_ms = seconds * 1000;
@@ -276,34 +285,15 @@ unsigned long TimeServiceClass::getTimeFromString(const String& input)
276285
**************************************************************************************/
277286

278287
#ifdef HAS_TCP
279-
bool TimeServiceClass::connected()
280-
{
281-
if(_con_hdl == nullptr) {
282-
return false;
283-
} else {
284-
return _con_hdl->getStatus() == NetworkConnectionState::CONNECTED;
285-
}
286-
}
287-
288288
unsigned long TimeServiceClass::getRemoteTime()
289289
{
290-
if(connected()) {
291-
/* At first try to obtain a valid time via NTP.
292-
* This is the most reliable time source and it will
293-
* ensure a correct behaviour of the library.
294-
*/
295-
unsigned long const ntp_time = NTPUtils::getTime(_con_hdl->getUDP());
296-
if(isTimeValid(ntp_time)) {
297-
return ntp_time;
298-
}
299-
300-
/* As fallback if NTP request fails try to obtain the
301-
* network time using the connection handler.
302-
*/
303-
unsigned long const connection_time = _con_hdl->getTime();
304-
if(isTimeValid(connection_time)) {
305-
return connection_time;
306-
}
290+
/* At first try to obtain a valid time via NTP.
291+
* This is the most reliable time source and it will
292+
* ensure a correct behaviour of the library.
293+
*/
294+
unsigned long const ntp_time = NTPUtils::getTime(_udp);
295+
if(isTimeValid(ntp_time)) {
296+
return ntp_time;
307297
}
308298

309299
/* Return the epoch timestamp at compile time as a last

src/utility/time/TimeService.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ class TimeServiceClass
4242

4343
TimeServiceClass();
4444

45-
void begin (ConnectionHandler * con_hdl);
45+
void begin (UDP & udp);
4646
unsigned long getTime();
4747
void setTime(unsigned long time);
4848
unsigned long getLocalTime();
4949
void setTimeZoneData(long offset, unsigned long valid_until);
5050
bool sync();
51+
bool isTimeValid();
5152
void setSyncInterval(unsigned long seconds);
5253
void setSyncFunction(syncTimeFunctionPtr sync_func);
5354

@@ -58,7 +59,7 @@ class TimeServiceClass
5859

5960
private:
6061

61-
ConnectionHandler * _con_hdl;
62+
UDP * _udp;
6263
bool _is_rtc_configured;
6364
bool _is_tz_configured;
6465
long _timezone_offset;
@@ -69,7 +70,6 @@ class TimeServiceClass
6970

7071
#ifdef HAS_TCP
7172
unsigned long getRemoteTime();
72-
bool connected();
7373
#endif
7474
void initRTC();
7575
void setRTC(unsigned long time);

0 commit comments

Comments
 (0)