update to async web server

This commit is contained in:
cjkas 2026-03-24 08:02:10 +01:00
parent b8721947e1
commit 5c185794a4
4 changed files with 1089 additions and 2378 deletions

View file

@ -1,4 +1,6 @@
#include "WResp.h" #include "WResp.h"
#include <WebServer.h>
#include <esp_task_wdt.h>
void JsonSockEvent::beginEvent(AsyncWebSocket *server, const char *evt, char *buff, size_t buffSize) { void JsonSockEvent::beginEvent(AsyncWebSocket *server, const char *evt, char *buff, size_t buffSize) {
this->server = server; this->server = server;
this->buff = buff; this->buff = buff;
@ -69,6 +71,7 @@ void JsonResponse::_safecat(const char *val, bool escape) {
} }
void AsyncJsonResp::beginResponse(AsyncWebServerRequest *request, char *buff, size_t buffSize) { void AsyncJsonResp::beginResponse(AsyncWebServerRequest *request, char *buff, size_t buffSize) {
this->_request = request;
this->buff = buff; this->buff = buff;
this->buffSize = buffSize; this->buffSize = buffSize;
this->buff[0] = 0x00; this->buff[0] = 0x00;
@ -78,6 +81,9 @@ void AsyncJsonResp::beginResponse(AsyncWebServerRequest *request, char *buff, si
} }
void AsyncJsonResp::endResponse() { void AsyncJsonResp::endResponse() {
if(strlen(this->buff)) this->flush(); if(strlen(this->buff)) this->flush();
if(this->_request && this->_stream) {
this->_request->send(this->_stream);
}
} }
void AsyncJsonResp::flush() { void AsyncJsonResp::flush() {
if(this->_stream && strlen(this->buff) > 0) { if(this->_stream && strlen(this->buff) > 0) {

View file

@ -1,10 +1,10 @@
#include <WebServer.h>
#include <esp_task_wdt.h>
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include "Somfy.h" #include "Somfy.h"
#ifndef wresp_h #ifndef wresp_h
#define wresp_h #define wresp_h
class WebServer;
class JsonFormatter { class JsonFormatter {
protected: protected:
char *buff; char *buff;
@ -66,6 +66,7 @@ class JsonResponse : public JsonFormatter {
class AsyncJsonResp : public JsonFormatter { class AsyncJsonResp : public JsonFormatter {
protected: protected:
void _safecat(const char *val, bool escape = false) override; void _safecat(const char *val, bool escape = false) override;
AsyncWebServerRequest *_request = nullptr;
AsyncResponseStream *_stream = nullptr; AsyncResponseStream *_stream = nullptr;
public: public:
void beginResponse(AsyncWebServerRequest *request, char *buff, size_t buffSize); void beginResponse(AsyncWebServerRequest *request, char *buff, size_t buffSize);

File diff suppressed because it is too large Load diff

View file

@ -1,4 +1,3 @@
#include <WebServer.h>
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <AsyncJson.h> #include <AsyncJson.h>
#include "Somfy.h" #include "Somfy.h"
@ -7,45 +6,18 @@
class Web { class Web {
public: public:
bool uploadSuccess = false; bool uploadSuccess = false;
void sendCORSHeaders(WebServer &server);
void sendCacheHeaders(uint32_t seconds=604800);
void startup(); void startup();
void handleLogin(WebServer &server);
void handleLogout(WebServer &server);
void handleStreamFile(WebServer &server, const char *filename, const char *encoding);
void handleController(WebServer &server);
void handleLoginContext(WebServer &server);
void handleGetRepeaters(WebServer &server);
void handleGetRooms(WebServer &server);
void handleGetShades(WebServer &server);
void handleGetGroups(WebServer &server);
void handleShadeCommand(WebServer &server);
void handleRepeatCommand(WebServer &server);
void handleGroupCommand(WebServer &server);
void handleTiltCommand(WebServer &server);
void handleDiscovery(WebServer &server);
void handleNotFound(WebServer &server);
void handleRoom(WebServer &server);
void handleShade(WebServer &server);
void handleGroup(WebServer &server);
void handleSetPositions(WebServer &server);
void handleSetSensor(WebServer &server);
void handleDownloadFirmware(WebServer &server);
void handleBackup(WebServer &server, bool attach = false);
void handleReboot(WebServer &server);
void handleDeserializationError(WebServer &server, DeserializationError &err);
void begin(); void begin();
void loop(); void loop();
void end(); void end();
// Web Handlers // Auth helpers
bool createAPIToken(const IPAddress ipAddress, char *token); bool createAPIToken(const IPAddress ipAddress, char *token);
bool createAPIToken(const char *payload, char *token); bool createAPIToken(const char *payload, char *token);
bool createAPIPinToken(const IPAddress ipAddress, const char *pin, char *token); bool createAPIPinToken(const IPAddress ipAddress, const char *pin, char *token);
bool createAPIPasswordToken(const IPAddress ipAddress, const char *username, const char *password, char *token); bool createAPIPasswordToken(const IPAddress ipAddress, const char *username, const char *password, char *token);
bool isAuthenticated(WebServer &server, bool cfg = false);
bool isAuthenticated(AsyncWebServerRequest *request, bool cfg = false); bool isAuthenticated(AsyncWebServerRequest *request, bool cfg = false);
// Async API handler overloads (port 8081) // Async API handlers
void handleDiscovery(AsyncWebServerRequest *request); void handleDiscovery(AsyncWebServerRequest *request);
void handleGetRooms(AsyncWebServerRequest *request); void handleGetRooms(AsyncWebServerRequest *request);
void handleGetShades(AsyncWebServerRequest *request); void handleGetShades(AsyncWebServerRequest *request);