1 #ifndef ACE_TIME_SYSTEM_TIME_SYNC_LOOP_H 2 #define ACE_TIME_SYSTEM_TIME_SYNC_LOOP_H 5 #include "SystemTimeKeeper.h" 28 uint16_t syncPeriodSeconds = 3600,
29 uint16_t initialSyncPeriodSeconds = 5,
30 uint16_t requestTimeoutMillis = 1000):
31 mSystemTimeKeeper(systemTimeKeeper),
32 mSyncPeriodSeconds(syncPeriodSeconds),
33 mInitialSyncPeriodSeconds(initialSyncPeriodSeconds),
34 mRequestTimeoutMillis(requestTimeoutMillis),
35 mCurrentSyncPeriodSeconds(initialSyncPeriodSeconds) {}
42 if (mSystemTimeKeeper.mSyncTimeProvider ==
nullptr)
return;
44 unsigned long nowMillis = millis();
45 unsigned long timeSinceLastSync = nowMillis - mLastSyncMillis;
47 if (timeSinceLastSync >= mCurrentSyncPeriodSeconds * 1000UL
48 || mSystemTimeKeeper.
getNow() == 0) {
49 acetime_t nowSeconds = mSystemTimeKeeper.mSyncTimeProvider->
getNow();
51 if (nowSeconds == 0) {
53 if (mCurrentSyncPeriodSeconds >= mSyncPeriodSeconds / 2) {
54 mCurrentSyncPeriodSeconds = mSyncPeriodSeconds;
56 mCurrentSyncPeriodSeconds *= 2;
59 mSystemTimeKeeper.
sync(nowSeconds);
60 mCurrentSyncPeriodSeconds = mSyncPeriodSeconds;
63 mLastSyncMillis = nowMillis;
72 unsigned long elapsedMillis = millis() - mLastSyncMillis;
73 return elapsedMillis / 1000;
78 uint16_t
const mSyncPeriodSeconds;
79 uint16_t
const mInitialSyncPeriodSeconds;
80 uint16_t
const mRequestTimeoutMillis;
82 unsigned long mLastSyncMillis = 0;
83 uint16_t mCurrentSyncPeriodSeconds;
SystemTimeSyncLoop(SystemTimeKeeper &systemTimeKeeper, uint16_t syncPeriodSeconds=3600, uint16_t initialSyncPeriodSeconds=5, uint16_t requestTimeoutMillis=1000)
Constructor.
void sync(acetime_t epochSeconds)
Similar to setNow() except that backupNow() is called only if the backupTimeKeeper is different from ...
void loop()
If AceRoutine coroutine infrastructure is not used, then call this from the global loop() method...
uint16_t getSecondsSinceLastSync() const
Return the number of seconds since last sync.
virtual acetime_t getNow() const =0
Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z).
A TimeKeeper that uses the Arduino millis() function to advance the time returned to the user...
A class that periodically that syncs the SystemTimeKeeper with its syncTimeProvider.
acetime_t getNow() const override
Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z).