Minor refactoring.
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
#include "wordclock_constants.h"
|
||||
#include "time.h"
|
||||
|
||||
extern UDPLogger logger;
|
||||
extern UDPLogger logger; // logging instance
|
||||
extern ESP8266Timer ITimer; // ESP8266 Timer
|
||||
extern void IRAM_ATTR TimerHandler(); // ISR function
|
||||
|
||||
@@ -25,22 +25,22 @@ TimeManager::TimeManager(const char *tz,
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
struct tm TimeManager::time_info(void)
|
||||
{
|
||||
localtime_r(&_time_now_local, &_time_info); // convert time
|
||||
return _time_info;
|
||||
}
|
||||
|
||||
TimeManagerState TimeManager::tm_state(void) const
|
||||
{
|
||||
return _tm_state;
|
||||
}
|
||||
|
||||
bool TimeManager::ntp_sync_successful(void) const
|
||||
{
|
||||
return (_time_now_ntp > 0);
|
||||
}
|
||||
|
||||
bool TimeManager::ntp_update_failed_prolonged(void)
|
||||
{
|
||||
bool retval = false;
|
||||
if (_time_now_local >= (_time_now_ntp + (time_t)_ntp_max_offline_time_s))
|
||||
{
|
||||
_tm_state = TM_PROLONGED_SYNC_FAIL;
|
||||
retval = true;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief NTP time update, should be called in loop().
|
||||
*
|
||||
@@ -118,27 +118,10 @@ NtpUpdateState TimeManager::ntp_time_update()
|
||||
return retval;
|
||||
}
|
||||
|
||||
bool TimeManager::ntp_update_failed_prolonged(void)
|
||||
{
|
||||
bool retval = false;
|
||||
if (_time_now_local >= (_time_now_ntp + (time_t)_ntp_max_offline_time_s))
|
||||
{
|
||||
_tm_state = TM_PROLONGED_SYNC_FAIL;
|
||||
retval = true;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
int TimeManager::tm_min(void)
|
||||
bool TimeManager::tm_isdst(void)
|
||||
{
|
||||
localtime_r(&_time_now_local, &_time_info); // convert time
|
||||
return _time_info.tm_min;
|
||||
}
|
||||
|
||||
int TimeManager::tm_hour(void)
|
||||
{
|
||||
localtime_r(&_time_now_local, &_time_info); // convert time
|
||||
return _time_info.tm_hour;
|
||||
return _time_info.tm_isdst > 0;
|
||||
}
|
||||
|
||||
int TimeManager::tm_day(void)
|
||||
@@ -147,10 +130,16 @@ int TimeManager::tm_day(void)
|
||||
return _time_info.tm_mday + 1; // add 1 to get actual day
|
||||
}
|
||||
|
||||
int TimeManager::tm_year(void)
|
||||
int TimeManager::tm_hour(void)
|
||||
{
|
||||
localtime_r(&_time_now_local, &_time_info); // convert time
|
||||
return _time_info.tm_year + NTP_START_YEAR; // add start year to get actual year
|
||||
return _time_info.tm_hour;
|
||||
}
|
||||
|
||||
int TimeManager::tm_min(void)
|
||||
{
|
||||
localtime_r(&_time_now_local, &_time_info); // convert time
|
||||
return _time_info.tm_min;
|
||||
}
|
||||
|
||||
int TimeManager::tm_mon(void)
|
||||
@@ -159,49 +148,21 @@ int TimeManager::tm_mon(void)
|
||||
return _time_info.tm_mon + 1; // add 1 to get actual month
|
||||
}
|
||||
|
||||
bool TimeManager::tm_isdst(void)
|
||||
int TimeManager::tm_year(void)
|
||||
{
|
||||
localtime_r(&_time_now_local, &_time_info); // convert time
|
||||
return _time_info.tm_isdst > 0;
|
||||
return _time_info.tm_year + NTP_START_YEAR; // add start year to get actual year
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sets up the timer ISR
|
||||
*
|
||||
*/
|
||||
void TimeManager::_set_up_timer_isr(void)
|
||||
struct tm TimeManager::time_info(void)
|
||||
{
|
||||
// set up timer interrupt after NTP update is done
|
||||
if (ITimer.attachInterruptInterval(PERIOD_CLOCK_UPDATE_US, TimerHandler))
|
||||
{
|
||||
_tm_state = TM_NORMAL;
|
||||
logger.log_string(String("Timer ISR was attached successfully!"));
|
||||
}
|
||||
else
|
||||
{
|
||||
_tm_state = TM_SETUP_FAILED;
|
||||
logger.log_string("WARNING: Timer interrupt was not attached!");
|
||||
}
|
||||
localtime_r(&_time_now_local, &_time_info); // convert time
|
||||
return _time_info;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sets up the NTP server config
|
||||
*
|
||||
*/
|
||||
void TimeManager::_set_up_ntp(void)
|
||||
TimeManagerState TimeManager::tm_state(void) const
|
||||
{
|
||||
if ((_tz != nullptr) && (_ntp_server != nullptr))
|
||||
{
|
||||
// set up NTP server and timezone at init
|
||||
configTime(_tz, _ntp_server);
|
||||
_tm_state = TM_INITIAL_SYNC;
|
||||
logger.log_string(String("NTP server connection was set up successfully!"));
|
||||
}
|
||||
else
|
||||
{
|
||||
_tm_state = TM_SETUP_FAILED;
|
||||
logger.log_string(String("ERROR: Timezone and/or NTP-Server were not given correctly!"));
|
||||
}
|
||||
return _tm_state;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -225,3 +186,42 @@ void TimeManager::log_time(struct tm time_info) const
|
||||
strftime(strftime_buf, sizeof(strftime_buf), "%c", &time_info);
|
||||
logger.log_string(String(strftime_buf));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sets up the NTP server config
|
||||
*
|
||||
*/
|
||||
void TimeManager::_set_up_ntp(void)
|
||||
{
|
||||
if ((_tz != nullptr) && (_ntp_server != nullptr))
|
||||
{
|
||||
// set up NTP server and timezone at init
|
||||
configTime(_tz, _ntp_server);
|
||||
_tm_state = TM_INITIAL_SYNC;
|
||||
logger.log_string(String("NTP configuration was set up successfully!"));
|
||||
}
|
||||
else
|
||||
{
|
||||
_tm_state = TM_SETUP_FAILED;
|
||||
logger.log_string(String("ERROR: Timezone and/or NTP-Server were not given correctly!"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sets up the timer ISR
|
||||
*
|
||||
*/
|
||||
void TimeManager::_set_up_timer_isr(void)
|
||||
{
|
||||
// set up timer interrupt after NTP update is done
|
||||
if (ITimer.attachInterruptInterval(PERIOD_CLOCK_UPDATE_US, TimerHandler))
|
||||
{
|
||||
_tm_state = TM_NORMAL;
|
||||
logger.log_string(String("Timer ISR was attached successfully!"));
|
||||
}
|
||||
else
|
||||
{
|
||||
_tm_state = TM_SETUP_FAILED;
|
||||
logger.log_string("WARNING: Timer interrupt was not attached!");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user