Skip to content

Commit

Permalink
🛜 Simplifies ap service
Browse files Browse the repository at this point in the history
  • Loading branch information
runeharlyk committed Sep 3, 2024
1 parent b7799b4 commit bfddd26
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 193 deletions.
37 changes: 0 additions & 37 deletions esp32/lib/ESP32-sveltekit/APStatus.cpp

This file was deleted.

41 changes: 0 additions & 41 deletions esp32/lib/ESP32-sveltekit/APStatus.h

This file was deleted.

17 changes: 12 additions & 5 deletions esp32/lib/ESP32-sveltekit/ESP32SvelteKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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); });
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
13 changes: 3 additions & 10 deletions esp32/lib/ESP32-sveltekit/ESP32SvelteKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

#include <Arduino.h>

#include <APSettingsService.h>
#include <APStatus.h>
#include <AnalyticsService.h>
#include <AuthenticationService.h>
#include <BatteryService.h>
Expand Down Expand Up @@ -47,6 +45,7 @@
#include <UploadFirmwareService.h>
#include <WiFi.h>
#include <wifi_service.h>
#include <ap_service.h>
#include <Wire.h>

#ifdef EMBED_WWW
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -122,7 +119,7 @@ class ESP32SvelteKit {

void setMDNSAppName(String name) { _appName = name; }

void recoveryMode() { _apSettingsService.recoveryMode(); }
void recoveryMode() { _apService.recoveryMode(); }

void loop();

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 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) {
Expand All @@ -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);
Expand All @@ -80,7 +82,7 @@ void APSettingsService::startAP() {
}
}

void APSettingsService::stopAP() {
void APService::stopAP() {
if (_dnsServer) {
ESP_LOGI(TAG, "Stopping captive portal");
_dnsServer->stop();
Expand All @@ -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 changes: 38 additions & 0 deletions esp32/lib/ESP32-sveltekit/ap_service.h
Original file line number Diff line number Diff line change
@@ -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();
};
Loading

0 comments on commit bfddd26

Please sign in to comment.