2018-02-26 00:11:31 +00:00
|
|
|
#include <NTPSettingsService.h>
|
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
NTPSettingsService::NTPSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) :
|
|
|
|
AdminSettingsService(server, fs, securityManager, NTP_SETTINGS_SERVICE_PATH, NTP_SETTINGS_FILE) {
|
2019-12-24 11:19:19 +00:00
|
|
|
#ifdef ESP32
|
2019-12-03 23:16:06 +00:00
|
|
|
WiFi.onEvent(
|
|
|
|
std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2),
|
|
|
|
WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
|
|
|
|
WiFi.onEvent(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2),
|
|
|
|
WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
|
2019-12-24 11:19:19 +00:00
|
|
|
#elif defined(ESP8266)
|
|
|
|
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(
|
|
|
|
std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
|
|
|
|
_onStationModeGotIPHandler =
|
|
|
|
WiFi.onStationModeGotIP(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1));
|
2018-11-11 17:47:44 +00:00
|
|
|
#endif
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
NTPSettingsService::~NTPSettingsService() {
|
|
|
|
}
|
2018-02-26 00:11:31 +00:00
|
|
|
|
|
|
|
void NTPSettingsService::loop() {
|
|
|
|
// detect when we need to re-configure NTP and do it in the main loop
|
|
|
|
if (_reconfigureNTP) {
|
|
|
|
_reconfigureNTP = false;
|
|
|
|
configureNTP();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NTPSettingsService::readFromJsonObject(JsonObject& root) {
|
2020-02-01 08:44:26 +00:00
|
|
|
_settings.enabled = root["enabled"] | NTP_SETTINGS_SERVICE_DEFAULT_ENABLED;
|
|
|
|
_settings.server = root["server"] | NTP_SETTINGS_SERVICE_DEFAULT_SERVER;
|
|
|
|
_settings.tzLabel = root["tz_label"] | NTP_SETTINGS_SERVICE_DEFAULT_TIME_ZONE_LABEL;
|
|
|
|
_settings.tzFormat = root["tz_format"] | NTP_SETTINGS_SERVICE_DEFAULT_TIME_ZONE_FORMAT;
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NTPSettingsService::writeToJsonObject(JsonObject& root) {
|
2020-02-01 08:44:26 +00:00
|
|
|
root["enabled"] = _settings.enabled;
|
|
|
|
root["server"] = _settings.server;
|
|
|
|
root["tz_label"] = _settings.tzLabel;
|
|
|
|
root["tz_format"] = _settings.tzFormat;
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NTPSettingsService::onConfigUpdated() {
|
|
|
|
_reconfigureNTP = true;
|
|
|
|
}
|
|
|
|
|
2019-12-24 11:19:19 +00:00
|
|
|
#ifdef ESP32
|
|
|
|
void NTPSettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
|
2020-02-01 08:44:26 +00:00
|
|
|
Serial.println("Got IP address, starting NTP Synchronization");
|
2018-02-26 00:11:31 +00:00
|
|
|
_reconfigureNTP = true;
|
|
|
|
}
|
|
|
|
|
2019-12-24 11:19:19 +00:00
|
|
|
void NTPSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
|
2020-02-01 08:44:26 +00:00
|
|
|
Serial.println("WiFi connection dropped, stopping NTP.");
|
2018-11-11 17:47:44 +00:00
|
|
|
_reconfigureNTP = false;
|
2020-01-20 11:14:46 +00:00
|
|
|
sntp_stop();
|
2018-11-11 17:47:44 +00:00
|
|
|
}
|
2019-12-24 11:19:19 +00:00
|
|
|
#elif defined(ESP8266)
|
|
|
|
void NTPSettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
|
2020-02-01 08:44:26 +00:00
|
|
|
Serial.println("Got IP address, starting NTP Synchronization");
|
2018-11-11 17:47:44 +00:00
|
|
|
_reconfigureNTP = true;
|
|
|
|
}
|
2018-02-26 00:11:31 +00:00
|
|
|
|
2019-12-24 11:19:19 +00:00
|
|
|
void NTPSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
|
2020-02-01 08:44:26 +00:00
|
|
|
Serial.println("WiFi connection dropped, stopping NTP.");
|
2018-02-26 00:11:31 +00:00
|
|
|
_reconfigureNTP = false;
|
2020-01-20 11:14:46 +00:00
|
|
|
sntp_stop();
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
2018-11-11 17:47:44 +00:00
|
|
|
#endif
|
2018-02-26 00:11:31 +00:00
|
|
|
|
|
|
|
void NTPSettingsService::configureNTP() {
|
|
|
|
Serial.println("Configuring NTP...");
|
2020-02-01 08:44:26 +00:00
|
|
|
if (_settings.enabled) {
|
2020-01-20 11:14:46 +00:00
|
|
|
#ifdef ESP32
|
2020-02-01 08:44:26 +00:00
|
|
|
configTzTime(_settings.tzFormat.c_str(), _settings.server.c_str());
|
2020-01-20 11:14:46 +00:00
|
|
|
#elif defined(ESP8266)
|
2020-02-01 08:44:26 +00:00
|
|
|
configTime(_settings.tzFormat.c_str(), _settings.server.c_str());
|
2020-01-20 11:14:46 +00:00
|
|
|
#endif
|
2019-12-03 23:16:06 +00:00
|
|
|
} else {
|
2020-02-01 08:44:26 +00:00
|
|
|
sntp_stop();
|
2019-12-03 23:16:06 +00:00
|
|
|
}
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|