mirror of
https://github.com/rstrouse/ESPSomfy-RTS.git
synced 2026-06-02 18:22:12 +02:00
Migrate web server to ESPAsyncWebServer
This commit is contained in:
parent
eb75868adb
commit
3ecb8edb18
10 changed files with 515 additions and 297 deletions
2
.github/workflows/ci.yaml
vendored
2
.github/workflows/ci.yaml
vendored
|
|
@ -124,6 +124,8 @@ jobs:
|
|||
arduino-cli lib install PubSubClient@${{ env.PUB_SUB_CLIENT_VERSION }}
|
||||
arduino-cli lib install SmartRC-CC1101-Driver-Lib@${{ env.SMARTRC_CC1101_VERSION }}
|
||||
arduino-cli lib install WebSockets@${{ env.WEB_SOCKET_VERSION }}
|
||||
arduino-cli lib install "ESP Async WebServer"
|
||||
arduino-cli lib install AsyncTCP
|
||||
|
||||
- name: Build ${{ matrix.name }}
|
||||
run: |
|
||||
|
|
|
|||
2
.github/workflows/release.yaml
vendored
2
.github/workflows/release.yaml
vendored
|
|
@ -159,6 +159,8 @@ jobs:
|
|||
arduino-cli lib install PubSubClient@${{ env.PUB_SUB_CLIENT_VERSION }}
|
||||
arduino-cli lib install SmartRC-CC1101-Driver-Lib@${{ env.SMARTRC_CC1101_VERSION }}
|
||||
arduino-cli lib install WebSockets@${{ env.WEB_SOCKET_VERSION }}
|
||||
arduino-cli lib install "ESP Async WebServer"
|
||||
arduino-cli lib install AsyncTCP
|
||||
|
||||
- name: Build ${{ matrix.name }}
|
||||
run: |
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include <esp_task_wdt.h>
|
||||
#include "ConfigSettings.h"
|
||||
#include "Network.h"
|
||||
#include "Somfy.h"
|
||||
#include "Web.h"
|
||||
#include "Sockets.h"
|
||||
#include "Utils.h"
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
#include <Preferences.h>
|
||||
#include <ELECHOUSE_CC1101_SRC_DRV.h>
|
||||
#include <SPI.h>
|
||||
#include <WebServer.h>
|
||||
#include <esp_task_wdt.h>
|
||||
#include "Utils.h"
|
||||
#include "ConfigSettings.h"
|
||||
|
|
|
|||
13
WResp.cpp
13
WResp.cpp
|
|
@ -1,4 +1,5 @@
|
|||
#include "WResp.h"
|
||||
#include "Web.h"
|
||||
void JsonSockEvent::beginEvent(WebSocketsServer *server, const char *evt, char *buff, size_t buffSize) {
|
||||
this->server = server;
|
||||
this->buff = buff;
|
||||
|
|
@ -33,22 +34,22 @@ void JsonSockEvent::_safecat(const char *val, bool escape) {
|
|||
else strcat(this->buff, val);
|
||||
if(escape) strcat(this->buff, "\"");
|
||||
}
|
||||
void JsonResponse::beginResponse(WebServer *server, char *buff, size_t buffSize) {
|
||||
void JsonResponse::beginResponse(WebRequestCompat *server, char *buff, size_t buffSize) {
|
||||
this->server = server;
|
||||
this->buff = buff;
|
||||
this->buffSize = buffSize;
|
||||
this->buff[0] = 0x00;
|
||||
this->_nocomma = true;
|
||||
server->setContentLength(CONTENT_LENGTH_UNKNOWN);
|
||||
this->_headersSent = false;
|
||||
this->response = server->beginResponseStream("application/json");
|
||||
}
|
||||
void JsonResponse::endResponse() {
|
||||
if(strlen(buff)) this->send();
|
||||
server->sendContent("", 0);
|
||||
server->send(this->response);
|
||||
this->response = nullptr;
|
||||
}
|
||||
void JsonResponse::send() {
|
||||
if(!this->_headersSent) server->send_P(200, "application/json", this->buff);
|
||||
else server->sendContent(this->buff);
|
||||
//Serial.printf("Sent %d bytes %d\n", strlen(this->buff), this->buffSize);
|
||||
if(this->response != nullptr) this->response->print(this->buff);
|
||||
this->buff[0] = 0x00;
|
||||
this->_headersSent = true;
|
||||
}
|
||||
|
|
|
|||
10
WResp.h
10
WResp.h
|
|
@ -1,9 +1,10 @@
|
|||
#include <WebServer.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <WebSocketsServer.h>
|
||||
#include "Somfy.h"
|
||||
#ifndef wresp_h
|
||||
#define wresp_h
|
||||
|
||||
class WebRequestCompat;
|
||||
|
||||
class JsonFormatter {
|
||||
protected:
|
||||
char *buff;
|
||||
|
|
@ -56,8 +57,9 @@ class JsonResponse : public JsonFormatter {
|
|||
protected:
|
||||
void _safecat(const char *val, bool escape = false) override;
|
||||
public:
|
||||
WebServer *server;
|
||||
void beginResponse(WebServer *server, char *buff, size_t buffSize);
|
||||
WebRequestCompat *server;
|
||||
AsyncResponseStream *response = nullptr;
|
||||
void beginResponse(WebRequestCompat *server, char *buff, size_t buffSize);
|
||||
void endResponse();
|
||||
void send();
|
||||
};
|
||||
|
|
|
|||
121
Web.h
121
Web.h
|
|
@ -1,37 +1,87 @@
|
|||
#include <WebServer.h>
|
||||
#include "Somfy.h"
|
||||
#include <ArduinoJson.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <vector>
|
||||
#ifndef webserver_h
|
||||
#define webserver_h
|
||||
|
||||
using HTTPMethod = uint8_t;
|
||||
|
||||
struct AsyncRequestContext {
|
||||
String body;
|
||||
bool bodyEnabled = true;
|
||||
std::vector<std::pair<String, String>> headers;
|
||||
};
|
||||
|
||||
class WebClientCompat {
|
||||
public:
|
||||
explicit WebClientCompat(AsyncWebServerRequest *request = nullptr) : request(request) {}
|
||||
IPAddress remoteIP() const;
|
||||
void bind(AsyncWebServerRequest *request);
|
||||
private:
|
||||
AsyncWebServerRequest *request;
|
||||
};
|
||||
|
||||
class WebRequestCompat {
|
||||
public:
|
||||
explicit WebRequestCompat(AsyncWebServerRequest *request);
|
||||
HTTPMethod method() const;
|
||||
bool hasArg(const char *name) const;
|
||||
String arg(const char *name) const;
|
||||
bool hasHeader(const char *name) const;
|
||||
String header(const char *name) const;
|
||||
void sendHeader(const char *name, const char *value);
|
||||
void sendHeader(const char *name, const String &value);
|
||||
void sendHeader(const String &name, const String &value);
|
||||
void sendHeader(const __FlashStringHelper *name, const __FlashStringHelper *value);
|
||||
void sendHeader(const __FlashStringHelper *name, const char *value);
|
||||
void send(int code);
|
||||
void send(int code, const char *body);
|
||||
void send(int code, const char *contentType, const char *content);
|
||||
void send(int code, const char *contentType, const String &content);
|
||||
void send(AsyncWebServerResponse *response);
|
||||
AsyncResponseStream *beginResponseStream(const char *contentType);
|
||||
String uri() const;
|
||||
WebClientCompat &client();
|
||||
AsyncWebServerRequest *raw() const;
|
||||
private:
|
||||
AsyncRequestContext *ctx() const;
|
||||
void addPendingHeaders(AsyncWebServerResponse *response);
|
||||
void clearContext();
|
||||
|
||||
AsyncWebServerRequest *request;
|
||||
mutable WebClientCompat requestClient;
|
||||
};
|
||||
|
||||
class Web {
|
||||
public:
|
||||
bool uploadSuccess = false;
|
||||
void sendCORSHeaders(WebServer &server);
|
||||
void sendCacheHeaders(uint32_t seconds=604800);
|
||||
void sendCORSHeaders(WebRequestCompat &server);
|
||||
void sendCacheHeaders(WebRequestCompat &server, uint32_t seconds=604800);
|
||||
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 handleLogin(WebRequestCompat &server);
|
||||
void handleLogout(WebRequestCompat &server);
|
||||
void handleStreamFile(WebRequestCompat &server, const char *filename, const char *encoding);
|
||||
void handleController(WebRequestCompat &server);
|
||||
void handleLoginContext(WebRequestCompat &server);
|
||||
void handleGetRepeaters(WebRequestCompat &server);
|
||||
void handleGetRooms(WebRequestCompat &server);
|
||||
void handleGetShades(WebRequestCompat &server);
|
||||
void handleGetGroups(WebRequestCompat &server);
|
||||
void handleShadeCommand(WebRequestCompat &server);
|
||||
void handleRepeatCommand(WebRequestCompat &server);
|
||||
void handleGroupCommand(WebRequestCompat &server);
|
||||
void handleTiltCommand(WebRequestCompat &server);
|
||||
void handleDiscovery(WebRequestCompat &server);
|
||||
void handleNotFound(WebRequestCompat &server);
|
||||
void handleRoom(WebRequestCompat &server);
|
||||
void handleShade(WebRequestCompat &server);
|
||||
void handleGroup(WebRequestCompat &server);
|
||||
void handleSetPositions(WebRequestCompat &server);
|
||||
void handleSetSensor(WebRequestCompat &server);
|
||||
void handleDownloadFirmware(WebRequestCompat &server);
|
||||
void handleBackup(WebRequestCompat &server, bool attach = false);
|
||||
void handleReboot(WebRequestCompat &server);
|
||||
void handleDeserializationError(WebRequestCompat &server, DeserializationError &err);
|
||||
void begin();
|
||||
void loop();
|
||||
void end();
|
||||
|
|
@ -40,11 +90,16 @@ class Web {
|
|||
bool createAPIToken(const char *payload, 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 isAuthenticated(WebServer &server, bool cfg = false);
|
||||
bool isAuthenticated(WebRequestCompat &server, bool cfg = false);
|
||||
|
||||
//void chunkRoomsResponse(WebServer &server, const char *elem = nullptr);
|
||||
//void chunkShadesResponse(WebServer &server, const char *elem = nullptr);
|
||||
//void chunkGroupsResponse(WebServer &server, const char *elem = nullptr);
|
||||
//void chunkGroupResponse(WebServer &server, SomfyGroup *, const char *prefix = nullptr);
|
||||
static AsyncRequestContext *ensureRequestContext(AsyncWebServerRequest *request);
|
||||
static AsyncRequestContext *findRequestContext(AsyncWebServerRequest *request);
|
||||
static void releaseRequestContext(AsyncWebServerRequest *request);
|
||||
static void collectBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total);
|
||||
|
||||
//void chunkRoomsResponse(WebRequestCompat &server, const char *elem = nullptr);
|
||||
//void chunkShadesResponse(WebRequestCompat &server, const char *elem = nullptr);
|
||||
//void chunkGroupsResponse(WebRequestCompat &server, const char *elem = nullptr);
|
||||
//void chunkGroupResponse(WebRequestCompat &server, SomfyGroup *, const char *prefix = nullptr);
|
||||
};
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -8,9 +8,9 @@
|
|||
<meta name="apple-mobile-web-app-title" content="ESPSomfy RTS App">
|
||||
<meta name="apple-mobile-web-app-status-bar-style" content="black">
|
||||
|
||||
<link rel="stylesheet" href="main.css?v=2.4.7c" type="text/css" />
|
||||
<link rel="stylesheet" href="widgets.css?v=2.4.7c" type="text/css" />
|
||||
<link rel="stylesheet" href="icons.css?v=2.4.7c" type="text/css" />
|
||||
<link rel="stylesheet" href="main.css?v=2.4.7e" type="text/css" />
|
||||
<link rel="stylesheet" href="widgets.css?v=2.4.7e" type="text/css" />
|
||||
<link rel="stylesheet" href="icons.css?v=2.4.7e" type="text/css" />
|
||||
<link rel="icon" type="image/png" href="favicon.png" />
|
||||
|
||||
<!-- iPad retina icon -->
|
||||
|
|
@ -114,7 +114,7 @@
|
|||
rel="apple-touch-startup-image">
|
||||
|
||||
|
||||
<script type="text/javascript" src="index.js?v=2.4.7c"></script>
|
||||
<script type="text/javascript" src="index.js?v=2.4.7e"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="divContainer" class="container main" data-auth="false">
|
||||
|
|
|
|||
|
|
@ -719,6 +719,7 @@ class UIBinder {
|
|||
return val;
|
||||
}
|
||||
toElement(el, val) {
|
||||
if (!el) return;
|
||||
let flds = el.querySelectorAll('*[data-bind]');
|
||||
flds.forEach((fld) => {
|
||||
let prop = fld.getAttribute('data-bind');
|
||||
|
|
@ -1673,8 +1674,10 @@ class Wifi {
|
|||
ui.serviceError(err);
|
||||
}
|
||||
else {
|
||||
document.getElementById('cbHardwired').checked = settings.connType >= 2;
|
||||
document.getElementById('cbFallbackWireless').checked = settings.connType === 3;
|
||||
let cbHardwired = document.getElementById('cbHardwired');
|
||||
let cbFallbackWireless = document.getElementById('cbFallbackWireless');
|
||||
if (cbHardwired) cbHardwired.checked = settings.connType >= 2;
|
||||
if (cbFallbackWireless) cbFallbackWireless.checked = settings.connType === 3;
|
||||
ui.toElement(pnl, settings);
|
||||
/*
|
||||
if (settings.connType >= 2) {
|
||||
|
|
@ -1691,9 +1694,12 @@ class Wifi {
|
|||
}
|
||||
*/
|
||||
ui.toElement(document.getElementById('divDHCP'), settings);
|
||||
document.getElementById('divETHSettings').style.display = settings.ethernet.boardType === 0 ? '' : 'none';
|
||||
document.getElementById('divStaticIP').style.display = settings.ip.dhcp ? 'none' : '';
|
||||
document.getElementById('spanCurrentIP').innerHTML = settings.ip.ip;
|
||||
let divETHSettings = document.getElementById('divETHSettings');
|
||||
let divStaticIP = document.getElementById('divStaticIP');
|
||||
let spanCurrentIP = document.getElementById('spanCurrentIP');
|
||||
if (divETHSettings) divETHSettings.style.display = settings.ethernet.boardType === 0 ? '' : 'none';
|
||||
if (divStaticIP) divStaticIP.style.display = settings.ip.dhcp ? 'none' : '';
|
||||
if (spanCurrentIP) spanCurrentIP.innerHTML = settings.ip.ip;
|
||||
this.useEthernetClicked();
|
||||
this.hiddenSSIDClicked();
|
||||
}
|
||||
|
|
@ -4230,7 +4236,8 @@ class MQTT {
|
|||
else {
|
||||
console.log(settings);
|
||||
ui.toElement(document.getElementById('divMQTT'), { mqtt: settings });
|
||||
document.getElementById('divDiscoveryTopic').style.display = settings.pubDisco ? '' : 'none';
|
||||
let divDiscoveryTopic = document.getElementById('divDiscoveryTopic');
|
||||
if (divDiscoveryTopic) divDiscoveryTopic.style.display = settings.pubDisco ? '' : 'none';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -4800,4 +4807,3 @@ class Firmware {
|
|||
}
|
||||
}
|
||||
var firmware = new Firmware();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue