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.

71 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 serialize(OTASettings& settings, JsonObject& root) {
  29. root["enabled"] = settings.enabled;
  30. root["port"] = settings.port;
  31. root["password"] = settings.password;
  32. }
  33. static void deserialize(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. }
  38. };
  39. class OTASettingsService : public StatefulService<OTASettings> {
  40. public:
  41. OTASettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
  42. void begin();
  43. void loop();
  44. private:
  45. HttpEndpoint<OTASettings> _httpEndpoint;
  46. FSPersistence<OTASettings> _fsPersistence;
  47. ArduinoOTAClass* _arduinoOTA;
  48. void configureArduinoOTA();
  49. #ifdef ESP32
  50. void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
  51. #elif defined(ESP8266)
  52. WiFiEventHandler _onStationModeGotIPHandler;
  53. void onStationModeGotIP(const WiFiEventStationModeGotIP& event);
  54. #endif
  55. };
  56. #endif // end OTASettingsService_h