Restuctures template

This commit is contained in:
Rune Harlyk
2024-04-22 21:32:50 +02:00
committed by Rune Harlyk
parent b804b9df1f
commit 9d6815cb05
4 changed files with 139 additions and 141 deletions
+4 -3
View File
@@ -3,6 +3,7 @@ build_flags =
-D BUILD_TARGET=\"$PIOENV\" -D BUILD_TARGET=\"$PIOENV\"
-D ESP32SVELTEKIT_RUNNING_CORE=0 -D ESP32SVELTEKIT_RUNNING_CORE=0
-D EMBED_WWW -D EMBED_WWW
;-D ENABLE_CORS -D SERVE_CONFIG_FILES
;-D SERIAL_INFO -D CORS_ORIGIN=\"*\"
;-D CORS_ORIGIN=\"*\" -D ENABLE_CORS
;-D SERIAL_INFO
+2 -2
View File
@@ -2,9 +2,9 @@
build_flags = build_flags =
-D FT_BATTERY=0 -D FT_BATTERY=0
-D FT_NTP=1 -D FT_NTP=1
-D FT_SECURITY=1 -D FT_SECURITY=0
-D FT_MQTT=1 -D FT_MQTT=1
-D FT_SLEEP=0 -D FT_SLEEP=1
-D FT_UPLOAD_FIRMWARE=1 -D FT_UPLOAD_FIRMWARE=1
-D FT_DOWNLOAD_FIRMWARE=1 -D FT_DOWNLOAD_FIRMWARE=1
-D FT_ANALYTICS=1 -D FT_ANALYTICS=1
+127 -134
View File
@@ -7,6 +7,7 @@
* *
* Copyright (C) 2018 - 2023 rjwats * Copyright (C) 2018 - 2023 rjwats
* Copyright (C) 2024 theelims * Copyright (C) 2024 theelims
* Copyright (C) 2024 runeharlyk
* *
* All Rights Reserved. This software may be modified and distributed under * All Rights Reserved. This software may be modified and distributed under
* the terms of the LGPL v3 license. See the LICENSE file for details. * the terms of the LGPL v3 license. See the LICENSE file for details.
@@ -14,191 +15,183 @@
#include <ESP32SvelteKit.h> #include <ESP32SvelteKit.h>
ESP32SvelteKit::ESP32SvelteKit(PsychicHttpServer *server, unsigned int numberEndpoints) : _server(server), ESP32SvelteKit::ESP32SvelteKit(PsychicHttpServer *server, unsigned int numberEndpoints)
_numberEndpoints(numberEndpoints), : _server(server), _numberEndpoints(numberEndpoints),
_featureService(server), _featureService(server), _securitySettingsService(server, &ESPFS),
_securitySettingsService(server, &ESPFS), _wifiSettingsService(server, &ESPFS, &_securitySettingsService, &_socket),
_wifiSettingsService(server, &ESPFS, &_securitySettingsService, &_socket), _wifiScanner(server, &_securitySettingsService),
_wifiScanner(server, &_securitySettingsService), _wifiStatus(server, &_securitySettingsService),
_wifiStatus(server, &_securitySettingsService), _apSettingsService(server, &ESPFS, &_securitySettingsService),
_apSettingsService(server, &ESPFS, &_securitySettingsService), _apStatus(server, &_securitySettingsService, &_apSettingsService),
_apStatus(server, &_securitySettingsService, &_apSettingsService), _socket(server, &_securitySettingsService, AuthenticationPredicates::IS_AUTHENTICATED),
_socket(server, &_securitySettingsService, AuthenticationPredicates::IS_AUTHENTICATED),
#if FT_ENABLED(FT_NTP) #if FT_ENABLED(FT_NTP)
_ntpSettingsService(server, &ESPFS, &_securitySettingsService), _ntpSettingsService(server, &ESPFS, &_securitySettingsService),
_ntpStatus(server, &_securitySettingsService), _ntpStatus(server, &_securitySettingsService),
#endif #endif
#if FT_ENABLED(FT_UPLOAD_FIRMWARE) #if FT_ENABLED(FT_UPLOAD_FIRMWARE)
_uploadFirmwareService(server, &_securitySettingsService), _uploadFirmwareService(server, &_securitySettingsService),
#endif #endif
#if FT_ENABLED(FT_DOWNLOAD_FIRMWARE) #if FT_ENABLED(FT_DOWNLOAD_FIRMWARE)
_downloadFirmwareService(server, &_securitySettingsService, &_socket), _downloadFirmwareService(server, &_securitySettingsService, &_socket),
#endif #endif
#if FT_ENABLED(FT_MQTT) #if FT_ENABLED(FT_MQTT)
_mqttSettingsService(server, &ESPFS, &_securitySettingsService), _mqttSettingsService(server, &ESPFS, &_securitySettingsService),
_mqttStatus(server, &_mqttSettingsService, &_securitySettingsService), _mqttStatus(server, &_mqttSettingsService, &_securitySettingsService),
#endif #endif
#if FT_ENABLED(FT_SECURITY) #if FT_ENABLED(FT_SECURITY)
_authenticationService(server, &_securitySettingsService), _authenticationService(server, &_securitySettingsService),
#endif #endif
#if FT_ENABLED(FT_SLEEP) #if FT_ENABLED(FT_SLEEP)
_sleepService(server, &_securitySettingsService), _sleepService(server, &_securitySettingsService),
#endif #endif
#if FT_ENABLED(FT_BATTERY) #if FT_ENABLED(FT_BATTERY)
_batteryService(&_socket), _batteryService(&_socket),
#endif #endif
#if FT_ENABLED(FT_ANALYTICS) #if FT_ENABLED(FT_ANALYTICS)
_analyticsService(&_socket), _analyticsService(&_socket),
#endif #endif
_restartService(server, &_securitySettingsService), _restartService(server, &_securitySettingsService),
_factoryResetService(server, &ESPFS, &_securitySettingsService), _factoryResetService(server, &ESPFS, &_securitySettingsService),
_systemStatus(server, &_securitySettingsService) _systemStatus(server, &_securitySettingsService) {
{
} }
void ESP32SvelteKit::begin() void ESP32SvelteKit::begin() {
{ ESP_LOGV("ESP32SvelteKit", "Loading settings from files system");
ESP_LOGV("ESP32SvelteKit", "Loading settings from files system"); ESP_LOGI("Running Firmware Version: %s\n", APP_VERSION);
ESPFS.begin(true); ESPFS.begin(true);
_wifiSettingsService.initWiFi(); _wifiSettingsService.initWiFi();
// SvelteKit uses a lot of handlers, so we need to increase the max_uri_handlers setupServer();
// WWWData has 77 Endpoints, Framework has 27, and Lighstate Demo has 4
_server->config.max_uri_handlers = _numberEndpoints; setupMDNS();
_server->listen(80);
startServices();
ESP_LOGV("ESP32SvelteKit", "Starting loop task");
xTaskCreatePinnedToCore(this->_loopImpl, "ESP32 SvelteKit Loop", 4096, this,
(tskIDLE_PRIORITY + 1), NULL,
ESP32SVELTEKIT_RUNNING_CORE);
}
void ESP32SvelteKit::setupServer() {
_server->config.max_uri_handlers = _numberEndpoints;
_server->listen(80);
#ifdef EMBED_WWW #ifdef EMBED_WWW
// Serve static resources from PROGMEM // Serve static resources from PROGMEM
ESP_LOGV("ESP32SvelteKit", "Registering routes from PROGMEM static resources"); ESP_LOGV("ESP32SvelteKit",
WWWData::registerRoutes( "Registering routes from PROGMEM static resources");
[&](const String &uri, const String &contentType, const uint8_t *content, size_t len) WWWData::registerRoutes([&](const String &uri, const String &contentType, const uint8_t *content, size_t len) {
{ PsychicHttpRequestCallback requestHandler = [contentType, content, len](PsychicRequest *request) {
PsychicHttpRequestCallback requestHandler = [contentType, content, len](PsychicRequest *request) PsychicResponse response(request);
{ response.setCode(200);
PsychicResponse response(request); response.setContentType(contentType.c_str());
response.setCode(200); response.addHeader("Content-Encoding", "gzip");
response.setContentType(contentType.c_str()); response.addHeader("Cache-Control", "public, immutable, max-age=31536000");
response.addHeader("Content-Encoding", "gzip"); response.setContent(content, len);
response.addHeader("Cache-Control", "public, immutable, max-age=31536000"); return response.send();
response.setContent(content, len); };
return response.send(); PsychicWebHandler *handler = new PsychicWebHandler();
}; handler->onRequest(requestHandler);
PsychicWebHandler *handler = new PsychicWebHandler(); _server->on(uri.c_str(), HTTP_GET, handler);
handler->onRequest(requestHandler);
_server->on(uri.c_str(), HTTP_GET, handler);
// Set default end-point for all non matching requests // Set default end-point for all non matching requests
// this is easier than using webServer.onNotFound() // this is easier than using webServer.onNotFound()
if (uri.equals("/index.html")) if (uri.equals("/index.html")) {
{ _server->defaultEndpoint->setHandler(handler);
_server->defaultEndpoint->setHandler(handler); }
} });
});
#else #else
// Serve static resources from /www/ // Serve static resources from /www/
ESP_LOGV("ESP32SvelteKit", "Registering routes from FS /www/ static resources"); ESP_LOGV("ESP32SvelteKit",
_server->serveStatic("/_app/", ESPFS, "/www/_app/"); "Registering routes from FS /www/ static resources");
_server->serveStatic("/favicon.png", ESPFS, "/www/favicon.png"); _server->serveStatic("/_app/", ESPFS, "/www/_app/");
// Serving all other get requests with "/www/index.htm" _server->serveStatic("/favicon.png", ESPFS, "/www/favicon.png");
_server->onNotFound([](PsychicRequest *request) // Serving all other get requests with "/www/index.htm"
{ _server->onNotFound([](PsychicRequest *request) {
if (request->method() == HTTP_GET) { if (request->method() == HTTP_GET) {
PsychicFileResponse response(request, ESPFS, "/www/index.html", "text/html"); PsychicFileResponse response(request, ESPFS, "/www/index.html",
return response.send(); "text/html");
// String url = "http://" + request->host() + "/index.html"; return response.send();
// request->redirect(url.c_str()); // String url = "http://" + request->host() + "/index.html";
} }); // request->redirect(url.c_str());
}
});
#endif #endif
#ifdef SERVE_CONFIG_FILES #ifdef SERVE_CONFIG_FILES
_server->serveStatic("/config/", ESPFS, "/config/"); _server->serveStatic("/config/", ESPFS, "/config/");
#endif
// Serve static resources from /config/ if set by platformio.ini
#if SERVE_CONFIG_FILES
_server->serveStatic("/config/", ESPFS, "/config/");
#endif #endif
#if defined(ENABLE_CORS) #if defined(ENABLE_CORS)
ESP_LOGV("ESP32SvelteKit", "Enabling CORS headers"); ESP_LOGV("ESP32SvelteKit", "Enabling CORS headers");
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", CORS_ORIGIN); DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin",
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Headers", "Accept, Content-Type, Authorization"); CORS_ORIGIN);
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Credentials", "true"); DefaultHeaders::Instance().addHeader("Access-Control-Allow-Headers",
"Accept, Content-Type, Authorization");
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Credentials",
"true");
#endif #endif
}
ESP_LOGV("ESP32SvelteKit", "Starting MDNS"); void ESP32SvelteKit::setupMDNS() {
MDNS.begin(_wifiSettingsService.getHostname().c_str()); ESP_LOGV("ESP32SvelteKit", "Starting MDNS");
MDNS.setInstanceName(_appName); MDNS.begin(_wifiSettingsService.getHostname().c_str());
MDNS.addService("http", "tcp", 80); MDNS.setInstanceName(_appName);
MDNS.addService("ws", "tcp", 80); MDNS.addService("http", "tcp", 80);
MDNS.addServiceTxt("http", "tcp", "Firmware Version", APP_VERSION); MDNS.addService("ws", "tcp", 80);
MDNS.addServiceTxt("http", "tcp", "Firmware Version", APP_VERSION);
}
#ifdef SERIAL_INFO void ESP32SvelteKit::startServices() {
Serial.printf("Running Firmware Version: %s\n", APP_VERSION); // Start the services
#endif _apStatus.begin();
_socket.begin();
// Start the services _apSettingsService.begin();
_apStatus.begin(); _factoryResetService.begin();
_socket.begin(); _featureService.begin();
_apSettingsService.begin(); _restartService.begin();
_factoryResetService.begin(); _systemStatus.begin();
_featureService.begin(); _wifiSettingsService.begin();
_restartService.begin(); _wifiScanner.begin();
_systemStatus.begin(); _wifiStatus.begin();
_wifiSettingsService.begin();
_wifiScanner.begin();
_wifiStatus.begin();
#if FT_ENABLED(FT_UPLOAD_FIRMWARE) #if FT_ENABLED(FT_UPLOAD_FIRMWARE)
_uploadFirmwareService.begin(); _uploadFirmwareService.begin();
#endif #endif
#if FT_ENABLED(FT_DOWNLOAD_FIRMWARE) #if FT_ENABLED(FT_DOWNLOAD_FIRMWARE)
_downloadFirmwareService.begin(); _downloadFirmwareService.begin();
#endif #endif
#if FT_ENABLED(FT_NTP) #if FT_ENABLED(FT_NTP)
_ntpSettingsService.begin(); _ntpSettingsService.begin();
_ntpStatus.begin(); _ntpStatus.begin();
#endif #endif
#if FT_ENABLED(FT_MQTT) #if FT_ENABLED(FT_MQTT)
_mqttSettingsService.begin(); _mqttSettingsService.begin();
_mqttStatus.begin(); _mqttStatus.begin();
#endif #endif
#if FT_ENABLED(FT_SECURITY) #if FT_ENABLED(FT_SECURITY)
_authenticationService.begin(); _authenticationService.begin();
_securitySettingsService.begin(); _securitySettingsService.begin();
#endif #endif
#if FT_ENABLED(FT_ANALYTICS) #if FT_ENABLED(FT_ANALYTICS)
_analyticsService.begin(); _analyticsService.begin();
#endif #endif
#if FT_ENABLED(FT_SLEEP) #if FT_ENABLED(FT_SLEEP)
_sleepService.begin(); _sleepService.begin();
#endif #endif
#if FT_ENABLED(FT_BATTERY) #if FT_ENABLED(FT_BATTERY)
_batteryService.begin(); _batteryService.begin();
#endif #endif
// Start the loop task
ESP_LOGV("ESP32SvelteKit", "Starting loop task");
xTaskCreatePinnedToCore(
this->_loopImpl, // Function that should be called
"ESP32 SvelteKit Loop", // Name of the task (for debugging)
4096, // Stack size (bytes)
this, // Pass reference to this class instance
(tskIDLE_PRIORITY + 1), // task priority
NULL, // Task handle
ESP32SVELTEKIT_RUNNING_CORE // Pin to application core
);
} }
void ESP32SvelteKit::_loop() void ESP32SvelteKit::_loop() {
{ while (1) {
while (1) _wifiSettingsService.loop(); // 30 seconds
{ _apSettingsService.loop(); // 10 seconds
_wifiSettingsService.loop(); // 30 seconds
_apSettingsService.loop(); // 10 seconds
#if FT_ENABLED(FT_MQTT) #if FT_ENABLED(FT_MQTT)
_mqttSettingsService.loop(); // 5 seconds _mqttSettingsService.loop(); // 5 seconds
#endif #endif
vTaskDelay(20 / portTICK_PERIOD_MS); vTaskDelay(20 / portTICK_PERIOD_MS);
} }
} }
+6 -2
View File
@@ -10,6 +10,7 @@
* *
* Copyright (C) 2018 - 2023 rjwats * Copyright (C) 2018 - 2023 rjwats
* Copyright (C) 2023 theelims * Copyright (C) 2023 theelims
* Copyright (C) 2024 runeharlyk
* *
* All Rights Reserved. This software may be modified and distributed under * All Rights Reserved. This software may be modified and distributed under
* the terms of the LGPL v3 license. See the LICENSE file for details. * the terms of the LGPL v3 license. See the LICENSE file for details.
@@ -52,11 +53,11 @@
#endif #endif
#ifndef APP_VERSION #ifndef APP_VERSION
#define APP_VERSION "demo" #define APP_VERSION "v1"
#endif #endif
#ifndef APP_NAME #ifndef APP_NAME
#define APP_NAME "ESP32 SvelteKit Demo" #define APP_NAME "SpotMicro"
#endif #endif
#ifndef ESP32SVELTEKIT_RUNNING_CORE #ifndef ESP32SVELTEKIT_RUNNING_CORE
@@ -206,6 +207,9 @@ private:
protected: protected:
static void _loopImpl(void *_this) { static_cast<ESP32SvelteKit *>(_this)->_loop(); } static void _loopImpl(void *_this) { static_cast<ESP32SvelteKit *>(_this)->_loop(); }
void _loop(); void _loop();
void setupServer();
void setupMDNS();
void startServices();
}; };
#endif #endif