Files
IoTManager/include/Clock.h

98 lines
2.2 KiB
C
Raw Normal View History

2020-06-22 03:11:02 +03:00
#pragma once
2020-06-25 09:21:42 +03:00
#include "Utils/TimeUtils.h"
#include "Utils/PrintMessage.h"
2020-06-22 03:11:02 +03:00
2020-06-25 20:13:20 +03:00
#include "TZ.h"
2020-06-22 03:11:02 +03:00
2020-06-25 09:21:42 +03:00
class Clock {
const char* MODULE = "Clock";
public:
Clock() : _timezone{0}, _ntp{}, _hasSynced{false}, _configured{false} {
}
bool hasSync() {
if (!_hasSynced) {
startSync();
}
return _hasSynced;
}
2020-06-25 20:13:20 +03:00
void setNtpPool(String ntp) {
2020-06-25 09:21:42 +03:00
_ntp = ntp;
}
void setTimezone(int timezone) {
_timezone = timezone;
}
void startSync() {
if (!_configured) {
pm.info("sync to: " + _ntp + " time zone: " + String(_timezone));
setupSntp();
_configured = true;
}
_hasSynced = hasTimeSynced();
if (_hasSynced) {
pm.info("synced " + getDateDigitalFormated() + " " + getTime());
} else {
pm.error("failed to obtain");
}
}
void setupSntp() {
int tzs = getBiasInSeconds();
int tzh = tzs / 3600;
tzs -= tzh * 3600;
int tzm = tzs / 60;
tzs -= tzm * 60;
char tzstr[64];
snprintf(tzstr, sizeof tzstr, "ESPUSER<%+d:%02d:%02d>", tzh, tzm, tzs);
pm.info(String(tzstr));
configTime(tzstr, _ntp.c_str());
}
// #ifdef ESP32
// uint8_t i = 0;
// struct tm timeinfo;
// while (!getLocalTime(&timeinfo) && i <= 4) {
// Serial.print(".");
// i++;
// delay(1000);
// }
// #endif
2020-06-25 20:13:20 +03:00
2020-06-25 09:21:42 +03:00
bool hasTimeSynced() {
2020-06-25 20:13:20 +03:00
int uptime = millis() / 1000;
return getSystemTime() > uptime;
}
time_t getSystemTime() {
timeval tv{0, 0};
timezone tz = getTimeZone(getBiasInMinutes());
time_t epoch = 0;
if (gettimeofday(&tv, &tz) != -1) {
epoch = tv.tv_sec;
}
return epoch + getBiasInSeconds();
2020-06-25 09:21:42 +03:00
}
int getBiasInSeconds() {
return getBiasInMinutes() * 60;
}
int getBiasInMinutes() {
return _timezone * 60;
}
const timezone getTimeZone(int minutes) {
return timezone{minutes, 0};
}
private:
int _timezone;
String _ntp;
bool _hasSynced;
bool _configured;
};