Skip to content

Commit b445fd7

Browse files
committed
Move connection check out of getRemoteTime and refactor getTime
1 parent a0b3e54 commit b445fd7

File tree

1 file changed

+28
-44
lines changed

1 file changed

+28
-44
lines changed

src/utility/time/TimeService.cpp

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -76,33 +76,21 @@ void TimeService::begin(ConnectionHandler * con_hdl)
7676

7777
unsigned long TimeService::getTime()
7878
{
79-
#ifdef ARDUINO_ARCH_SAMD
80-
if(!_is_rtc_configured)
81-
{
82-
unsigned long utc = getRemoteTime();
83-
if(EPOCH_AT_COMPILE_TIME != utc)
84-
{
85-
rtc.setEpoch(utc);
86-
_is_rtc_configured = true;
87-
}
88-
return utc;
79+
unsigned long time = EPOCH_AT_COMPILE_TIME;
80+
81+
if(_is_rtc_configured) {
82+
return get_rtc_time();
8983
}
90-
return rtc.getEpoch();
91-
#elif ARDUINO_ARCH_MBED
92-
if(!_is_rtc_configured)
93-
{
94-
unsigned long utc = getRemoteTime();
95-
if(EPOCH_AT_COMPILE_TIME != utc)
96-
{
97-
set_time(utc);
98-
_is_rtc_configured = true;
99-
}
100-
return utc;
84+
85+
if(isConnected()) {
86+
time = getRemoteTime();
10187
}
102-
return time(NULL);
103-
#else
104-
return getRemoteTime();
105-
#endif
88+
89+
if(time != EPOCH_AT_COMPILE_TIME) {
90+
set_rtc_time(time);
91+
_is_rtc_configured = true;
92+
}
93+
return time;
10694
}
10795

10896
void TimeService::setTimeZoneData(long offset, unsigned long dst_until)
@@ -206,28 +194,26 @@ unsigned long TimeService::getRemoteTime()
206194
#include "../../AIoTC_Config.h"
207195
#ifndef HAS_LORA
208196

209-
if(isConnected()) {
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-
}
197+
/* At first try to see if a valid time can be obtained
198+
* using the network time available via the connection
199+
* handler.
200+
*/
201+
unsigned long const connection_time = _con_hdl->getTime();
202+
if(isTimeValid(connection_time)) {
203+
return connection_time;
204+
}
218205

219206
#ifndef __AVR__
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-
}
207+
/* If no valid network time is available try to obtain the
208+
* time via NTP next.
209+
*/
210+
unsigned long const ntp_time = NTPUtils::getTime(_con_hdl->getUDP());
211+
if(isTimeValid(ntp_time)) {
212+
return ntp_time;
213+
}
227214
#endif
228215

229216
#endif /* ifndef HAS_LORA */
230-
}
231217

232218
/* Return the epoch timestamp at compile time as a last
233219
* line of defense. Otherwise the certificate expiration
@@ -289,10 +275,8 @@ unsigned long get_rtc_time() {
289275
void set_rtc_time(unsigned long time) {
290276
#ifdef ARDUINO_ARCH_SAMD
291277
rtc.setEpoch(time);
292-
_is_rtc_configured = true;
293278
#elif ARDUINO_ARCH_MBED
294279
set_time(time);
295-
_is_rtc_configured = true;
296280
#else
297281

298282
#endif

0 commit comments

Comments
 (0)