Skip to content

Commit

Permalink
🛜 Simplify wifi services
Browse files Browse the repository at this point in the history
  • Loading branch information
runeharlyk committed Sep 3, 2024
1 parent 9dbe31d commit e69e485
Show file tree
Hide file tree
Showing 15 changed files with 352 additions and 522 deletions.
26 changes: 15 additions & 11 deletions esp32/lib/ESP32-sveltekit/ESP32SvelteKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ ESP32SvelteKit::ESP32SvelteKit(PsychicHttpServer *server, unsigned int numberEnd
_taskManager(),
_featureService(server),
_securitySettingsService(server, &ESPFS),
_wifiSettingsService(server, &ESPFS, &_securitySettingsService, &_socket),
_wifiScanner(server, &_securitySettingsService),
_wifiStatus(server, &_securitySettingsService),
_apSettingsService(server, &ESPFS, &_securitySettingsService),
_apStatus(server, &_securitySettingsService, &_apSettingsService),
_socket(server, &_securitySettingsService, AuthenticationPredicates::IS_AUTHENTICATED),
Expand Down Expand Up @@ -72,14 +69,12 @@ void ESP32SvelteKit::begin() {
ESP_LOGI("Running Firmware Version: %s", APP_VERSION);
ESPFS.begin(true);

_wifiSettingsService.initWiFi();
startServices();

setupServer();

setupMDNS();

startServices();

ESP_LOGV("ESP32SvelteKit", "Starting loop task");
_taskManager.createTask(this->_loopImpl, "Spot main", 4096, this, 2, NULL, ESP32SVELTEKIT_RUNNING_CORE);
}
Expand All @@ -88,6 +83,17 @@ void ESP32SvelteKit::setupServer() {
_server->config.max_uri_handlers = _numberEndpoints;
_server->listen(80);

_server->on("/api/wifi/scan", HTTP_GET, _wifiService.handleScan);
_server->on("/api/wifi/networks", HTTP_GET,
[this](PsychicRequest *request) { return _wifiService.getNetworks(request); });
_server->on("/api/wifi/sta/status", HTTP_GET,
[this](PsychicRequest *request) { return _wifiService.getNetworkStatus(request); });
_server->on("/api/wifi/sta/settings", HTTP_GET,
[this](PsychicRequest *request) { return _wifiService.endpoint.getState(request); });
_server->on("/api/wifi/sta/settings", HTTP_POST, [this](PsychicRequest *request, JsonVariant &json) {
return _wifiService.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 @@ -140,24 +146,22 @@ void ESP32SvelteKit::setupServer() {

void ESP32SvelteKit::setupMDNS() {
ESP_LOGV("ESP32SvelteKit", "Starting MDNS");
MDNS.begin(_wifiSettingsService.getHostname().c_str());
MDNS.begin(_wifiService.getHostname());
MDNS.setInstanceName(_appName);
MDNS.addService("http", "tcp", 80);
MDNS.addService("ws", "tcp", 80);
MDNS.addServiceTxt("http", "tcp", "Firmware Version", APP_VERSION);
}

void ESP32SvelteKit::startServices() {
_wifiService.begin();
_apStatus.begin();
_socket.begin();
_apSettingsService.begin();
_factoryResetService.begin();
_featureService.begin();
_restartService.begin();
_systemStatus.begin();
_wifiSettingsService.begin();
_wifiScanner.begin();
_wifiStatus.begin();

#if FT_ENABLED(USE_UPLOAD_FIRMWARE)
_uploadFirmwareService.begin();
Expand Down Expand Up @@ -203,7 +207,7 @@ void IRAM_ATTR ESP32SvelteKit::loop() {
#if FT_ENABLED(USE_WS2812)
_ledService.loop();
#endif
_wifiSettingsService.loop();
_wifiService.loop();
_apSettingsService.loop();
#if FT_ENABLED(USE_ANALYTICS)
_analyticsService.loop();
Expand Down
13 changes: 5 additions & 8 deletions esp32/lib/ESP32-sveltekit/ESP32SvelteKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@
#include <TaskManager.h>
#include <UploadFirmwareService.h>
#include <WiFi.h>
#include <WiFiScanner.h>
#include <WiFiSettingsService.h>
#include <WiFiStatus.h>
#include <wifi_service.h>
#include <Wire.h>

#ifdef EMBED_WWW
Expand Down Expand Up @@ -85,8 +83,6 @@ class ESP32SvelteKit {

EventSocket *getSocket() { return &_socket; }

StatefulService<WiFiSettings> *getWiFiSettingsService() { return &_wifiSettingsService; }

StatefulService<APSettings> *getAPSettingsService() { return &_apSettingsService; }

#if FT_ENABLED(USE_NTP)
Expand Down Expand Up @@ -135,9 +131,10 @@ class ESP32SvelteKit {
unsigned int _numberEndpoints;
FeaturesService _featureService;
SecuritySettingsService _securitySettingsService;
WiFiSettingsService _wifiSettingsService;
WiFiScanner _wifiScanner;
WiFiStatus _wifiStatus;
WiFiService _wifiService;
// WiFiSettingsService _wifiSettingsService;
// WiFiScanner _wifiScanner;
// WiFiStatus _wifiStatus;
APSettingsService _apSettingsService;
APStatus _apStatus;
EventSocket _socket;
Expand Down
2 changes: 2 additions & 0 deletions esp32/lib/ESP32-sveltekit/FSPersistence.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
*
* Copyright (C) 2018 - 2023 rjwats
* Copyright (C) 2023 theelims
* Copyright (C) 2024 runeharlyk
*
* 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 <FS.h>
#include <StatefulService.h>
#include <ESPFS.h>

template <class T>
class FSPersistence {
Expand Down
18 changes: 0 additions & 18 deletions esp32/lib/ESP32-sveltekit/StatefulService.cpp

This file was deleted.

11 changes: 4 additions & 7 deletions esp32/lib/ESP32-sveltekit/StatefulService.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*
* Copyright (C) 2018 - 2023 rjwats
* Copyright (C) 2023 theelims
* Copyright (C) 2024 runeharlyk
*
* All Rights Reserved. This software may be modified and distributed under
* the terms of the LGPL v3 license. See the LICENSE file for details.
Expand All @@ -23,11 +24,7 @@
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>

enum class StateUpdateResult {
CHANGED = 0, // The update changed the state and propagation should take place if required
UNCHANGED, // The state was unchanged, propagation should not take place
ERROR // There was a problem updating the state, propagation should not take place
};
#include <state_result.h>

template <typename T>
using JsonStateUpdater = std::function<StateUpdateResult(JsonObject &root, T &settings)>;
Expand All @@ -41,7 +38,7 @@ typedef std::function<void(const String &originId)> StateUpdateCallback;
typedef std::function<void(const String &originId, StateUpdateResult &result)> StateHookCallback;

typedef struct StateUpdateHandlerInfo {
static update_handler_id_t currentUpdatedHandlerId;
static inline update_handler_id_t currentUpdatedHandlerId = 0;
update_handler_id_t _id;
StateUpdateCallback _cb;
bool _allowRemove;
Expand All @@ -50,7 +47,7 @@ typedef struct StateUpdateHandlerInfo {
} StateUpdateHandlerInfo_t;

typedef struct StateHookHandlerInfo {
static hook_handler_id_t currentHookHandlerId;
static inline hook_handler_id_t currentHookHandlerId = 0;
hook_handler_id_t _id;
StateHookCallback _cb;
bool _allowRemove;
Expand Down
63 changes: 0 additions & 63 deletions esp32/lib/ESP32-sveltekit/WiFiScanner.cpp

This file was deleted.

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

This file was deleted.

Loading

0 comments on commit e69e485

Please sign in to comment.