Skip to content

Commit c9cfdbe

Browse files
committed
Add possibility to configure a custom time sync function
1 parent a51b2d5 commit c9cfdbe

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/utility/time/TimeService.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,9 @@ TimeService::TimeService()
105105
, _is_tz_configured(false)
106106
, _timezone_offset(0)
107107
, _timezone_dst_until(0)
108-
#ifdef HAS_TCP
109-
, _last_ntp_sync_tick(0)
110-
, _ntp_sync_interval_ms(TIMESERVICE_NTP_SYNC_TIMEOUT_ms)
111-
#endif
108+
, _last_sync_tick(0)
109+
, _sync_interval_ms(TIMESERVICE_NTP_SYNC_TIMEOUT_ms)
110+
, _sync_func(nullptr)
112111
{
113112

114113
}
@@ -130,7 +129,7 @@ unsigned long TimeService::getTime()
130129
{
131130
/* Check if it's time to sync */
132131
unsigned long const current_tick = millis();
133-
bool const is_ntp_sync_timeout = (current_tick - _last_ntp_sync_tick) > _ntp_sync_interval_ms;
132+
bool const is_ntp_sync_timeout = (current_tick - _last_sync_tick) > _sync_interval_ms;
134133
if(!_is_rtc_configured || is_ntp_sync_timeout) {
135134
sync();
136135
}
@@ -145,7 +144,6 @@ void TimeService::setTime(unsigned long time)
145144
setRTC(time);
146145
}
147146

148-
#ifdef HAS_TCP
149147
bool TimeService::sync()
150148
{
151149
_is_rtc_configured = false;
@@ -166,17 +164,23 @@ bool TimeService::sync()
166164
if(isTimeValid(utc)) {
167165
DEBUG_DEBUG("TimeServiceClass::%s Drift: %d RTC value: %u", __FUNCTION__, getRTC() - utc, utc);
168166
setRTC(utc);
169-
_last_ntp_sync_tick = millis();
167+
_last_sync_tick = millis();
170168
_is_rtc_configured = true;
171169
}
172170
return _is_rtc_configured;
173171
}
174172

175173
void TimeService::setSyncInterval(unsigned long seconds)
176174
{
177-
_ntp_sync_interval_ms = seconds * 1000;
175+
_sync_interval_ms = seconds * 1000;
176+
}
177+
178+
void TimeService::setSyncFunction(syncTimeFunctionPtr sync_func)
179+
{
180+
if(sync_func) {
181+
_sync_func = sync_func;
182+
}
178183
}
179-
#endif
180184

181185
void TimeService::setTimeZoneData(long offset, unsigned long dst_until)
182186
{

src/utility/time/TimeService.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
#include <AIoTC_Config.h>
2626
#include <Arduino_ConnectionHandler.h>
2727

28+
/******************************************************************************
29+
* TYPEDEF
30+
******************************************************************************/
31+
32+
typedef unsigned long(*syncTimeFunctionPtr)(void);
33+
2834
/**************************************************************************************
2935
* CLASS DECLARATION
3036
**************************************************************************************/
@@ -41,10 +47,9 @@ class TimeService
4147
void setTime(unsigned long time);
4248
unsigned long getLocalTime();
4349
void setTimeZoneData(long offset, unsigned long valid_until);
44-
#ifdef HAS_TCP
4550
bool sync();
46-
#endif
4751
void setSyncInterval(unsigned long seconds);
52+
void setSyncFunction(syncTimeFunctionPtr sync_func);
4853

4954
/* Helper function to convert an input String into a UNIX timestamp.
5055
* The input String format must be as follow "2021 Nov 01 17:00:00"
@@ -58,10 +63,9 @@ class TimeService
5863
bool _is_tz_configured;
5964
long _timezone_offset;
6065
unsigned long _timezone_dst_until;
61-
#ifdef HAS_TCP
62-
unsigned long _last_ntp_sync_tick;
63-
unsigned long _ntp_sync_interval_ms;
64-
#endif
66+
unsigned long _last_sync_tick;
67+
unsigned long _sync_interval_ms;
68+
syncTimeFunctionPtr _sync_func;
6569

6670
#ifdef HAS_TCP
6771
unsigned long getRemoteTime();

0 commit comments

Comments
 (0)