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.

90 lines
3.4 KiB

  1. #include <NTPSettingsService.h>
  2. NTPSettingsService::NTPSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) :
  3. _httpEndpoint(NTPSettings::read, NTPSettings::update, this, server, NTP_SETTINGS_SERVICE_PATH, securityManager),
  4. _fsPersistence(NTPSettings::read, NTPSettings::update, this, fs, NTP_SETTINGS_FILE),
  5. _timeHandler(TIME_PATH,
  6. securityManager->wrapCallback(
  7. std::bind(&NTPSettingsService::configureTime, this, std::placeholders::_1, std::placeholders::_2),
  8. AuthenticationPredicates::IS_ADMIN)) {
  9. _timeHandler.setMethod(HTTP_POST);
  10. _timeHandler.setMaxContentLength(MAX_TIME_SIZE);
  11. server->addHandler(&_timeHandler);
  12. #ifdef ESP32
  13. WiFi.onEvent(
  14. std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2),
  15. WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
  16. WiFi.onEvent(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2),
  17. WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
  18. #elif defined(ESP8266)
  19. _onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(
  20. std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
  21. _onStationModeGotIPHandler =
  22. WiFi.onStationModeGotIP(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1));
  23. #endif
  24. addUpdateHandler([&](const String& originId) { configureNTP(); }, false);
  25. }
  26. void NTPSettingsService::begin() {
  27. _fsPersistence.readFromFS();
  28. configureNTP();
  29. }
  30. #ifdef ESP32
  31. void NTPSettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
  32. Serial.println(F("Got IP address, starting NTP Synchronization"));
  33. configureNTP();
  34. }
  35. void NTPSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
  36. Serial.println(F("WiFi connection dropped, stopping NTP."));
  37. configureNTP();
  38. }
  39. #elif defined(ESP8266)
  40. void NTPSettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
  41. Serial.println(F("Got IP address, starting NTP Synchronization"));
  42. configureNTP();
  43. }
  44. void NTPSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
  45. Serial.println(F("WiFi connection dropped, stopping NTP."));
  46. configureNTP();
  47. }
  48. #endif
  49. void NTPSettingsService::configureNTP() {
  50. if (WiFi.isConnected() && _state.enabled) {
  51. Serial.println(F("Starting NTP..."));
  52. #ifdef ESP32
  53. configTzTime(_state.tzFormat.c_str(), _state.server.c_str());
  54. #elif defined(ESP8266)
  55. configTime(_state.tzFormat.c_str(), _state.server.c_str());
  56. #endif
  57. } else {
  58. #ifdef ESP32
  59. setenv("TZ", _state.tzFormat.c_str(), 1);
  60. tzset();
  61. #elif defined(ESP8266)
  62. setTZ(_state.tzFormat.c_str());
  63. #endif
  64. sntp_stop();
  65. }
  66. }
  67. void NTPSettingsService::configureTime(AsyncWebServerRequest* request, JsonVariant& json) {
  68. if (!sntp_enabled() && json.is<JsonObject>()) {
  69. String timeUtc = json["time_utc"];
  70. struct tm tm = {0};
  71. char* s = strptime(timeUtc.c_str(), "%Y-%m-%dT%H:%M:%SZ", &tm);
  72. if (s != nullptr) {
  73. time_t time = mktime(&tm);
  74. struct timeval now = {.tv_sec = time};
  75. settimeofday(&now, nullptr);
  76. AsyncWebServerResponse* response = request->beginResponse(200);
  77. request->send(response);
  78. return;
  79. }
  80. }
  81. AsyncWebServerResponse* response = request->beginResponse(400);
  82. request->send(response);
  83. }