Fork of the excellent esp8266-react - https://github.com/rjwats/esp8266-react
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.8 KiB

  1. #include <NTPSettingsService.h>
  2. NTPSettingsService::NTPSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) :
  3. AdminSettingsService(server, fs, securityManager, NTP_SETTINGS_SERVICE_PATH, NTP_SETTINGS_FILE) {
  4. #ifdef ESP32
  5. WiFi.onEvent(
  6. std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2),
  7. WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
  8. WiFi.onEvent(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2),
  9. WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
  10. #elif defined(ESP8266)
  11. _onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(
  12. std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
  13. _onStationModeGotIPHandler =
  14. WiFi.onStationModeGotIP(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1));
  15. #endif
  16. }
  17. NTPSettingsService::~NTPSettingsService() {
  18. }
  19. void NTPSettingsService::loop() {
  20. // detect when we need to re-configure NTP and do it in the main loop
  21. if (_reconfigureNTP) {
  22. _reconfigureNTP = false;
  23. configureNTP();
  24. }
  25. }
  26. void NTPSettingsService::readFromJsonObject(JsonObject& root) {
  27. _enabled = root["enabled"] | NTP_SETTINGS_SERVICE_DEFAULT_ENABLED;
  28. _server = root["server"] | NTP_SETTINGS_SERVICE_DEFAULT_SERVER;
  29. _tzLabel = root["tz_label"] | NTP_SETTINGS_SERVICE_DEFAULT_TIME_ZONE_LABEL;
  30. _tzFormat = root["tz_format"] | NTP_SETTINGS_SERVICE_DEFAULT_TIME_ZONE_FORMAT;
  31. }
  32. void NTPSettingsService::writeToJsonObject(JsonObject& root) {
  33. root["enabled"] = _enabled;
  34. root["server"] = _server;
  35. root["tz_label"] = _tzLabel;
  36. root["tz_format"] = _tzFormat;
  37. }
  38. void NTPSettingsService::onConfigUpdated() {
  39. _reconfigureNTP = true;
  40. }
  41. #ifdef ESP32
  42. void NTPSettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
  43. Serial.printf("Got IP address, starting NTP Synchronization\n");
  44. _reconfigureNTP = true;
  45. }
  46. void NTPSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
  47. Serial.printf("WiFi connection dropped, stopping NTP.\n");
  48. _reconfigureNTP = false;
  49. sntp_stop();
  50. }
  51. #elif defined(ESP8266)
  52. void NTPSettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
  53. Serial.printf("Got IP address, starting NTP Synchronization\n");
  54. _reconfigureNTP = true;
  55. }
  56. void NTPSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
  57. Serial.printf("WiFi connection dropped, stopping NTP.\n");
  58. _reconfigureNTP = false;
  59. sntp_stop();
  60. }
  61. #endif
  62. void NTPSettingsService::configureNTP() {
  63. Serial.println("Configuring NTP...");
  64. if (_enabled) {
  65. #ifdef ESP32
  66. configTzTime(_tzFormat.c_str(), _server.c_str());
  67. #elif defined(ESP8266)
  68. configTime(_tzFormat.c_str(), _server.c_str());
  69. #endif
  70. } else {
  71. sntp_stop();
  72. }
  73. }