From bfddd262f287e60e0c183d7db8fb8a161b430208 Mon Sep 17 00:00:00 2001 From: Rune Harlyk Date: Tue, 3 Sep 2024 21:16:40 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=9C=20Simplifies=20ap=20service?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- esp32/lib/ESP32-sveltekit/APStatus.cpp | 37 --------- esp32/lib/ESP32-sveltekit/APStatus.h | 41 --------- esp32/lib/ESP32-sveltekit/ESP32SvelteKit.cpp | 17 ++-- esp32/lib/ESP32-sveltekit/ESP32SvelteKit.h | 13 +-- .../{APSettingsService.cpp => ap_service.cpp} | 83 +++++++++---------- esp32/lib/ESP32-sveltekit/ap_service.h | 38 +++++++++ .../{APSettingsService.h => ap_settings.h} | 63 ++------------ 7 files changed, 99 insertions(+), 193 deletions(-) delete mode 100644 esp32/lib/ESP32-sveltekit/APStatus.cpp delete mode 100644 esp32/lib/ESP32-sveltekit/APStatus.h rename esp32/lib/ESP32-sveltekit/{APSettingsService.cpp => ap_service.cpp} (62%) create mode 100644 esp32/lib/ESP32-sveltekit/ap_service.h rename esp32/lib/ESP32-sveltekit/{APSettingsService.h => ap_settings.h} (71%) diff --git a/esp32/lib/ESP32-sveltekit/APStatus.cpp b/esp32/lib/ESP32-sveltekit/APStatus.cpp deleted file mode 100644 index abbaed15..00000000 --- a/esp32/lib/ESP32-sveltekit/APStatus.cpp +++ /dev/null @@ -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::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(); -} diff --git a/esp32/lib/ESP32-sveltekit/APStatus.h b/esp32/lib/ESP32-sveltekit/APStatus.h deleted file mode 100644 index d2d23b80..00000000 --- a/esp32/lib/ESP32-sveltekit/APStatus.h +++ /dev/null @@ -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 - -#include -#include -#include -#include -#include - -#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 diff --git a/esp32/lib/ESP32-sveltekit/ESP32SvelteKit.cpp b/esp32/lib/ESP32-sveltekit/ESP32SvelteKit.cpp index 49644a1b..0c05c3b9 100644 --- a/esp32/lib/ESP32-sveltekit/ESP32SvelteKit.cpp +++ b/esp32/lib/ESP32-sveltekit/ESP32SvelteKit.cpp @@ -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 diff --git a/esp32/lib/ESP32-sveltekit/ESP32SvelteKit.h b/esp32/lib/ESP32-sveltekit/ESP32SvelteKit.h index b424ffa2..4b3793f6 100644 --- a/esp32/lib/ESP32-sveltekit/ESP32SvelteKit.h +++ b/esp32/lib/ESP32-sveltekit/ESP32SvelteKit.h @@ -18,8 +18,6 @@ #include -#include -#include #include #include #include @@ -47,6 +45,7 @@ #include #include #include +#include #include #ifdef EMBED_WWW @@ -83,8 +82,6 @@ class ESP32SvelteKit { EventSocket *getSocket() { return &_socket; } - StatefulService *getAPSettingsService() { return &_apSettingsService; } - #if FT_ENABLED(USE_NTP) StatefulService *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; diff --git a/esp32/lib/ESP32-sveltekit/APSettingsService.cpp b/esp32/lib/ESP32-sveltekit/ap_service.cpp similarity index 62% rename from esp32/lib/ESP32-sveltekit/APSettingsService.cpp rename to esp32/lib/ESP32-sveltekit/ap_service.cpp index 0fb51c37..03d05600 100644 --- a/esp32/lib/ESP32-sveltekit/APSettingsService.cpp +++ b/esp32/lib/ESP32-sveltekit/ap_service.cpp @@ -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 -#include +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 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 APSettingsService::reconfigureAP() { +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; -} +} \ No newline at end of file diff --git a/esp32/lib/ESP32-sveltekit/ap_service.h b/esp32/lib/ESP32-sveltekit/ap_service.h new file mode 100644 index 00000000..72186a00 --- /dev/null +++ b/esp32/lib/ESP32-sveltekit/ap_service.h @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include + +class APService : public StatefulService { + public: + APService(); + ~APService(); + + void begin(); + void loop(); + void recoveryMode(); + + esp_err_t getStatus(PsychicRequest *request); + void status(JsonObject &root); + APNetworkStatus getAPNetworkStatus(); + + StatefulHttpEndpoint endpoint; + + private: + PsychicHttpServer *_server; + FSPersistence _fsPersistence; + + DNSServer *_dnsServer; + + volatile unsigned long _lastManaged; + volatile boolean _reconfigureAp; + volatile boolean _recoveryMode = false; + + void reconfigureAP(); + void manageAP(); + void startAP(); + void stopAP(); + void handleDNS(); +}; \ No newline at end of file diff --git a/esp32/lib/ESP32-sveltekit/APSettingsService.h b/esp32/lib/ESP32-sveltekit/ap_settings.h similarity index 71% rename from esp32/lib/ESP32-sveltekit/APSettingsService.h rename to esp32/lib/ESP32-sveltekit/ap_settings.h index 81f6042a..ccf848a1 100644 --- a/esp32/lib/ESP32-sveltekit/APSettingsService.h +++ b/esp32/lib/ESP32-sveltekit/ap_settings.h @@ -1,27 +1,12 @@ -#ifndef APSettingsConfig_h -#define APSettingsConfig_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. - **/ +#pragma once -#include -#include -#include -#include #include -#include -#include +#include +#include +#include +#include +#include +#include #include #include @@ -129,36 +114,4 @@ class APSettings { settings = newSettings; return StateUpdateResult::CHANGED; } -}; - -class APSettingsService : public StatefulService { - public: - APSettingsService(PsychicHttpServer *server, FS *fs, SecurityManager *securityManager); - - void begin(); - void loop(); - APNetworkStatus getAPNetworkStatus(); - void recoveryMode(); - - private: - PsychicHttpServer *_server; - SecurityManager *_securityManager; - HttpEndpoint _httpEndpoint; - FSPersistence _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 +}; \ No newline at end of file