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.

82 lines
2.6 KiB

  1. #include <APSettingsService.h>
  2. APSettingsService::APSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) :
  3. _httpEndpoint(APSettings::read, APSettings::update, this, server, AP_SETTINGS_SERVICE_PATH, securityManager),
  4. _fsPersistence(APSettings::read, APSettings::update, this, fs, AP_SETTINGS_FILE),
  5. _dnsServer(nullptr),
  6. _lastManaged(0),
  7. _reconfigureAp(false) {
  8. addUpdateHandler([&](const String& originId) { reconfigureAP(); }, false);
  9. }
  10. void APSettingsService::begin() {
  11. _fsPersistence.readFromFS();
  12. reconfigureAP();
  13. }
  14. void APSettingsService::reconfigureAP() {
  15. _lastManaged = millis() - MANAGE_NETWORK_DELAY;
  16. _reconfigureAp = true;
  17. }
  18. void APSettingsService::loop() {
  19. unsigned long currentMillis = millis();
  20. unsigned long manageElapsed = (unsigned long)(currentMillis - _lastManaged);
  21. if (manageElapsed >= MANAGE_NETWORK_DELAY) {
  22. _lastManaged = currentMillis;
  23. manageAP();
  24. }
  25. handleDNS();
  26. }
  27. void APSettingsService::manageAP() {
  28. WiFiMode_t currentWiFiMode = WiFi.getMode();
  29. if (_state.provisionMode == AP_MODE_ALWAYS ||
  30. (_state.provisionMode == AP_MODE_DISCONNECTED && WiFi.status() != WL_CONNECTED)) {
  31. if (_reconfigureAp || currentWiFiMode == WIFI_OFF || currentWiFiMode == WIFI_STA) {
  32. startAP();
  33. }
  34. } else if ((currentWiFiMode == WIFI_AP || currentWiFiMode == WIFI_AP_STA) &&
  35. (_reconfigureAp || !WiFi.softAPgetStationNum())) {
  36. stopAP();
  37. }
  38. _reconfigureAp = false;
  39. }
  40. void APSettingsService::startAP() {
  41. Serial.println(F("Starting software access point"));
  42. WiFi.softAP(_state.ssid.c_str(), _state.password.c_str());
  43. if (!_dnsServer) {
  44. IPAddress apIp = WiFi.softAPIP();
  45. Serial.print(F("Starting captive portal on "));
  46. Serial.println(apIp);
  47. _dnsServer = new DNSServer;
  48. _dnsServer->start(DNS_PORT, "*", apIp);
  49. }
  50. }
  51. void APSettingsService::stopAP() {
  52. if (_dnsServer) {
  53. Serial.println(F("Stopping captive portal"));
  54. _dnsServer->stop();
  55. delete _dnsServer;
  56. _dnsServer = nullptr;
  57. }
  58. Serial.println(F("Stopping software access point"));
  59. WiFi.softAPdisconnect(true);
  60. }
  61. void APSettingsService::handleDNS() {
  62. if (_dnsServer) {
  63. _dnsServer->processNextRequest();
  64. }
  65. }
  66. APNetworkStatus APSettingsService::getAPNetworkStatus() {
  67. WiFiMode_t currentWiFiMode = WiFi.getMode();
  68. bool apActive = currentWiFiMode == WIFI_AP || currentWiFiMode == WIFI_AP_STA;
  69. if (apActive && _state.provisionMode != AP_MODE_ALWAYS && WiFi.status() == WL_CONNECTED) {
  70. return APNetworkStatus::LINGERING;
  71. }
  72. return apActive ? APNetworkStatus::ACTIVE : APNetworkStatus::INACTIVE;
  73. }