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.

83 lines
1.9 KiB

  1. #ifndef APSettingsConfig_h
  2. #define APSettingsConfig_h
  3. #include <HttpEndpoint.h>
  4. #include <FSPersistence.h>
  5. #include <DNSServer.h>
  6. #include <IPAddress.h>
  7. #define MANAGE_NETWORK_DELAY 10000
  8. #define AP_MODE_ALWAYS 0
  9. #define AP_MODE_DISCONNECTED 1
  10. #define AP_MODE_NEVER 2
  11. #define DNS_PORT 53
  12. #ifndef FACTORY_AP_SSID
  13. #define FACTORY_AP_SSID "ESP8266-React"
  14. #endif
  15. #ifndef FACTORY_AP_PASSWORD
  16. #define FACTORY_AP_PASSWORD "esp-react"
  17. #endif
  18. #ifndef FACTORY_AP_PROVISION_MODE
  19. #define FACTORY_AP_PROVISION_MODE AP_MODE_DISCONNECTED
  20. #endif
  21. #define AP_SETTINGS_FILE "/config/apSettings.json"
  22. #define AP_SETTINGS_SERVICE_PATH "/rest/apSettings"
  23. class APSettings {
  24. public:
  25. uint8_t provisionMode;
  26. String ssid;
  27. String password;
  28. static void serialize(APSettings& settings, JsonObject& root) {
  29. root["provision_mode"] = settings.provisionMode;
  30. root["ssid"] = settings.ssid;
  31. root["password"] = settings.password;
  32. }
  33. static void deserialize(JsonObject& root, APSettings& settings) {
  34. settings.provisionMode = root["provision_mode"] | FACTORY_AP_PROVISION_MODE;
  35. switch (settings.provisionMode) {
  36. case AP_MODE_ALWAYS:
  37. case AP_MODE_DISCONNECTED:
  38. case AP_MODE_NEVER:
  39. break;
  40. default:
  41. settings.provisionMode = AP_MODE_ALWAYS;
  42. }
  43. settings.ssid = root["ssid"] | FACTORY_AP_SSID;
  44. settings.password = root["password"] | FACTORY_AP_PASSWORD;
  45. }
  46. };
  47. class APSettingsService : public StatefulService<APSettings> {
  48. public:
  49. APSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
  50. void begin();
  51. void loop();
  52. private:
  53. HttpEndpoint<APSettings> _httpEndpoint;
  54. FSPersistence<APSettings> _fsPersistence;
  55. // for the mangement delay loop
  56. unsigned long _lastManaged;
  57. // for the captive portal
  58. DNSServer* _dnsServer;
  59. void reconfigureAP();
  60. void manageAP();
  61. void startAP();
  62. void stopAP();
  63. void handleDNS();
  64. };
  65. #endif // end APSettingsConfig_h