Skip to content

Commit ada180c

Browse files
committed
Make sync interval configurable
1 parent 9471ee0 commit ada180c

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/utility/time/TimeService.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <time.h>
2525
#include "TimeService.h"
2626
#include "NTPUtils.h"
27+
#include "AIoTC_Const.h"
2728

2829
#ifdef ARDUINO_ARCH_SAMD
2930
#include <RTCZero.h>
@@ -90,8 +91,7 @@ unsigned long esp8266_getRTC();
9091
**************************************************************************************/
9192

9293
/* Default NTP synch is scheduled each 24 hours from startup */
93-
static unsigned long const AIOT_TIMESERVICE_NTP_SYNC_TIMEOUT_ms = 86400000;
94-
94+
static time_t const TIMESERVICE_NTP_SYNC_TIMEOUT_ms = DAYS * 1000;
9595
static time_t const EPOCH_AT_COMPILE_TIME = cvt_time(__DATE__);
9696
static time_t const EPOCH = 0;
9797

@@ -106,6 +106,7 @@ TimeService::TimeService()
106106
, _timezone_offset(0)
107107
, _timezone_dst_until(0)
108108
, _last_ntp_sync_tick(0)
109+
, _ntp_sync_interval_ms(TIMESERVICE_NTP_SYNC_TIMEOUT_ms)
109110
{
110111

111112
}
@@ -124,7 +125,7 @@ unsigned long TimeService::getTime()
124125
{
125126
/* Check if it's time to sync */
126127
unsigned long const current_tick = millis();
127-
bool const is_ntp_sync_timeout = (current_tick - _last_ntp_sync_tick) > AIOT_TIMESERVICE_NTP_SYNC_TIMEOUT_ms;
128+
bool const is_ntp_sync_timeout = (current_tick - _last_ntp_sync_tick) > _ntp_sync_interval_ms;
128129
if(!_is_rtc_configured || is_ntp_sync_timeout) {
129130
sync();
130131
}
@@ -148,6 +149,11 @@ bool TimeService::sync()
148149
return _is_rtc_configured;
149150
}
150151

152+
void TimeService::setSyncInterval(unsigned long seconds)
153+
{
154+
_ntp_sync_interval_ms = seconds * 1000;
155+
}
156+
151157
void TimeService::setTimeZoneData(long offset, unsigned long dst_until)
152158
{
153159
if(_timezone_offset != offset)

src/utility/time/TimeService.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class TimeService
4040
unsigned long getLocalTime();
4141
void setTimeZoneData(long offset, unsigned long valid_until);
4242
bool sync();
43+
void setSyncInterval(unsigned long seconds);
44+
4345
/* Helper function to convert an input String into a UNIX timestamp.
4446
* The input String format must be as follow "2021 Nov 01 17:00:00"
4547
*/
@@ -53,6 +55,7 @@ class TimeService
5355
long _timezone_offset;
5456
unsigned long _timezone_dst_until;
5557
unsigned long _last_ntp_sync_tick;
58+
unsigned long _ntp_sync_interval_ms;
5659

5760
unsigned long getRemoteTime();
5861
bool connected();

0 commit comments

Comments
 (0)