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.

72 lines
1.7 KiB

  1. #ifndef OTASettingsService_h
  2. #define OTASettingsService_h
  3. #include <HttpEndpoint.h>
  4. #include <FSPersistence.h>
  5. #ifdef ESP32
  6. #include <ESPmDNS.h>
  7. #elif defined(ESP8266)
  8. #include <ESP8266mDNS.h>
  9. #endif
  10. #include <ArduinoOTA.h>
  11. #include <WiFiUdp.h>
  12. #ifndef FACTORY_OTA_PORT
  13. #define FACTORY_OTA_PORT 8266
  14. #endif
  15. #ifndef FACTORY_OTA_PASSWORD
  16. #define FACTORY_OTA_PASSWORD "esp-react"
  17. #endif
  18. #ifndef FACTORY_OTA_ENABLED
  19. #define FACTORY_OTA_ENABLED true
  20. #endif
  21. #define OTA_SETTINGS_FILE "/config/otaSettings.json"
  22. #define OTA_SETTINGS_SERVICE_PATH "/rest/otaSettings"
  23. class OTASettings {
  24. public:
  25. bool enabled;
  26. int port;
  27. String password;
  28. static void read(OTASettings& settings, JsonObject& root) {
  29. root["enabled"] = settings.enabled;
  30. root["port"] = settings.port;
  31. root["password"] = settings.password;
  32. }
  33. static StateUpdateResult update(JsonObject& root, OTASettings& settings) {
  34. settings.enabled = root["enabled"] | FACTORY_OTA_ENABLED;
  35. settings.port = root["port"] | FACTORY_OTA_PORT;
  36. settings.password = root["password"] | FACTORY_OTA_PASSWORD;
  37. return StateUpdateResult::CHANGED;
  38. }
  39. };
  40. class OTASettingsService : public StatefulService<OTASettings> {
  41. public:
  42. OTASettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
  43. void begin();
  44. void loop();
  45. private:
  46. HttpEndpoint<OTASettings> _httpEndpoint;
  47. FSPersistence<OTASettings> _fsPersistence;
  48. ArduinoOTAClass* _arduinoOTA;
  49. void configureArduinoOTA();
  50. #ifdef ESP32
  51. void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
  52. #elif defined(ESP8266)
  53. WiFiEventHandler _onStationModeGotIPHandler;
  54. void onStationModeGotIP(const WiFiEventStationModeGotIP& event);
  55. #endif
  56. };
  57. #endif // end OTASettingsService_h