🪄 Formats APSettingsService
This commit is contained in:
@@ -14,86 +14,70 @@
|
|||||||
|
|
||||||
#include <APSettingsService.h>
|
#include <APSettingsService.h>
|
||||||
|
|
||||||
static const char* TAG = "APSettingsService";
|
static const char *TAG = "APSettingsService";
|
||||||
|
|
||||||
APSettingsService::APSettingsService(PsychicHttpServer *server,
|
APSettingsService::APSettingsService(PsychicHttpServer *server, FS *fs, SecurityManager *securityManager)
|
||||||
FS *fs,
|
: _server(server),
|
||||||
SecurityManager *securityManager) : _server(server),
|
|
||||||
_securityManager(securityManager),
|
_securityManager(securityManager),
|
||||||
_httpEndpoint(APSettings::read, APSettings::update, this, server, AP_SETTINGS_SERVICE_PATH, securityManager),
|
_httpEndpoint(APSettings::read, APSettings::update, this, server, AP_SETTINGS_SERVICE_PATH, securityManager),
|
||||||
_fsPersistence(APSettings::read, APSettings::update, this, fs, AP_SETTINGS_FILE),
|
_fsPersistence(APSettings::read, APSettings::update, this, fs, AP_SETTINGS_FILE),
|
||||||
_dnsServer(nullptr),
|
_dnsServer(nullptr),
|
||||||
_lastManaged(0),
|
_lastManaged(0),
|
||||||
_reconfigureAp(false)
|
_reconfigureAp(false) {
|
||||||
{
|
addUpdateHandler([&](const String &originId) { reconfigureAP(); }, false);
|
||||||
addUpdateHandler([&](const String &originId)
|
|
||||||
{ reconfigureAP(); },
|
|
||||||
false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void APSettingsService::begin()
|
void APSettingsService::begin() {
|
||||||
{
|
|
||||||
_httpEndpoint.begin();
|
_httpEndpoint.begin();
|
||||||
_fsPersistence.readFromFS();
|
_fsPersistence.readFromFS();
|
||||||
reconfigureAP();
|
reconfigureAP();
|
||||||
}
|
}
|
||||||
|
|
||||||
void APSettingsService::reconfigureAP()
|
void APSettingsService::reconfigureAP() {
|
||||||
{
|
|
||||||
_lastManaged = millis() - MANAGE_NETWORK_DELAY;
|
_lastManaged = millis() - MANAGE_NETWORK_DELAY;
|
||||||
_reconfigureAp = true;
|
_reconfigureAp = true;
|
||||||
_recoveryMode = false;
|
_recoveryMode = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void APSettingsService::recoveryMode()
|
void APSettingsService::recoveryMode() {
|
||||||
{
|
|
||||||
ESP_LOGI(TAG, "Recovery Mode needed");
|
ESP_LOGI(TAG, "Recovery Mode needed");
|
||||||
_lastManaged = millis() - MANAGE_NETWORK_DELAY;
|
_lastManaged = millis() - MANAGE_NETWORK_DELAY;
|
||||||
_recoveryMode = true;
|
_recoveryMode = true;
|
||||||
_reconfigureAp = true;
|
_reconfigureAp = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void APSettingsService::loop()
|
void APSettingsService::loop() {
|
||||||
{
|
|
||||||
unsigned long currentMillis = millis();
|
unsigned long currentMillis = millis();
|
||||||
unsigned long manageElapsed = (currentMillis - _lastManaged);
|
unsigned long manageElapsed = (currentMillis - _lastManaged);
|
||||||
if (manageElapsed >= MANAGE_NETWORK_DELAY)
|
if (manageElapsed >= MANAGE_NETWORK_DELAY) {
|
||||||
{
|
|
||||||
_lastManaged = currentMillis;
|
_lastManaged = currentMillis;
|
||||||
manageAP();
|
manageAP();
|
||||||
}
|
}
|
||||||
handleDNS();
|
handleDNS();
|
||||||
}
|
}
|
||||||
|
|
||||||
void APSettingsService::manageAP()
|
void APSettingsService::manageAP() {
|
||||||
{
|
|
||||||
WiFiMode_t currentWiFiMode = WiFi.getMode();
|
WiFiMode_t currentWiFiMode = WiFi.getMode();
|
||||||
if (_state.provisionMode == AP_MODE_ALWAYS ||
|
if (_state.provisionMode == AP_MODE_ALWAYS ||
|
||||||
(_state.provisionMode == AP_MODE_DISCONNECTED && WiFi.status() != WL_CONNECTED) || _recoveryMode)
|
(_state.provisionMode == AP_MODE_DISCONNECTED && WiFi.status() != WL_CONNECTED) || _recoveryMode) {
|
||||||
{
|
if (_reconfigureAp || currentWiFiMode == WIFI_OFF || currentWiFiMode == WIFI_STA) {
|
||||||
if (_reconfigureAp || currentWiFiMode == WIFI_OFF || currentWiFiMode == WIFI_STA)
|
|
||||||
{
|
|
||||||
startAP();
|
startAP();
|
||||||
}
|
}
|
||||||
}
|
} else if ((currentWiFiMode == WIFI_AP || currentWiFiMode == WIFI_AP_STA) &&
|
||||||
else if ((currentWiFiMode == WIFI_AP || currentWiFiMode == WIFI_AP_STA) &&
|
(_reconfigureAp || !WiFi.softAPgetStationNum())) {
|
||||||
(_reconfigureAp || !WiFi.softAPgetStationNum()))
|
|
||||||
{
|
|
||||||
stopAP();
|
stopAP();
|
||||||
}
|
}
|
||||||
_reconfigureAp = false;
|
_reconfigureAp = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void APSettingsService::startAP()
|
void APSettingsService::startAP() {
|
||||||
{
|
|
||||||
ESP_LOGI(TAG, "Starting software access point");
|
ESP_LOGI(TAG, "Starting software access point");
|
||||||
WiFi.softAPConfig(_state.localIP, _state.gatewayIP, _state.subnetMask);
|
WiFi.softAPConfig(_state.localIP, _state.gatewayIP, _state.subnetMask);
|
||||||
WiFi.softAP(_state.ssid.c_str(), _state.password.c_str(), _state.channel, _state.ssidHidden, _state.maxClients);
|
WiFi.softAP(_state.ssid.c_str(), _state.password.c_str(), _state.channel, _state.ssidHidden, _state.maxClients);
|
||||||
#if CONFIG_IDF_TARGET_ESP32C3
|
#if CONFIG_IDF_TARGET_ESP32C3
|
||||||
WiFi.setTxPower(WIFI_POWER_8_5dBm); // https://www.wemos.cc/en/latest/c3/c3_mini_1_0_0.html#about-wifi
|
WiFi.setTxPower(WIFI_POWER_8_5dBm); // https://www.wemos.cc/en/latest/c3/c3_mini_1_0_0.html#about-wifi
|
||||||
#endif
|
#endif
|
||||||
if (!_dnsServer)
|
if (!_dnsServer) {
|
||||||
{
|
|
||||||
IPAddress apIp = WiFi.softAPIP();
|
IPAddress apIp = WiFi.softAPIP();
|
||||||
ESP_LOGI(TAG, "Starting captive portal on %s", apIp.toString().c_str());
|
ESP_LOGI(TAG, "Starting captive portal on %s", apIp.toString().c_str());
|
||||||
_dnsServer = new DNSServer;
|
_dnsServer = new DNSServer;
|
||||||
@@ -101,10 +85,8 @@ void APSettingsService::startAP()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void APSettingsService::stopAP()
|
void APSettingsService::stopAP() {
|
||||||
{
|
if (_dnsServer) {
|
||||||
if (_dnsServer)
|
|
||||||
{
|
|
||||||
ESP_LOGI(TAG, "Stopping captive portal");
|
ESP_LOGI(TAG, "Stopping captive portal");
|
||||||
_dnsServer->stop();
|
_dnsServer->stop();
|
||||||
delete _dnsServer;
|
delete _dnsServer;
|
||||||
@@ -114,20 +96,16 @@ void APSettingsService::stopAP()
|
|||||||
WiFi.softAPdisconnect(true);
|
WiFi.softAPdisconnect(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void APSettingsService::handleDNS()
|
void APSettingsService::handleDNS() {
|
||||||
{
|
if (_dnsServer) {
|
||||||
if (_dnsServer)
|
|
||||||
{
|
|
||||||
_dnsServer->processNextRequest();
|
_dnsServer->processNextRequest();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
APNetworkStatus APSettingsService::getAPNetworkStatus()
|
APNetworkStatus APSettingsService::getAPNetworkStatus() {
|
||||||
{
|
|
||||||
WiFiMode_t currentWiFiMode = WiFi.getMode();
|
WiFiMode_t currentWiFiMode = WiFi.getMode();
|
||||||
bool apActive = currentWiFiMode == WIFI_AP || currentWiFiMode == WIFI_AP_STA;
|
bool apActive = currentWiFiMode == WIFI_AP || currentWiFiMode == WIFI_AP_STA;
|
||||||
if (apActive && _state.provisionMode != AP_MODE_ALWAYS && WiFi.status() == WL_CONNECTED)
|
if (apActive && _state.provisionMode != AP_MODE_ALWAYS && WiFi.status() == WL_CONNECTED) {
|
||||||
{
|
|
||||||
return APNetworkStatus::LINGERING;
|
return APNetworkStatus::LINGERING;
|
||||||
}
|
}
|
||||||
return apActive ? APNetworkStatus::ACTIVE : APNetworkStatus::INACTIVE;
|
return apActive ? APNetworkStatus::ACTIVE : APNetworkStatus::INACTIVE;
|
||||||
|
|||||||
@@ -70,16 +70,10 @@
|
|||||||
#define MANAGE_NETWORK_DELAY 10000
|
#define MANAGE_NETWORK_DELAY 10000
|
||||||
#define DNS_PORT 53
|
#define DNS_PORT 53
|
||||||
|
|
||||||
enum APNetworkStatus
|
enum APNetworkStatus { ACTIVE = 0, INACTIVE, LINGERING };
|
||||||
{
|
|
||||||
ACTIVE = 0,
|
|
||||||
INACTIVE,
|
|
||||||
LINGERING
|
|
||||||
};
|
|
||||||
|
|
||||||
class APSettings
|
class APSettings {
|
||||||
{
|
public:
|
||||||
public:
|
|
||||||
uint8_t provisionMode;
|
uint8_t provisionMode;
|
||||||
String ssid;
|
String ssid;
|
||||||
String password;
|
String password;
|
||||||
@@ -91,15 +85,13 @@ public:
|
|||||||
IPAddress gatewayIP;
|
IPAddress gatewayIP;
|
||||||
IPAddress subnetMask;
|
IPAddress subnetMask;
|
||||||
|
|
||||||
bool operator==(const APSettings &settings) const
|
bool operator==(const APSettings &settings) const {
|
||||||
{
|
|
||||||
return provisionMode == settings.provisionMode && ssid == settings.ssid && password == settings.password &&
|
return provisionMode == settings.provisionMode && ssid == settings.ssid && password == settings.password &&
|
||||||
channel == settings.channel && ssidHidden == settings.ssidHidden && maxClients == settings.maxClients &&
|
channel == settings.channel && ssidHidden == settings.ssidHidden && maxClients == settings.maxClients &&
|
||||||
localIP == settings.localIP && gatewayIP == settings.gatewayIP && subnetMask == settings.subnetMask;
|
localIP == settings.localIP && gatewayIP == settings.gatewayIP && subnetMask == settings.subnetMask;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void read(APSettings &settings, JsonObject &root)
|
static void read(APSettings &settings, JsonObject &root) {
|
||||||
{
|
|
||||||
root["provision_mode"] = settings.provisionMode;
|
root["provision_mode"] = settings.provisionMode;
|
||||||
root["ssid"] = settings.ssid;
|
root["ssid"] = settings.ssid;
|
||||||
root["password"] = settings.password;
|
root["password"] = settings.password;
|
||||||
@@ -111,18 +103,14 @@ public:
|
|||||||
root["subnet_mask"] = settings.subnetMask.toString();
|
root["subnet_mask"] = settings.subnetMask.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
static StateUpdateResult update(JsonObject &root, APSettings &settings)
|
static StateUpdateResult update(JsonObject &root, APSettings &settings) {
|
||||||
{
|
|
||||||
APSettings newSettings = {};
|
APSettings newSettings = {};
|
||||||
newSettings.provisionMode = root["provision_mode"] | FACTORY_AP_PROVISION_MODE;
|
newSettings.provisionMode = root["provision_mode"] | FACTORY_AP_PROVISION_MODE;
|
||||||
switch (settings.provisionMode)
|
switch (settings.provisionMode) {
|
||||||
{
|
|
||||||
case AP_MODE_ALWAYS:
|
case AP_MODE_ALWAYS:
|
||||||
case AP_MODE_DISCONNECTED:
|
case AP_MODE_DISCONNECTED:
|
||||||
case AP_MODE_NEVER:
|
case AP_MODE_NEVER: break;
|
||||||
break;
|
default: newSettings.provisionMode = AP_MODE_DISCONNECTED;
|
||||||
default:
|
|
||||||
newSettings.provisionMode = AP_MODE_DISCONNECTED;
|
|
||||||
}
|
}
|
||||||
newSettings.ssid = root["ssid"] | SettingValue::format(FACTORY_AP_SSID);
|
newSettings.ssid = root["ssid"] | SettingValue::format(FACTORY_AP_SSID);
|
||||||
newSettings.password = root["password"] | FACTORY_AP_PASSWORD;
|
newSettings.password = root["password"] | FACTORY_AP_PASSWORD;
|
||||||
@@ -134,8 +122,7 @@ public:
|
|||||||
JsonUtils::readIP(root, "gateway_ip", newSettings.gatewayIP, FACTORY_AP_GATEWAY_IP);
|
JsonUtils::readIP(root, "gateway_ip", newSettings.gatewayIP, FACTORY_AP_GATEWAY_IP);
|
||||||
JsonUtils::readIP(root, "subnet_mask", newSettings.subnetMask, FACTORY_AP_SUBNET_MASK);
|
JsonUtils::readIP(root, "subnet_mask", newSettings.subnetMask, FACTORY_AP_SUBNET_MASK);
|
||||||
|
|
||||||
if (newSettings == settings)
|
if (newSettings == settings) {
|
||||||
{
|
|
||||||
return StateUpdateResult::UNCHANGED;
|
return StateUpdateResult::UNCHANGED;
|
||||||
}
|
}
|
||||||
settings = newSettings;
|
settings = newSettings;
|
||||||
@@ -143,9 +130,8 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class APSettingsService : public StatefulService<APSettings>
|
class APSettingsService : public StatefulService<APSettings> {
|
||||||
{
|
public:
|
||||||
public:
|
|
||||||
APSettingsService(PsychicHttpServer *server, FS *fs, SecurityManager *securityManager);
|
APSettingsService(PsychicHttpServer *server, FS *fs, SecurityManager *securityManager);
|
||||||
|
|
||||||
void begin();
|
void begin();
|
||||||
@@ -153,7 +139,7 @@ public:
|
|||||||
APNetworkStatus getAPNetworkStatus();
|
APNetworkStatus getAPNetworkStatus();
|
||||||
void recoveryMode();
|
void recoveryMode();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PsychicHttpServer *_server;
|
PsychicHttpServer *_server;
|
||||||
SecurityManager *_securityManager;
|
SecurityManager *_securityManager;
|
||||||
HttpEndpoint<APSettings> _httpEndpoint;
|
HttpEndpoint<APSettings> _httpEndpoint;
|
||||||
|
|||||||
Reference in New Issue
Block a user