2018-02-26 00:11:31 +00:00
|
|
|
#include <APSettingsService.h>
|
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
APSettingsService::APSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) :
|
2020-05-14 22:23:45 +00:00
|
|
|
_httpEndpoint(APSettings::serialize,
|
|
|
|
APSettings::deserialize,
|
|
|
|
this,
|
|
|
|
server,
|
|
|
|
AP_SETTINGS_SERVICE_PATH,
|
|
|
|
securityManager),
|
|
|
|
_fsPersistence(APSettings::serialize, APSettings::deserialize, this, fs, AP_SETTINGS_FILE) {
|
|
|
|
addUpdateHandler([&](String originId) { reconfigureAP(); }, false);
|
2019-12-03 23:16:06 +00:00
|
|
|
}
|
2018-02-26 00:11:31 +00:00
|
|
|
|
2019-09-30 20:28:24 +00:00
|
|
|
void APSettingsService::begin() {
|
2020-05-14 22:23:45 +00:00
|
|
|
_fsPersistence.readFromFS();
|
2020-02-01 08:44:26 +00:00
|
|
|
reconfigureAP();
|
|
|
|
}
|
|
|
|
|
|
|
|
void APSettingsService::reconfigureAP() {
|
|
|
|
_lastManaged = millis() - MANAGE_NETWORK_DELAY;
|
2019-09-30 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2018-02-26 00:11:31 +00:00
|
|
|
void APSettingsService::loop() {
|
2018-10-23 12:55:25 +00:00
|
|
|
unsigned long currentMillis = millis();
|
|
|
|
unsigned long manageElapsed = (unsigned long)(currentMillis - _lastManaged);
|
2019-12-03 23:16:06 +00:00
|
|
|
if (manageElapsed >= MANAGE_NETWORK_DELAY) {
|
2018-10-23 12:55:25 +00:00
|
|
|
_lastManaged = currentMillis;
|
2019-12-03 23:16:06 +00:00
|
|
|
manageAP();
|
|
|
|
}
|
2018-03-06 22:39:33 +00:00
|
|
|
handleDNS();
|
|
|
|
}
|
|
|
|
|
|
|
|
void APSettingsService::manageAP() {
|
|
|
|
WiFiMode_t currentWiFiMode = WiFi.getMode();
|
2020-05-14 22:23:45 +00:00
|
|
|
if (_state.provisionMode == AP_MODE_ALWAYS ||
|
|
|
|
(_state.provisionMode == AP_MODE_DISCONNECTED && WiFi.status() != WL_CONNECTED)) {
|
2018-03-06 22:39:33 +00:00
|
|
|
if (currentWiFiMode == WIFI_OFF || currentWiFiMode == WIFI_STA) {
|
|
|
|
startAP();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (currentWiFiMode == WIFI_AP || currentWiFiMode == WIFI_AP_STA) {
|
|
|
|
stopAP();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void APSettingsService::startAP() {
|
|
|
|
Serial.println("Starting software access point");
|
2020-05-14 22:23:45 +00:00
|
|
|
WiFi.softAP(_state.ssid.c_str(), _state.password.c_str());
|
2018-03-06 22:39:33 +00:00
|
|
|
if (!_dnsServer) {
|
|
|
|
IPAddress apIp = WiFi.softAPIP();
|
|
|
|
Serial.print("Starting captive portal on ");
|
|
|
|
Serial.println(apIp);
|
|
|
|
_dnsServer = new DNSServer;
|
|
|
|
_dnsServer->start(DNS_PORT, "*", apIp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void APSettingsService::stopAP() {
|
|
|
|
if (_dnsServer) {
|
|
|
|
Serial.println("Stopping captive portal");
|
|
|
|
_dnsServer->stop();
|
|
|
|
delete _dnsServer;
|
2019-06-02 22:15:56 +00:00
|
|
|
_dnsServer = nullptr;
|
2018-03-06 22:39:33 +00:00
|
|
|
}
|
|
|
|
Serial.println("Stopping software access point");
|
|
|
|
WiFi.softAPdisconnect(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void APSettingsService::handleDNS() {
|
|
|
|
if (_dnsServer) {
|
|
|
|
_dnsServer->processNextRequest();
|
|
|
|
}
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|