🐛 Handle spa
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
#include <nvs_flash.h>
|
#include <nvs_flash.h>
|
||||||
#include <freertos/FreeRTOS.h>
|
#include <freertos/FreeRTOS.h>
|
||||||
|
#include <freertos/task.h>
|
||||||
#include <freertos/event_groups.h>
|
#include <freertos/event_groups.h>
|
||||||
#include <utils/ip_address.h>
|
#include <utils/ip_address.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|||||||
@@ -3,3 +3,4 @@
|
|||||||
#include "WWWData.h"
|
#include "WWWData.h"
|
||||||
|
|
||||||
void mountStaticAssets(WebServer& s);
|
void mountStaticAssets(WebServer& s);
|
||||||
|
void mountSpaFallback(WebServer& s);
|
||||||
|
|||||||
+3
-3
@@ -42,7 +42,7 @@ WiFiService wifiService;
|
|||||||
APService apService;
|
APService apService;
|
||||||
|
|
||||||
void setupServer() {
|
void setupServer() {
|
||||||
server.config(50 + WWW_ASSETS_COUNT, 32768);
|
server.config(50 + WWW_ASSETS_COUNT, 16384);
|
||||||
server.listen(80);
|
server.listen(80);
|
||||||
|
|
||||||
server.on("/api/system/reset", HTTP_POST,
|
server.on("/api/system/reset", HTTP_POST,
|
||||||
@@ -107,8 +107,10 @@ void setupServer() {
|
|||||||
STAITC_PROTO_POST_ENDPOINT(server, "/api/files/delete", file_delete_request, FileSystem::handleDelete);
|
STAITC_PROTO_POST_ENDPOINT(server, "/api/files/delete", file_delete_request, FileSystem::handleDelete);
|
||||||
STAITC_PROTO_POST_ENDPOINT(server, "/api/files/edit", file_edit_request, FileSystem::handleEdit);
|
STAITC_PROTO_POST_ENDPOINT(server, "/api/files/edit", file_edit_request, FileSystem::handleEdit);
|
||||||
STAITC_PROTO_POST_ENDPOINT(server, "/api/files/mkdir", file_mkdir_request, FileSystem::mkdir);
|
STAITC_PROTO_POST_ENDPOINT(server, "/api/files/mkdir", file_mkdir_request, FileSystem::mkdir);
|
||||||
|
wsSocket.begin();
|
||||||
#if EMBED_WEBAPP
|
#if EMBED_WEBAPP
|
||||||
mountStaticAssets(server);
|
mountStaticAssets(server);
|
||||||
|
mountSpaFallback(server);
|
||||||
#endif
|
#endif
|
||||||
server.on("/*", HTTP_OPTIONS, [](httpd_req_t *request) {
|
server.on("/*", HTTP_OPTIONS, [](httpd_req_t *request) {
|
||||||
httpd_resp_set_status(request, "200 OK");
|
httpd_resp_set_status(request, "200 OK");
|
||||||
@@ -286,8 +288,6 @@ void IRAM_ATTR serviceLoopEntry(void *) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
setupServer();
|
setupServer();
|
||||||
|
|
||||||
wsSocket.begin();
|
|
||||||
setupEventSocket();
|
setupEventSocket();
|
||||||
|
|
||||||
ESP_LOGI("main", "Service task started");
|
ESP_LOGI("main", "Service task started");
|
||||||
|
|||||||
@@ -184,6 +184,7 @@ bool WiFiClass::begin(const char* ssid, const char* password, int32_t channel, c
|
|||||||
wifi_mode_t currentMode = getMode();
|
wifi_mode_t currentMode = getMode();
|
||||||
if (currentMode == WIFI_MODE_NULL || currentMode == WIFI_MODE_AP) {
|
if (currentMode == WIFI_MODE_NULL || currentMode == WIFI_MODE_AP) {
|
||||||
mode(currentMode == WIFI_MODE_AP ? WIFI_MODE_APSTA : WIFI_MODE_STA);
|
mode(currentMode == WIFI_MODE_AP ? WIFI_MODE_APSTA : WIFI_MODE_STA);
|
||||||
|
vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
wifi_config_t wifi_config = {};
|
wifi_config_t wifi_config = {};
|
||||||
@@ -370,6 +371,7 @@ int16_t WiFiClass::scanNetworks(bool async) {
|
|||||||
wifi_mode_t currentMode = getMode();
|
wifi_mode_t currentMode = getMode();
|
||||||
if (currentMode == WIFI_MODE_NULL || currentMode == WIFI_MODE_AP) {
|
if (currentMode == WIFI_MODE_NULL || currentMode == WIFI_MODE_AP) {
|
||||||
mode(currentMode == WIFI_MODE_AP ? WIFI_MODE_APSTA : WIFI_MODE_STA);
|
mode(currentMode == WIFI_MODE_AP ? WIFI_MODE_APSTA : WIFI_MODE_STA);
|
||||||
|
vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
_scanStatus = -1;
|
_scanStatus = -1;
|
||||||
|
|||||||
+15
-62
@@ -27,12 +27,6 @@ void WiFiService::begin() {
|
|||||||
|
|
||||||
_persistence.readFromFS();
|
_persistence.readFromFS();
|
||||||
_lastConnectionAttempt = 0;
|
_lastConnectionAttempt = 0;
|
||||||
|
|
||||||
if (state().wifi_networks_count >= 1) {
|
|
||||||
WiFi.mode(WIFI_MODE_STA);
|
|
||||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
||||||
configureNetwork(state().wifi_networks[0]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WiFiService::reconfigureWiFiConnection() {
|
void WiFiService::reconfigureWiFiConnection() {
|
||||||
@@ -127,65 +121,24 @@ esp_err_t WiFiService::getNetworkStatus(httpd_req_t *request) {
|
|||||||
|
|
||||||
void WiFiService::manageSTA() {
|
void WiFiService::manageSTA() {
|
||||||
if (WiFi.isConnected() || state().wifi_networks_count == 0) return;
|
if (WiFi.isConnected() || state().wifi_networks_count == 0) return;
|
||||||
if ((WiFi.getMode() & WIFI_MODE_STA) == 0) {
|
wifi_mode_t mode = WiFi.getMode();
|
||||||
WiFi.mode(WIFI_MODE_STA);
|
if (mode == WIFI_MODE_NULL || mode == WIFI_MODE_AP) return;
|
||||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
||||||
|
static uint32_t startTime = 0;
|
||||||
|
static bool attempted = false;
|
||||||
|
|
||||||
|
if (startTime == 0) {
|
||||||
|
startTime = esp_timer_get_time() / 1000;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
connectToWiFi();
|
|
||||||
}
|
|
||||||
|
|
||||||
void WiFiService::connectToWiFi() {
|
uint32_t now = esp_timer_get_time() / 1000;
|
||||||
int scanResult = WiFi.scanNetworks();
|
if (now - startTime < 3000) return;
|
||||||
if (scanResult < 0) {
|
|
||||||
ESP_LOGE(TAG, "WiFi scan failed.");
|
|
||||||
} else if (scanResult == 0) {
|
|
||||||
ESP_LOGI(TAG, "No networks found.");
|
|
||||||
} else {
|
|
||||||
ESP_LOGI(TAG, "%d networks found.", scanResult);
|
|
||||||
|
|
||||||
WiFiNetwork *bestNetwork = nullptr;
|
if (!attempted && state().wifi_networks_count > 0) {
|
||||||
int32_t bestNetworkDb = FACTORY_WIFI_RSSI_THRESHOLD;
|
attempted = true;
|
||||||
|
ESP_LOGI(TAG, "Connecting to: %s", state().wifi_networks[0].ssid);
|
||||||
for (int i = 0; i < scanResult; ++i) {
|
configureNetwork(state().wifi_networks[0]);
|
||||||
std::string ssid_scan;
|
|
||||||
int32_t rssi_scan;
|
|
||||||
uint8_t sec_scan;
|
|
||||||
uint8_t *BSSID_scan;
|
|
||||||
int32_t chan_scan;
|
|
||||||
|
|
||||||
WiFi.getNetworkInfo(i, ssid_scan, sec_scan, rssi_scan, BSSID_scan, chan_scan);
|
|
||||||
|
|
||||||
for (pb_size_t j = 0; j < state().wifi_networks_count; j++) {
|
|
||||||
WiFiNetwork &network = state().wifi_networks[j];
|
|
||||||
if (ssid_scan == network.ssid) {
|
|
||||||
if (rssi_scan > bestNetworkDb) {
|
|
||||||
bestNetworkDb = rssi_scan;
|
|
||||||
bestNetwork = &network;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!state().priority_rssi) {
|
|
||||||
for (pb_size_t j = 0; j < state().wifi_networks_count; j++) {
|
|
||||||
WiFiNetwork &network = state().wifi_networks[j];
|
|
||||||
for (int i = 0; i < scanResult; ++i) {
|
|
||||||
if (WiFi.SSID(i) == network.ssid) {
|
|
||||||
ESP_LOGI(TAG, "Connecting to first available network: %s", network.ssid);
|
|
||||||
configureNetwork(network);
|
|
||||||
WiFi.scanDelete();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (bestNetwork) {
|
|
||||||
ESP_LOGI(TAG, "Connecting to strongest network: %s", bestNetwork->ssid);
|
|
||||||
configureNetwork(*bestNetwork);
|
|
||||||
} else {
|
|
||||||
ESP_LOGI(TAG, "No known networks found.");
|
|
||||||
}
|
|
||||||
|
|
||||||
WiFi.scanDelete();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,14 @@
|
|||||||
#include "www_mount.hpp"
|
#include "www_mount.hpp"
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
static const WebAsset* findAsset(const char* uri) {
|
||||||
|
for (size_t i = 0; i < WWW_ASSETS_COUNT; i++) {
|
||||||
|
if (strcmp(WWW_ASSETS[i].uri, uri) == 0) {
|
||||||
|
return &WWW_ASSETS[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
static esp_err_t web_send(httpd_req_t* req, const WebAsset& asset) {
|
static esp_err_t web_send(httpd_req_t* req, const WebAsset& asset) {
|
||||||
httpd_resp_set_status(req, "200 OK");
|
httpd_resp_set_status(req, "200 OK");
|
||||||
@@ -23,3 +33,16 @@ void mountStaticAssets(WebServer& server) {
|
|||||||
server.on(a->uri, HTTP_GET, [a](httpd_req_t* req) { return web_send(req, *a); });
|
server.on(a->uri, HTTP_GET, [a](httpd_req_t* req) { return web_send(req, *a); });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void mountSpaFallback(WebServer& server) {
|
||||||
|
const WebAsset* indexAsset = findAsset(WWW_OPT.default_uri);
|
||||||
|
if (indexAsset) {
|
||||||
|
server.on("/*", HTTP_GET, [indexAsset](httpd_req_t* req) {
|
||||||
|
if (strncmp(req->uri, "/api/", 5) == 0) {
|
||||||
|
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "Not found");
|
||||||
|
return ESP_FAIL;
|
||||||
|
}
|
||||||
|
return web_send(req, *indexAsset);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user