🛜 Simplifies ap service

This commit is contained in:
Rune Harlyk
2024-09-03 21:16:40 +02:00
committed by Rune Harlyk
parent 8ac2fad1b1
commit 756f1c0148
7 changed files with 99 additions and 193 deletions
-37
View File
@@ -1,37 +0,0 @@
/**
* ESP32 SvelteKit
*
* A simple, secure and extensible framework for IoT projects for ESP32 platforms
* with responsive Sveltekit front-end built with TailwindCSS and DaisyUI.
* https://github.com/theelims/ESP32-sveltekit
*
* Copyright (C) 2018 - 2023 rjwats
* Copyright (C) 2023 theelims
*
* All Rights Reserved. This software may be modified and distributed under
* the terms of the LGPL v3 license. See the LICENSE file for details.
**/
#include <APStatus.h>
APStatus::APStatus(PsychicHttpServer *server, SecurityManager *securityManager, APSettingsService *apSettingsService)
: _server(server), _securityManager(securityManager), _apSettingsService(apSettingsService) {}
void APStatus::begin() {
_server->on(AP_STATUS_SERVICE_PATH, HTTP_GET,
_securityManager->wrapRequest(std::bind(&APStatus::apStatus, this, std::placeholders::_1),
AuthenticationPredicates::IS_AUTHENTICATED));
ESP_LOGV("APStatus", "Registered GET endpoint: %s", AP_STATUS_SERVICE_PATH);
}
esp_err_t APStatus::apStatus(PsychicRequest *request) {
PsychicJsonResponse response = PsychicJsonResponse(request, false);
JsonObject root = response.getRoot();
root["status"] = _apSettingsService->getAPNetworkStatus();
root["ip_address"] = WiFi.softAPIP().toString();
root["mac_address"] = WiFi.softAPmacAddress();
root["station_num"] = WiFi.softAPgetStationNum();
return response.send();
}
-41
View File
@@ -1,41 +0,0 @@
#ifndef APStatus_h
#define APStatus_h
/**
* ESP32 SvelteKit
*
* A simple, secure and extensible framework for IoT projects for ESP32 platforms
* with responsive Sveltekit front-end built with TailwindCSS and DaisyUI.
* https://github.com/theelims/ESP32-sveltekit
*
* Copyright (C) 2018 - 2023 rjwats
* Copyright (C) 2023 theelims
*
* All Rights Reserved. This software may be modified and distributed under
* the terms of the LGPL v3 license. See the LICENSE file for details.
**/
#include <WiFi.h>
#include <ArduinoJson.h>
#include <PsychicHttp.h>
#include <IPAddress.h>
#include <SecurityManager.h>
#include <APSettingsService.h>
#define AP_STATUS_SERVICE_PATH "/api/apStatus"
class APStatus {
public:
APStatus(PsychicHttpServer *server, SecurityManager *securityManager, APSettingsService *apSettingsService);
void begin();
private:
PsychicHttpServer *_server;
SecurityManager *_securityManager;
APSettingsService *_apSettingsService;
esp_err_t apStatus(PsychicRequest *request);
};
#endif // end APStatus_h
+12 -5
View File
@@ -21,8 +21,6 @@ ESP32SvelteKit::ESP32SvelteKit(PsychicHttpServer *server, unsigned int numberEnd
_taskManager(),
_featureService(server),
_securitySettingsService(server, &ESPFS),
_apSettingsService(server, &ESPFS, &_securitySettingsService),
_apStatus(server, &_securitySettingsService, &_apSettingsService),
_socket(server, &_securitySettingsService, AuthenticationPredicates::IS_AUTHENTICATED),
#if FT_ENABLED(USE_NTP)
_ntpSettingsService(server, &ESPFS, &_securitySettingsService),
@@ -83,6 +81,7 @@ void ESP32SvelteKit::setupServer() {
_server->config.max_uri_handlers = _numberEndpoints;
_server->listen(80);
// wifi
_server->on("/api/wifi/scan", HTTP_GET, _wifiService.handleScan);
_server->on("/api/wifi/networks", HTTP_GET,
[this](PsychicRequest *request) { return _wifiService.getNetworks(request); });
@@ -94,6 +93,15 @@ void ESP32SvelteKit::setupServer() {
return _wifiService.endpoint.handleStateUpdate(request, json);
});
// ap
_server->on("/api/wifi/ap/status", HTTP_GET,
[this](PsychicRequest *request) { return _apService.getStatus(request); });
_server->on("/api/wifi/ap/settings", HTTP_GET,
[this](PsychicRequest *request) { return _apService.endpoint.getState(request); });
_server->on("/api/wifi/ap/settings", HTTP_POST, [this](PsychicRequest *request, JsonVariant &json) {
return _apService.endpoint.handleStateUpdate(request, json);
});
#ifdef EMBED_WWW
ESP_LOGV("ESP32SvelteKit", "Registering routes from PROGMEM static resources");
WWWData::registerRoutes([&](const String &uri, const String &contentType, const uint8_t *content, size_t len) {
@@ -155,9 +163,8 @@ void ESP32SvelteKit::setupMDNS() {
void ESP32SvelteKit::startServices() {
_wifiService.begin();
_apStatus.begin();
_apService.begin();
_socket.begin();
_apSettingsService.begin();
_factoryResetService.begin();
_featureService.begin();
_restartService.begin();
@@ -208,7 +215,7 @@ void IRAM_ATTR ESP32SvelteKit::loop() {
_ledService.loop();
#endif
_wifiService.loop();
_apSettingsService.loop();
_apService.loop();
#if FT_ENABLED(USE_ANALYTICS)
_analyticsService.loop();
#endif
+3 -10
View File
@@ -18,8 +18,6 @@
#include <Arduino.h>
#include <APSettingsService.h>
#include <APStatus.h>
#include <AnalyticsService.h>
#include <AuthenticationService.h>
#include <BatteryService.h>
@@ -47,6 +45,7 @@
#include <UploadFirmwareService.h>
#include <WiFi.h>
#include <wifi_service.h>
#include <ap_service.h>
#include <Wire.h>
#ifdef EMBED_WWW
@@ -83,8 +82,6 @@ class ESP32SvelteKit {
EventSocket *getSocket() { return &_socket; }
StatefulService<APSettings> *getAPSettingsService() { return &_apSettingsService; }
#if FT_ENABLED(USE_NTP)
StatefulService<NTPSettings> *getNTPSettingsService() { return &_ntpSettingsService; }
#endif
@@ -122,7 +119,7 @@ class ESP32SvelteKit {
void setMDNSAppName(String name) { _appName = name; }
void recoveryMode() { _apSettingsService.recoveryMode(); }
void recoveryMode() { _apService.recoveryMode(); }
void loop();
@@ -132,11 +129,7 @@ class ESP32SvelteKit {
FeaturesService _featureService;
SecuritySettingsService _securitySettingsService;
WiFiService _wifiService;
// WiFiSettingsService _wifiSettingsService;
// WiFiScanner _wifiScanner;
// WiFiStatus _wifiStatus;
APSettingsService _apSettingsService;
APStatus _apStatus;
APService _apService;
EventSocket _socket;
#if FT_ENABLED(USE_NTP)
NTPSettingsService _ntpSettingsService;
@@ -1,57 +1,59 @@
/**
* ESP32 SvelteKit
*
* A simple, secure and extensible framework for IoT projects for ESP32 platforms
* with responsive Sveltekit front-end built with TailwindCSS and DaisyUI.
* https://github.com/theelims/ESP32-sveltekit
*
* Copyright (C) 2018 - 2023 rjwats
* Copyright (C) 2023 theelims
*
* All Rights Reserved. This software may be modified and distributed under
* the terms of the LGPL v3 license. See the LICENSE file for details.
**/
#include <ap_service.h>
#include <APSettingsService.h>
static const char *TAG = "APService";
static const char *TAG = "APSettingsService";
APSettingsService::APSettingsService(PsychicHttpServer *server, FS *fs, SecurityManager *securityManager)
: _server(server),
_securityManager(securityManager),
_httpEndpoint(APSettings::read, APSettings::update, this, server, AP_SETTINGS_SERVICE_PATH, securityManager),
_fsPersistence(APSettings::read, APSettings::update, this, fs, AP_SETTINGS_FILE),
_dnsServer(nullptr),
_lastManaged(0),
_reconfigureAp(false) {
APService::APService()
: endpoint(APSettings::read, APSettings::update, this),
_fsPersistence(APSettings::read, APSettings::update, this, &ESPFS, NTP_SETTINGS_FILE) {
addUpdateHandler([&](const String &originId) { reconfigureAP(); }, false);
}
void APSettingsService::begin() {
_httpEndpoint.begin();
_fsPersistence.readFromFS();
reconfigureAP();
APService::~APService() {}
void APService::begin() { _fsPersistence.readFromFS(); }
esp_err_t APService::getStatus(PsychicRequest *request) {
PsychicJsonResponse response = PsychicJsonResponse(request, false);
JsonObject root = response.getRoot();
status(root);
return response.send();
}
void APSettingsService::reconfigureAP() {
void APService::status(JsonObject &root) {
root["status"] = getAPNetworkStatus();
root["ip_address"] = WiFi.softAPIP().toString();
root["mac_address"] = WiFi.softAPmacAddress();
root["station_num"] = WiFi.softAPgetStationNum();
}
APNetworkStatus APService::getAPNetworkStatus() {
WiFiMode_t currentWiFiMode = WiFi.getMode();
bool apActive = currentWiFiMode == WIFI_AP || currentWiFiMode == WIFI_AP_STA;
if (apActive && _state.provisionMode != AP_MODE_ALWAYS && WiFi.status() == WL_CONNECTED) {
return APNetworkStatus::LINGERING;
}
return apActive ? APNetworkStatus::ACTIVE : APNetworkStatus::INACTIVE;
}
void APService::reconfigureAP() {
_lastManaged = millis() - MANAGE_NETWORK_DELAY;
_reconfigureAp = true;
_recoveryMode = false;
}
void APSettingsService::recoveryMode() {
void APService::recoveryMode() {
ESP_LOGI(TAG, "Recovery Mode needed");
_lastManaged = millis() - MANAGE_NETWORK_DELAY;
_recoveryMode = true;
_reconfigureAp = true;
}
void APSettingsService::loop() {
void APService::loop() {
EXECUTE_EVERY_N_MS(MANAGE_NETWORK_DELAY, manageAP());
handleDNS();
}
void APSettingsService::manageAP() {
void APService::manageAP() {
WiFiMode_t currentWiFiMode = WiFi.getMode();
if (_state.provisionMode == AP_MODE_ALWAYS ||
(_state.provisionMode == AP_MODE_DISCONNECTED && WiFi.status() != WL_CONNECTED) || _recoveryMode) {
@@ -65,7 +67,7 @@ void APSettingsService::manageAP() {
_reconfigureAp = false;
}
void APSettingsService::startAP() {
void APService::startAP() {
ESP_LOGI(TAG, "Starting software access point: %s", _state.ssid.c_str());
WiFi.softAPConfig(_state.localIP, _state.gatewayIP, _state.subnetMask);
WiFi.softAP(_state.ssid.c_str(), _state.password.c_str(), _state.channel, _state.ssidHidden, _state.maxClients);
@@ -80,7 +82,7 @@ void APSettingsService::startAP() {
}
}
void APSettingsService::stopAP() {
void APService::stopAP() {
if (_dnsServer) {
ESP_LOGI(TAG, "Stopping captive portal");
_dnsServer->stop();
@@ -91,17 +93,8 @@ void APSettingsService::stopAP() {
WiFi.softAPdisconnect(true);
}
void APSettingsService::handleDNS() {
void APService::handleDNS() {
if (_dnsServer) {
_dnsServer->processNextRequest();
}
}
APNetworkStatus APSettingsService::getAPNetworkStatus() {
WiFiMode_t currentWiFiMode = WiFi.getMode();
bool apActive = currentWiFiMode == WIFI_AP || currentWiFiMode == WIFI_AP_STA;
if (apActive && _state.provisionMode != AP_MODE_ALWAYS && WiFi.status() == WL_CONNECTED) {
return APNetworkStatus::LINGERING;
}
return apActive ? APNetworkStatus::ACTIVE : APNetworkStatus::INACTIVE;
}
}
+38
View File
@@ -0,0 +1,38 @@
#include <StatefulService.h>
#include <stateful_service_endpoint.h>
#include <FSPersistence.h>
#include <ap_settings.h>
#include <timing.h>
#include <WiFi.h>
class APService : public StatefulService<APSettings> {
public:
APService();
~APService();
void begin();
void loop();
void recoveryMode();
esp_err_t getStatus(PsychicRequest *request);
void status(JsonObject &root);
APNetworkStatus getAPNetworkStatus();
StatefulHttpEndpoint<APSettings> endpoint;
private:
PsychicHttpServer *_server;
FSPersistence<APSettings> _fsPersistence;
DNSServer *_dnsServer;
volatile unsigned long _lastManaged;
volatile boolean _reconfigureAp;
volatile boolean _recoveryMode = false;
void reconfigureAP();
void manageAP();
void startAP();
void stopAP();
void handleDNS();
};
@@ -1,27 +1,12 @@
#ifndef APSettingsConfig_h
#define APSettingsConfig_h
#pragma once
/**
* ESP32 SvelteKit
*
* A simple, secure and extensible framework for IoT projects for ESP32 platforms
* with responsive Sveltekit front-end built with TailwindCSS and DaisyUI.
* https://github.com/theelims/ESP32-sveltekit
*
* Copyright (C) 2018 - 2023 rjwats
* Copyright (C) 2023 theelims
*
* All Rights Reserved. This software may be modified and distributed under
* the terms of the LGPL v3 license. See the LICENSE file for details.
**/
#include <SettingValue.h>
#include <HttpEndpoint.h>
#include <FSPersistence.h>
#include <JsonUtils.h>
#include <WiFi.h>
#include <Timing.h>
#include <ESPFS.h>
#include <IPAddress.h>
#include <ArduinoJson.h>
#include <JsonUtils.h>
#include <SettingValue.h>
#include <IPUtils.h>
#include <state_result.h>
#include <DNSServer.h>
#include <IPAddress.h>
@@ -129,36 +114,4 @@ class APSettings {
settings = newSettings;
return StateUpdateResult::CHANGED;
}
};
class APSettingsService : public StatefulService<APSettings> {
public:
APSettingsService(PsychicHttpServer *server, FS *fs, SecurityManager *securityManager);
void begin();
void loop();
APNetworkStatus getAPNetworkStatus();
void recoveryMode();
private:
PsychicHttpServer *_server;
SecurityManager *_securityManager;
HttpEndpoint<APSettings> _httpEndpoint;
FSPersistence<APSettings> _fsPersistence;
// for the captive portal
DNSServer *_dnsServer;
// for the mangement delay loop
volatile unsigned long _lastManaged;
volatile boolean _reconfigureAp;
volatile boolean _recoveryMode = false;
void reconfigureAP();
void manageAP();
void startAP();
void stopAP();
void handleDNS();
};
#endif // end APSettingsConfig_h
};