diff --git a/platformio.ini b/platformio.ini index 7e1c725..1d1b6a2 100644 --- a/platformio.ini +++ b/platformio.ini @@ -16,21 +16,21 @@ build_flags = -D CONFIG_ASYNC_TCP_RUNNING_CORE=1 ;tbd experiment without ;-D CONFIG_ASYNC_TCP_TASK_STACK_SIZE ; 8192*2 here as default !!! lib_deps = - https://github.com/MoonModules/ESPAsyncWebServer.git @ 3.2.2 ; + queueLength (see https://github.com/esphome/ESPAsyncWebServer/pull/38) - ; https://github.com/mathieucarbou/ESPAsyncWebServer.git @ 3.3.5 ; WIP... + https://github.com/MoonModules/ESPAsyncWebServer.git @ 3.2.2 ; + queueLen(gth) (see https://github.com/esphome/ESPAsyncWebServer/pull/38) -; ESPAsyncWebServer AirCoookie v2.0.7 version (2.2.1 is latest) +; ;MC version ; [ESPAsyncWebServer] ; build_flags = ; -D CONFIG_ASYNC_TCP_USE_WDT=0 ;why? tbd: experiment without -; ; -D WS_MAX_QUEUED_MESSAGES=64 ;not supported to set here in AirCoookie version -; -D CONFIG_ASYNC_TCP_STACK_SIZE=CONFIG_ASYNC_TCP_TASK_STACK_SIZE ; ESPHome uses CONFIG_ASYNC_TCP_STACK_SIZE (used in SysModSystem) +; -D WS_MAX_QUEUED_MESSAGES=64 +; -D CONFIG_ASYNC_TCP_RUNNING_CORE=1 ;tbd experiment without +; ;-D CONFIG_ASYNC_TCP_TASK_STACK_SIZE ; 8192*2 here as default !!! +; -D STARBASE_USE_MC_AWS ;mathieucarbou/ESPAsyncWebServer ; lib_deps = -; ; https://github.com/lost-hope/ESPAsyncWebServer.git#master ;aircoookie + artifx on /edit -; ; https://github.com/ewowi/ESPAsyncWebServer.git ;me-no-dev + 64 + queueLength - alternative -; https://github.com/pbolduc/AsyncTCP.git @ 1.2.0 ; WLEDMM this must be first in the list, otherwise Aircoookie/ESPAsyncWebServer pulls in an older version of AsyncTCP !! -; https://github.com/ewoudwijma/ESPAsyncWebServer.git#v2.0.7 ;aircoookie + getClients + 64 -; ; https://github.com/ewoudwijma/ESPAsyncWebServer.git#v2.2.1 ;crashes on ws refererence in sendDataWs !! +; https://github.com/mathieucarbou/ESPAsyncWebServer.git @ 3.3.10 ; WIP... + + + ; see also: ; https://github.com/esphome/ESPAsyncWebServer/issues/34 ; Decide which ESPAsyncWebServer fork should be canonical? diff --git a/src/App/LedModFixture.h b/src/App/LedModFixture.h index 60790ca..0918454 100644 --- a/src/App/LedModFixture.h +++ b/src/App/LedModFixture.h @@ -61,7 +61,11 @@ class LedModFixture:public SysModule { // ui->setComment(var, "Click to enlarge"); return true; case onLoop: { - var["interval"] = max(eff->fixture.nrOfLeds * web->ws.count()/200, 16U)*10; //interval in ms * 10, not too fast //from cs to ms + #ifdef STARBASE_USE_MC_AWS + var["interval"] = 25;// max(eff->fixture.nrOfLeds * ws.count()/200, 16U)*10; // error: reference to 'ws' is ambiguous !!! + #else + var["interval"] = max(eff->fixture.nrOfLeds * web->ws.count()/200, 16U)*10; //interval in ms * 10, not too fast //from cs to ms + #endif web->sendDataWs([this](AsyncWebSocketMessageBuffer * wsBuf) { byte* buffer; diff --git a/src/Sys/SysModSystem.cpp b/src/Sys/SysModSystem.cpp index f1f476f..72f5690 100644 --- a/src/Sys/SysModSystem.cpp +++ b/src/Sys/SysModSystem.cpp @@ -90,7 +90,11 @@ void SysModSystem::setup() { ui->initButton(parentVar, "reboot", false, [](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun case onChange: - web->ws.closeAll(1012); + #ifdef STARBASE_USE_MC_AWS + ws.closeAll(1012); + #else + web->ws.closeAll(1012); + #endif // mdls->reboot(); //not working yet // long dly = millis(); diff --git a/src/Sys/SysModWeb.cpp b/src/Sys/SysModWeb.cpp index 3534aef..99c559f 100644 --- a/src/Sys/SysModWeb.cpp +++ b/src/Sys/SysModWeb.cpp @@ -59,8 +59,15 @@ void SysModWeb::setup() { ui->initNumber(tableVar, "clNr", UINT16_MAX, 0, 999, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun case onSetValue: { - unsigned8 rowNr = 0; for (auto &client:ws.getClients()) + unsigned8 rowNr = 0; + for (auto &clientx:ws.getClients()) { + #ifdef STARBASE_USE_MC_AWS + AsyncWebSocketClient *client = &clientx; + #else + AsyncWebSocketClient *client = clientx; + #endif mdl->setValue(var, client->id(), rowNr++); + } return true; } case onUI: ui->setLabel(var, "Nr"); @@ -70,8 +77,15 @@ void SysModWeb::setup() { ui->initText(tableVar, "clIp", nullptr, 16, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun case onSetValue: { - unsigned8 rowNr = 0; for (auto &client:ws.getClients()) + unsigned8 rowNr = 0; for (auto &clientx:ws.getClients()) { + #ifdef STARBASE_USE_MC_AWS + AsyncWebSocketClient *client = &clientx; + #else + AsyncWebSocketClient *client = clientx; + #endif + mdl->setValue(var, JsonString(client->remoteIP().toString().c_str(), JsonString::Copied), rowNr++); + } return true; } case onUI: ui->setLabel(var, "IP"); @@ -81,8 +95,14 @@ void SysModWeb::setup() { ui->initCheckBox(tableVar, "clIsFull", UINT16_MAX, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun case onSetValue: { - unsigned8 rowNr = 0; for (auto &client:ws.getClients()) + unsigned8 rowNr = 0; for (auto &clientx:ws.getClients()) { + #ifdef STARBASE_USE_MC_AWS + AsyncWebSocketClient *client = &clientx; + #else + AsyncWebSocketClient *client = clientx; + #endif mdl->setValue(var, client->queueIsFull(), rowNr++); + } return true; } case onUI: ui->setLabel(var, "Is full"); @@ -92,8 +112,15 @@ void SysModWeb::setup() { ui->initSelect(tableVar, "clStatus", UINT16_MAX, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun case onSetValue: { - unsigned8 rowNr = 0; for (auto &client:ws.getClients()) + unsigned8 rowNr = 0; for (auto &clientx:ws.getClients()) { + #ifdef STARBASE_USE_MC_AWS + AsyncWebSocketClient *client = &clientx; + #else + AsyncWebSocketClient *client = clientx; + #endif + mdl->setValue(var, client->status(), rowNr++); + } return true; } case onUI: { @@ -110,8 +137,14 @@ void SysModWeb::setup() { ui->initNumber(tableVar, "clLength", UINT16_MAX, 0, WS_MAX_QUEUED_MESSAGES, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun case onSetValue: { - unsigned8 rowNr = 0; for (auto &client:ws.getClients()) + unsigned8 rowNr = 0; for (auto &clientx:ws.getClients()) { + #ifdef STARBASE_USE_MC_AWS + AsyncWebSocketClient *client = &clientx; + #else + AsyncWebSocketClient *client = clientx; + #endif mdl->setValue(var, client->queueLen(), rowNr++); + } return true; } case onUI: ui->setLabel(var, "Length"); @@ -424,11 +457,18 @@ void SysModWeb::sendDataWs(std::function fi AsyncWebSocketMessageBuffer * wsBuf = ws.makeBuffer(len); //assert failed: block_trim_free heap_tlsf.c:371 (block_is_free(block) && "block must be free"), AsyncWebSocket::makeBuffer(unsigned int) if (wsBuf) { - wsBuf->lock(); + #ifndef STARBASE_USE_MC_AWS + wsBuf->lock(); + #endif fill(wsBuf); //function parameter - for (auto &loopClient:ws.getClients()) { + for (auto &loopClientx:ws.getClients()) { + #ifdef STARBASE_USE_MC_AWS + AsyncWebSocketClient *loopClient = &loopClientx; + #else + AsyncWebSocketClient *loopClient = loopClientx; + #endif if (!client || client == loopClient) { if (loopClient->status() == WS_CONNECTED && !loopClient->queueIsFull()) { //WS_MAX_QUEUED_MESSAGES / ws.count() / 2)) { //binary is lossy if (!isBinary || loopClient->queueLen() <= 3) { @@ -441,21 +481,27 @@ void SysModWeb::sendDataWs(std::function fi } } else { - printClient("sendDataWs client full or not connected", loopClient); + printClient("sendDataWs client full or not connected", (AsyncWebSocketClient *)&loopClient); // ppf("sendDataWs client full or not connected\n"); ws.cleanupClients(); //only if above threshold - ws._cleanBuffers(); + #ifndef STARBASE_USE_MC_AWS + ws._cleanBuffers(); + #endif } } } - wsBuf->unlock(); - ws._cleanBuffers(); + #ifndef STARBASE_USE_MC_AWS + wsBuf->unlock(); + ws._cleanBuffers(); + #endif } else { ppf("sendDataWs WS buffer allocation failed\n"); ws.closeAll(1013); //code 1013 = temporary overload, try again later ws.cleanupClients(0); //disconnect ALL clients to release memory - ws._cleanBuffers(); + #ifndef STARBASE_USE_MC_AWS + ws._cleanBuffers(); + #endif } } @@ -472,7 +518,11 @@ void SysModWeb::serveIndex(WebRequest *request) { // if (handleIfNoneMatchCacheHeader(request)) return; WebResponse *response; - response = request->beginResponse_P(200, "text/html", PAGE_index, PAGE_index_L); + #ifdef STARBASE_USE_MC_AWS + response = request->beginResponse(200, "text/html", PAGE_index, PAGE_index_L); + #else + response = request->beginResponse_P(200, "text/html", PAGE_index, PAGE_index_L); + #endif response->addHeader("Content-Encoding","gzip"); // setStaticContentCacheHeaders(response); request->send(response); @@ -488,7 +538,12 @@ void SysModWeb::serveNewUI(WebRequest *request) { // if (handleIfNoneMatchCacheHeader(request)) return; WebResponse *response; - response = request->beginResponse_P(200, "text/html", PAGE_newui, PAGE_newui_L); + #ifdef STARBASE_USE_MC_AWS + response = request->beginResponse(200, "text/html", PAGE_newui, PAGE_newui_L); + #else + response = request->beginResponse_P(200, "text/html", PAGE_newui, PAGE_newui_L); + #endif + response->addHeader("Content-Encoding","gzip"); // setStaticContentCacheHeaders(response); request->send(response); @@ -625,7 +680,16 @@ void SysModWeb::jsonHandler(WebRequest *request, JsonVariant json) { } void SysModWeb::clientsToJson(JsonArray array, bool nameOnly, const char * filter) { - for (auto &client:ws.getClients()) { + for (auto &clientx:ws.getClients()) { + // NM: typedef LinkedList AsyncWebSocketClientLinkedList; LinkedList is a class + // MC: std::list& getClients() { return _clients; } + + #ifdef STARBASE_USE_MC_AWS + AsyncWebSocketClient *client = &clientx; + #else + AsyncWebSocketClient *client = clientx; + #endif + if (nameOnly) { array.add(JsonString(client->remoteIP().toString().c_str(), JsonString::Copied)); } else { diff --git a/src/Sys/SysModWeb.h b/src/Sys/SysModWeb.h index ed58c21..c6f1281 100644 --- a/src/Sys/SysModWeb.h +++ b/src/Sys/SysModWeb.h @@ -34,6 +34,12 @@ #define WebResponse AsyncWebServerResponse #endif +#ifdef STARBASE_USE_MC_AWS + static WebSocket ws("/ws"); + static WebServer server(80); + //static otherwise multiple definition of `server'; +#endif + class SysModWeb:public SysModule { public: @@ -41,8 +47,10 @@ class SysModWeb:public SysModule { WebSocket ws = WebSocket(); WebServer server = WebServer(); #else - WebSocket ws = WebSocket("/ws"); - WebServer server = WebServer(80); + #ifndef STARBASE_USE_MC_AWS + WebSocket ws = WebSocket("/ws"); + WebServer server = WebServer(80); + #endif #endif SemaphoreHandle_t wsMutex = xSemaphoreCreateMutex();