Fix for NTP update time. Minor refactoring.
This commit is contained in:
@@ -72,7 +72,7 @@
|
|||||||
#define BUILD_YEAR (__DATE__ + 7) // Will expand to current year at compile time as string.
|
#define BUILD_YEAR (__DATE__ + 7) // Will expand to current year at compile time as string.
|
||||||
#define NTP_MININUM_RX_YEAR (atoi(BUILD_YEAR) - 1) // Will expand to current year minus one at compile time.
|
#define NTP_MININUM_RX_YEAR (atoi(BUILD_YEAR) - 1) // Will expand to current year minus one at compile time.
|
||||||
#define NTP_START_YEAR (1900) // NTP minimum year is 1900
|
#define NTP_START_YEAR (1900) // NTP minimum year is 1900
|
||||||
#define NTP_UPDATE_PERIOD_S (12 * 3600) // 12h period between updates
|
#define NTP_UPDATE_PERIOD_S (6 * 3600) // 6h period between NTP updates
|
||||||
#define NTP_RETRY_DELAY_US (10 * 1000 * 1000) // 10s retry delay time between failed NTP requests
|
#define NTP_RETRY_DELAY_US (10 * 1000 * 1000) // 10s retry delay time between failed NTP requests
|
||||||
#define NTP_MAX_OFFLINE_TIME_S (7 * 24 * 3600) // Watchdog value, maxmimum offline time before a restart is triggered
|
#define NTP_MAX_OFFLINE_TIME_S (7 * 24 * 3600) // Watchdog value, maxmimum offline time before a restart is triggered
|
||||||
|
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ NtpUpdateState TimeManager::ntp_time_update()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if it is time for a NTP sync.
|
// Check if it is time for a NTP sync.
|
||||||
if ((_tm_state != TM_INITIAL_SYNC) && (_tm_state != TM_RETRY_SYNC) && ((system_get_time() - _ntp_sync_timestamp_us) <= (_ntp_update_period_s * 1000000)))
|
if ((_tm_state != TM_INITIAL_SYNC) && (_tm_state != TM_RETRY_SYNC) && (((system_get_time() - _ntp_sync_timestamp_us) / 1000000) <= _ntp_update_period_s))
|
||||||
{
|
{
|
||||||
return NTP_UPDATE_TOO_EARLY;
|
return NTP_UPDATE_TOO_EARLY;
|
||||||
}
|
}
|
||||||
@@ -175,7 +175,7 @@ void TimeManager::_set_up_timer_isr(void)
|
|||||||
if (ITimer.attachInterruptInterval(PERIOD_CLOCK_UPDATE_US, TimerHandler))
|
if (ITimer.attachInterruptInterval(PERIOD_CLOCK_UPDATE_US, TimerHandler))
|
||||||
{
|
{
|
||||||
_tm_state = TM_NORMAL;
|
_tm_state = TM_NORMAL;
|
||||||
logger.log_string(String("Timer ISR was initialized!"));
|
logger.log_string(String("Timer ISR was attached successfully!"));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -195,7 +195,7 @@ void TimeManager::_set_up_ntp(void)
|
|||||||
// set up NTP server and timezone at init
|
// set up NTP server and timezone at init
|
||||||
configTime(_tz, _ntp_server);
|
configTime(_tz, _ntp_server);
|
||||||
_tm_state = TM_INITIAL_SYNC;
|
_tm_state = TM_INITIAL_SYNC;
|
||||||
logger.log_string(String("NTP server was initialized!"));
|
logger.log_string(String("NTP server connection was set up successfully!"));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -251,9 +251,6 @@ void loop()
|
|||||||
if ((current_time_us - last_heartbeat_us) >= PERIOD_HEARTBEAT_US)
|
if ((current_time_us - last_heartbeat_us) >= PERIOD_HEARTBEAT_US)
|
||||||
{
|
{
|
||||||
send_heartbeat(); // send heartbeat update
|
send_heartbeat(); // send heartbeat update
|
||||||
|
|
||||||
tm_mgr.log_time(); // TODO rm
|
|
||||||
|
|
||||||
last_heartbeat_us = system_get_time();
|
last_heartbeat_us = system_get_time();
|
||||||
delay(10);
|
delay(10);
|
||||||
}
|
}
|
||||||
@@ -283,7 +280,10 @@ void loop()
|
|||||||
|
|
||||||
if ((current_time_us - last_time_update_us) >= PERIOD_TIME_UPDATE_US)
|
if ((current_time_us - last_time_update_us) >= PERIOD_TIME_UPDATE_US)
|
||||||
{
|
{
|
||||||
tm_mgr.ntp_time_update(); // NTP time update
|
if (tm_mgr.ntp_time_update() == NTP_UPDATE_OK) // NTP time update
|
||||||
|
{
|
||||||
|
logger.log_string("NTP sync successful!");
|
||||||
|
}
|
||||||
|
|
||||||
if (tm_mgr.ntp_update_failed_prolonged() == true)
|
if (tm_mgr.ntp_update_failed_prolonged() == true)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user