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-01-20 11:14:46 +00:00
|
|
|
_enabled = root["enabled"] | NTP_SETTINGS_SERVICE_DEFAULT_ENABLED;
|
2018-02-26 00:11:31 +00:00
|
|
|
_server = root["server"] | NTP_SETTINGS_SERVICE_DEFAULT_SERVER;
|
2020-01-20 11:14:46 +00:00
|
|
|
_tzLabel = root["tz_label"] | NTP_SETTINGS_SERVICE_DEFAULT_TIME_ZONE_LABEL;
|
|
|
|
_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-01-20 11:14:46 +00:00
|
|
|
root["enabled"] = _enabled;
|
2018-02-26 00:11:31 +00:00
|
|
|
root["server"] = _server;
|
2020-01-20 11:14:46 +00:00
|
|
|
root["tz_label"] = _tzLabel;
|
|
|
|
root["tz_format"] = _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) {
|
2018-02-26 00:11:31 +00:00
|
|
|
Serial.printf("Got IP address, starting NTP Synchronization\n");
|
|
|
|
_reconfigureNTP = true;
|
|
|
|
}
|
|
|
|
|
2019-12-24 11:19:19 +00:00
|
|
|
void NTPSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
|
2018-02-26 00:11:31 +00:00
|
|
|
Serial.printf("WiFi connection dropped, stopping NTP.\n");
|
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) {
|
2018-11-11 17:47:44 +00:00
|
|
|
Serial.printf("Got IP address, starting NTP Synchronization\n");
|
|
|
|
_reconfigureNTP = true;
|
|
|
|
}
|
2018-02-26 00:11:31 +00:00
|
|
|
|
2019-12-24 11:19:19 +00:00
|
|
|
void NTPSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
|
2018-11-11 17:47:44 +00:00
|
|
|
Serial.printf("WiFi connection dropped, stopping NTP.\n");
|
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-01-20 11:14:46 +00:00
|
|
|
if (_enabled) {
|
|
|
|
#ifdef ESP32
|
|
|
|
configTzTime(_tzFormat.c_str(), _server.c_str());
|
|
|
|
#elif defined(ESP8266)
|
|
|
|
configTime(_tzFormat.c_str(), _server.c_str());
|
|
|
|
#endif
|
2019-12-03 23:16:06 +00:00
|
|
|
} else {
|
2020-01-20 11:14:46 +00:00
|
|
|
sntp_stop();
|
2019-12-03 23:16:06 +00:00
|
|
|
}
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|