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.

117 lines
3.6 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. #if defined(ESP8266)
  5. _onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(
  6. std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
  7. _onStationModeGotIPHandler =
  8. WiFi.onStationModeGotIP(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1));
  9. #elif defined(ESP_PLATFORM)
  10. WiFi.onEvent(
  11. std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2),
  12. WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
  13. WiFi.onEvent(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2),
  14. WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
  15. #endif
  16. NTP.onNTPSyncEvent([this](NTPSyncEvent_t ntpEvent) {
  17. _ntpEvent = ntpEvent;
  18. _syncEventTriggered = true;
  19. });
  20. }
  21. NTPSettingsService::~NTPSettingsService() {
  22. }
  23. void NTPSettingsService::loop() {
  24. // detect when we need to re-configure NTP and do it in the main loop
  25. if (_reconfigureNTP) {
  26. _reconfigureNTP = false;
  27. configureNTP();
  28. }
  29. // output sync event to serial
  30. if (_syncEventTriggered) {
  31. processSyncEvent(_ntpEvent);
  32. _syncEventTriggered = false;
  33. }
  34. // keep time synchronized in background
  35. now();
  36. }
  37. void NTPSettingsService::readFromJsonObject(JsonObject& root) {
  38. _server = root["server"] | NTP_SETTINGS_SERVICE_DEFAULT_SERVER;
  39. _interval = root["interval"];
  40. // validate server is specified, resorting to default
  41. _server.trim();
  42. if (!_server) {
  43. _server = NTP_SETTINGS_SERVICE_DEFAULT_SERVER;
  44. }
  45. // make sure interval is in bounds
  46. if (_interval < NTP_SETTINGS_MIN_INTERVAL) {
  47. _interval = NTP_SETTINGS_MIN_INTERVAL;
  48. } else if (_interval > NTP_SETTINGS_MAX_INTERVAL) {
  49. _interval = NTP_SETTINGS_MAX_INTERVAL;
  50. }
  51. }
  52. void NTPSettingsService::writeToJsonObject(JsonObject& root) {
  53. root["server"] = _server;
  54. root["interval"] = _interval;
  55. }
  56. void NTPSettingsService::onConfigUpdated() {
  57. _reconfigureNTP = true;
  58. }
  59. #if defined(ESP8266)
  60. void NTPSettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
  61. Serial.printf("Got IP address, starting NTP Synchronization\n");
  62. _reconfigureNTP = true;
  63. }
  64. void NTPSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
  65. Serial.printf("WiFi connection dropped, stopping NTP.\n");
  66. _reconfigureNTP = false;
  67. NTP.stop();
  68. }
  69. #elif defined(ESP_PLATFORM)
  70. void NTPSettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
  71. Serial.printf("Got IP address, starting NTP Synchronization\n");
  72. _reconfigureNTP = true;
  73. }
  74. void NTPSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
  75. Serial.printf("WiFi connection dropped, stopping NTP.\n");
  76. _reconfigureNTP = false;
  77. NTP.stop();
  78. }
  79. #endif
  80. void NTPSettingsService::configureNTP() {
  81. Serial.println("Configuring NTP...");
  82. // disable sync
  83. NTP.stop();
  84. // enable sync
  85. NTP.begin(_server);
  86. NTP.setInterval(_interval);
  87. }
  88. void NTPSettingsService::processSyncEvent(NTPSyncEvent_t ntpEvent) {
  89. if (ntpEvent) {
  90. Serial.print("Time Sync error: ");
  91. if (ntpEvent == noResponse)
  92. Serial.println("NTP server not reachable");
  93. else if (ntpEvent == invalidAddress)
  94. Serial.println("Invalid NTP server address");
  95. } else {
  96. Serial.print("Got NTP time: ");
  97. Serial.println(NTP.getTimeDateString(NTP.getLastNTPSync()));
  98. }
  99. }