81 lines
2.6 KiB
C++
81 lines
2.6 KiB
C++
#ifndef TIME_MANAGER_H
|
|
#define TIME_MANAGER_H
|
|
|
|
#include <Arduino.h>
|
|
#include <time.h>
|
|
#include "udp_logger.h"
|
|
|
|
typedef enum
|
|
{
|
|
TIME_UPDATE_FAILED = 0,
|
|
TIME_UPDATE_OK = 1,
|
|
TIME_UPDATE_PENDING = 2,
|
|
} TimeUpdateState;
|
|
|
|
typedef enum
|
|
{
|
|
TM_INIT = 0,
|
|
TM_INITIAL_SYNC = 1,
|
|
TM_NORMAL = 2,
|
|
TM_SYNC_OVERDUE = 3,
|
|
TM_SYNC_TIMEOUT = 4,
|
|
TM_SETUP_FAILED = 5,
|
|
} TimeManagerState;
|
|
|
|
class TimeManager
|
|
{
|
|
#define NTP_MAX_UPDATE_TIME_US (5 * 1000 * 1000) // 5000ms max update time
|
|
|
|
public:
|
|
// constructors
|
|
TimeManager();
|
|
TimeManager(const char *tz,
|
|
const char *ntp_server,
|
|
bool (*is_wifi_connected)(void),
|
|
uint32 ntp_max_offline_time_s,
|
|
UDPLogger *logger);
|
|
|
|
// init
|
|
void init();
|
|
|
|
// callback
|
|
void time_set_cb(void); // callback which is called when NTP time was set
|
|
|
|
// ntp methods
|
|
bool ntp_sync_successful(void) const; // was there a NTP sync once?
|
|
bool ntp_sync_overdue(void); // function to check if NTP sync is overdue
|
|
bool ntp_sync_timeout(void); // function to check if maximum time since last NTP sync has been reached
|
|
TimeUpdateState get_time(); // main time update method, called in loop
|
|
|
|
// getter for time values
|
|
bool isdst(void) const; // true if summertime (daylight saving time)
|
|
int day(void) const;
|
|
int hour(void) const;
|
|
int minute(void) const;
|
|
int month(void) const;
|
|
int year(void) const;
|
|
struct tm time_info(void) const;
|
|
|
|
// 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:
|
|
void _set_up_ntp(void); // set up NTP server
|
|
bool (*_is_wifi_connected)(void); // function to check if wifi is connected
|
|
|
|
const char *_ntp_server = "pool.ntp.org"; // ntp server address
|
|
const char *_tz; // timezone
|
|
struct tm _time_info = {0, 0, 0, 0, 0, 0, 0, 0, 0}; // structure tm holds time information
|
|
time_t _now = 0; // local time value
|
|
time_t _ntp_sync_timestamp_s = 0; // timestamp of last successful ntp sync
|
|
TimeManagerState _tm_state = TM_INIT; // Main state
|
|
UDPLogger *_logger; // logger instance
|
|
uint32 _ntp_max_offline_time_s; // maximum time in seconds which is considered ok since last NTP update
|
|
};
|
|
|
|
#endif /* TIME_MANAGER_H */
|