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) :
|
2020-05-29 19:18:43 +00:00
|
|
|
_httpEndpoint(NTPSettings::read, NTPSettings::update, this, server, NTP_SETTINGS_SERVICE_PATH, securityManager),
|
|
|
|
_fsPersistence(NTPSettings::read, NTPSettings::update, this, fs, 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
|
2020-05-21 07:42:21 +00:00
|
|
|
addUpdateHandler([&](const String& originId) { configureNTP(); }, false);
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 22:23:45 +00:00
|
|
|
void NTPSettingsService::begin() {
|
|
|
|
_fsPersistence.readFromFS();
|
|
|
|
configureNTP();
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 11:19:19 +00:00
|
|
|
#ifdef ESP32
|
|
|
|
void NTPSettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
|
2020-05-21 07:42:21 +00:00
|
|
|
Serial.println(F("Got IP address, starting NTP Synchronization"));
|
2020-05-14 22:23:45 +00:00
|
|
|
configureNTP();
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 11:19:19 +00:00
|
|
|
void NTPSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
|
2020-05-21 07:42:21 +00:00
|
|
|
Serial.println(F("WiFi connection dropped, stopping NTP."));
|
2020-05-14 22:23:45 +00:00
|
|
|
configureNTP();
|
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-05-21 07:42:21 +00:00
|
|
|
Serial.println(F("Got IP address, starting NTP Synchronization"));
|
2020-05-14 22:23:45 +00:00
|
|
|
configureNTP();
|
2018-11-11 17:47:44 +00:00
|
|
|
}
|
2018-02-26 00:11:31 +00:00
|
|
|
|
2019-12-24 11:19:19 +00:00
|
|
|
void NTPSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
|
2020-05-21 07:42:21 +00:00
|
|
|
Serial.println(F("WiFi connection dropped, stopping NTP."));
|
2020-05-14 22:23:45 +00:00
|
|
|
configureNTP();
|
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() {
|
2020-05-14 22:23:45 +00:00
|
|
|
if (WiFi.isConnected() && _state.enabled) {
|
2020-05-21 07:42:21 +00:00
|
|
|
Serial.println(F("Starting NTP..."));
|
2020-01-20 11:14:46 +00:00
|
|
|
#ifdef ESP32
|
2020-05-14 22:23:45 +00:00
|
|
|
configTzTime(_state.tzFormat.c_str(), _state.server.c_str());
|
2020-01-20 11:14:46 +00:00
|
|
|
#elif defined(ESP8266)
|
2020-05-14 22:23:45 +00:00
|
|
|
configTime(_state.tzFormat.c_str(), _state.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
|
|
|
}
|