Minor refactoring.
This commit is contained in:
@@ -30,22 +30,7 @@ class TimeManager
|
|||||||
#define NTP_MAX_UPDATE_TIME_US (500 * 1000) // 500ms max update time
|
#define NTP_MAX_UPDATE_TIME_US (500 * 1000) // 500ms max update time
|
||||||
|
|
||||||
public:
|
public:
|
||||||
bool ntp_sync_successful(void) const; // was there a NTP sync once?
|
// constructor
|
||||||
bool ntp_update_failed_prolonged(void); // indicates if maximum time since last NTP update was too long
|
|
||||||
NtpUpdateState ntp_time_update();
|
|
||||||
struct tm time_info(void);
|
|
||||||
TimeManagerState tm_state(void) const;
|
|
||||||
void increment_time_now_local(void);
|
|
||||||
void log_time() const; // log _time_info
|
|
||||||
void log_time(struct tm time_info) const; // log argument time_info
|
|
||||||
|
|
||||||
int tm_min(void);
|
|
||||||
int tm_hour(void);
|
|
||||||
int tm_day(void);
|
|
||||||
int tm_mon(void);
|
|
||||||
int tm_year(void);
|
|
||||||
bool tm_isdst(void); // true if summertime
|
|
||||||
|
|
||||||
TimeManager(const char *tz,
|
TimeManager(const char *tz,
|
||||||
const char *ntp_server,
|
const char *ntp_server,
|
||||||
uint32 ntp_update_period_s,
|
uint32 ntp_update_period_s,
|
||||||
@@ -53,9 +38,35 @@ public:
|
|||||||
uint32 ntp_max_offline_time_s,
|
uint32 ntp_max_offline_time_s,
|
||||||
UDPLogger *logger);
|
UDPLogger *logger);
|
||||||
|
|
||||||
|
// ntp methods
|
||||||
|
bool ntp_sync_successful(void) const; // was there a NTP sync once?
|
||||||
|
bool ntp_update_failed_prolonged(void); // indicates if maximum time since last NTP update was too long
|
||||||
|
NtpUpdateState ntp_time_update(); // main NTP time update method, called in loop
|
||||||
|
|
||||||
|
// ISR method
|
||||||
|
void increment_time_now_local(void); // should be called by timer ISR
|
||||||
|
|
||||||
|
// getter for time values
|
||||||
|
bool tm_isdst(void); // true if summertime
|
||||||
|
int tm_day(void);
|
||||||
|
int tm_hour(void);
|
||||||
|
int tm_min(void);
|
||||||
|
int tm_mon(void);
|
||||||
|
int tm_year(void);
|
||||||
|
struct tm time_info(void);
|
||||||
|
|
||||||
|
// getter
|
||||||
|
TimeManagerState tm_state(void) const; // get current state
|
||||||
|
|
||||||
|
// logging
|
||||||
|
void log_time() const; // log _time_info
|
||||||
|
void log_time(struct tm time_info) const; // log argument time_info
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void _set_up_ntp(void); // set up NTP server
|
// setup methods
|
||||||
void _set_up_timer_isr(void); // set up timer interrupt
|
void _set_up_ntp(void); // set up NTP server
|
||||||
|
void _set_up_timer_isr(void); // set up timer interrupt
|
||||||
|
|
||||||
const char *_tz; // timezone
|
const char *_tz; // timezone
|
||||||
const char *_ntp_server; // ntp server address
|
const char *_ntp_server; // ntp server address
|
||||||
UDPLogger *_logger; // logger instance
|
UDPLogger *_logger; // logger instance
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
#include "wordclock_constants.h"
|
#include "wordclock_constants.h"
|
||||||
#include "time.h"
|
#include "time.h"
|
||||||
|
|
||||||
extern UDPLogger logger;
|
extern UDPLogger logger; // logging instance
|
||||||
extern ESP8266Timer ITimer; // ESP8266 Timer
|
extern ESP8266Timer ITimer; // ESP8266 Timer
|
||||||
extern void IRAM_ATTR TimerHandler(); // ISR function
|
extern void IRAM_ATTR TimerHandler(); // ISR function
|
||||||
|
|
||||||
@@ -25,22 +25,22 @@ TimeManager::TimeManager(const char *tz,
|
|||||||
_logger = logger;
|
_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
|
bool TimeManager::ntp_sync_successful(void) const
|
||||||
{
|
{
|
||||||
return (_time_now_ntp > 0);
|
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().
|
* @brief NTP time update, should be called in loop().
|
||||||
*
|
*
|
||||||
@@ -118,27 +118,10 @@ NtpUpdateState TimeManager::ntp_time_update()
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TimeManager::ntp_update_failed_prolonged(void)
|
bool TimeManager::tm_isdst(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)
|
|
||||||
{
|
{
|
||||||
localtime_r(&_time_now_local, &_time_info); // convert time
|
localtime_r(&_time_now_local, &_time_info); // convert time
|
||||||
return _time_info.tm_min;
|
return _time_info.tm_isdst > 0;
|
||||||
}
|
|
||||||
|
|
||||||
int TimeManager::tm_hour(void)
|
|
||||||
{
|
|
||||||
localtime_r(&_time_now_local, &_time_info); // convert time
|
|
||||||
return _time_info.tm_hour;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int TimeManager::tm_day(void)
|
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
|
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
|
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)
|
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
|
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
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
struct tm TimeManager::time_info(void)
|
||||||
* @brief Sets up the timer ISR
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
void TimeManager::_set_up_timer_isr(void)
|
|
||||||
{
|
{
|
||||||
// set up timer interrupt after NTP update is done
|
localtime_r(&_time_now_local, &_time_info); // convert time
|
||||||
if (ITimer.attachInterruptInterval(PERIOD_CLOCK_UPDATE_US, TimerHandler))
|
return _time_info;
|
||||||
{
|
|
||||||
_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!");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
TimeManagerState TimeManager::tm_state(void) const
|
||||||
* @brief Sets up the NTP server config
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
void TimeManager::_set_up_ntp(void)
|
|
||||||
{
|
{
|
||||||
if ((_tz != nullptr) && (_ntp_server != nullptr))
|
return _tm_state;
|
||||||
{
|
|
||||||
// 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!"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -225,3 +186,42 @@ void TimeManager::log_time(struct tm time_info) const
|
|||||||
strftime(strftime_buf, sizeof(strftime_buf), "%c", &time_info);
|
strftime(strftime_buf, sizeof(strftime_buf), "%c", &time_info);
|
||||||
logger.log_string(String(strftime_buf));
|
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