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.

111 lines
3.6 KiB

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