33 lines
924 B
C++
33 lines
924 B
C++
#ifndef TIMEMANAGER_H
|
|
#define TIMEMANAGER_H
|
|
|
|
#include <WiFiUdp.h>
|
|
|
|
class TimeManager {
|
|
public:
|
|
void begin();
|
|
void Stop();
|
|
bool getTime(unsigned long tick); // Called every second to handle requests and updates
|
|
inline bool hasNTPUpdate() { return bHasNTPTime; };
|
|
inline unsigned long getFirstNTPTime() { return firstNTPTime; }
|
|
bool checkNTPResponse();
|
|
void sendNTPRequest();
|
|
|
|
private:
|
|
WiFiUDP udp;
|
|
const char* ntpServer = "pool.ntp.org";
|
|
const unsigned int localPort = 2390;
|
|
bool udpInitialized = false;
|
|
bool bHasNTPTime = false;
|
|
unsigned long firstNTPTime = 0;
|
|
|
|
|
|
unsigned long lastNTPRequestMillis = 0;
|
|
const unsigned long NTP_REQUEST_INTERVAL = 30 * 60 * 1000; // 30 minutes
|
|
const unsigned long NTP_FAILED_INTERNAL = 1 * 60 * 1000; // 1 minute
|
|
|
|
void setSystemClock(unsigned long epochTime);
|
|
};
|
|
|
|
extern TimeManager timeManager;
|
|
#endif // TIMEMANAGER_H
|