Skip to content

Commit 91a4168

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

File tree

1 file changed

+27
-42
lines changed

1 file changed

+27
-42
lines changed

src/utility/time/TimeService.cpp

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -76,33 +76,20 @@ 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+
}
92+
return time;
10693
}
10794

10895
void TimeService::setTimeZoneData(long offset, unsigned long dst_until)
@@ -206,28 +193,26 @@ unsigned long TimeService::getRemoteTime()
206193
#include "../../AIoTC_Config.h"
207194
#ifndef HAS_LORA
208195

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

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

229215
#endif /* ifndef HAS_LORA */
230-
}
231216

232217
/* Return the epoch timestamp at compile time as a last
233218
* line of defense. Otherwise the certificate expiration

0 commit comments

Comments
 (0)