From 65cbd0035683f391993ccae7ac394f401b7aced7 Mon Sep 17 00:00:00 2001 From: Ewoud Date: Tue, 9 Apr 2024 11:21:53 +0200 Subject: [PATCH] empty mdlTbl if nr of modules have been changed + small changes SysModUI: remove ui-> SysModWeb: remove web-> SysModules: empty mdlTbl if nr of modules have been changed (back to defaults) UserModE131: default disabled UserModMDNS: only reset if connected --- src/Sys/SysModModel.h | 4 ++-- src/Sys/SysModNetwork.cpp | 2 +- src/Sys/SysModUI.cpp | 4 ++-- src/Sys/SysModUI.h | 2 +- src/Sys/SysModWeb.cpp | 24 ++++++++++++------------ src/SysModules.cpp | 11 ++++++++++- src/User/UserModE131.h | 1 + src/User/UserModMDNS.h | 2 ++ 8 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/Sys/SysModModel.h b/src/Sys/SysModModel.h index 3c92b90f..e99b9e9e 100644 --- a/src/Sys/SysModModel.h +++ b/src/Sys/SysModModel.h @@ -307,10 +307,10 @@ class SysModModel:public SysModule { if (rowNr != UINT8_MAX && rowNr < valueArray.size()) return valueArray[rowNr]; else if (valueArray.size()) - return valueArray[0]; + return valueArray[0]; //return the first element else { USER_PRINTF("dev getValue no array or rownr wrong %s %s %d\n", varID(var), var["value"].as().c_str(), rowNr); - return JsonVariant(); + return JsonVariant(); // return null } } else diff --git a/src/Sys/SysModNetwork.cpp b/src/Sys/SysModNetwork.cpp index 7682838c..a12eb4b3 100644 --- a/src/Sys/SysModNetwork.cpp +++ b/src/Sys/SysModNetwork.cpp @@ -137,7 +137,7 @@ void SysModNetwork::initConnection() { else USER_PRINTF("No SSID"); - isConfirmedConnection = false; //neet to test if really connected in handleConnection + isConfirmedConnection = false; //need to test if really connected in handleConnection } void SysModNetwork::initAP() { diff --git a/src/Sys/SysModUI.cpp b/src/Sys/SysModUI.cpp index 720aa8ec..2d440e45 100644 --- a/src/Sys/SysModUI.cpp +++ b/src/Sys/SysModUI.cpp @@ -70,7 +70,7 @@ void SysModUI::loop() { void SysModUI::loop1s() { //if something changed in vloops - ui->callVarFun("vlLoopps", UINT8_MAX, f_ValueFun); + callVarFun("vlLoopps", UINT8_MAX, f_ValueFun); for (VarLoop &varLoop : loopFunctions) varLoop.counter = 0; } @@ -229,7 +229,7 @@ void SysModUI::processJson(JsonVariant json) { { //a button never sets the value if (var["type"] == "button") { //button always - ui->callVarFun(var, rowNr, f_ChangeFun); + callVarFun(var, rowNr, f_ChangeFun); if (rowNr != UINT8_MAX) web->getResponseObject()[mdl->varID(var)]["rowNr"] = rowNr; } else { diff --git a/src/Sys/SysModUI.h b/src/Sys/SysModUI.h index afdd678f..3d4e8edb 100644 --- a/src/Sys/SysModUI.h +++ b/src/Sys/SysModUI.h @@ -238,7 +238,7 @@ class SysModUI: public SysModule { return result; } - // assuming ui->callVarFun(varID, UINT8_MAX, f_UIFun); has been called before + // assuming callVarFun(varID, UINT8_MAX, f_UIFun); has been called before uint8_t selectOptionToValue(const char *varID, const char *label) { JsonArray options = web->getResponseObject()[varID]["options"]; // USER_PRINTF("selectOptionToValue fileName %s %s\n", label, options[0].as().c_str()); diff --git a/src/Sys/SysModWeb.cpp b/src/Sys/SysModWeb.cpp index 0d1b941a..f255ee0f 100644 --- a/src/Sys/SysModWeb.cpp +++ b/src/Sys/SysModWeb.cpp @@ -289,8 +289,8 @@ void SysModWeb::wsEvent(WebSocket * ws, WebClient * client, AwsEventType type, v USER_PRINT_Async("pong\n"); client->text("pong"); } else { - JsonDocument *responseDoc = web->getResponseDoc(); //we need the doc for deserializeJson - JsonObject responseObject = web->getResponseObject(); + JsonDocument *responseDoc = getResponseDoc(); //we need the doc for deserializeJson + JsonObject responseObject = getResponseObject(); DeserializationError error = deserializeJson(*responseDoc, data, len); //data to responseDoc @@ -302,7 +302,7 @@ void SysModWeb::wsEvent(WebSocket * ws, WebClient * client, AwsEventType type, v ui->processJson(responseObject); //adds to responseDoc / responseObject if (responseObject.size()) { - web->sendResponseObject(isUiFun?client:nullptr); //uiFun only send to requesting client async response + sendResponseObject(isUiFun?client:nullptr); //uiFun only send to requesting client async response } else { USER_PRINT_Async("WS_EVT_DATA no responseDoc\n"); @@ -467,7 +467,7 @@ void SysModWeb::serveUpload(WebRequest *request, const String& filename, size_t USER_PRINT_Async("serveUpload r:%s f:%s i:%d l:%d f:%d\n", request->url().c_str(), filename.c_str(), index, len, final); mdl->setValue("upload", index/10000); - web->sendResponseObject(); //otherwise not send in asyn_tcp thread + sendResponseObject(); //otherwise not send in asyn_tcp thread if (!index) { String finalname = filename; @@ -487,7 +487,7 @@ void SysModWeb::serveUpload(WebRequest *request, const String& filename, size_t request->_tempFile.close(); mdl->setValue("upload", UINT16_MAX - 10); //success - web->sendResponseObject(); //otherwise not send in asyn_tcp thread + sendResponseObject(); //otherwise not send in asyn_tcp thread request->send(200, "text/plain", F("File Uploaded!")); @@ -501,7 +501,7 @@ void SysModWeb::serveUpdate(WebRequest *request, const String& filename, size_t // USER_PRINT_Async("serveUpdate r:%s f:%s i:%d l:%d f:%d\n", request->url().c_str(), filename.c_str(), index, len, final); mdl->setValue("update", index/10000); - web->sendResponseObject(); //otherwise not send in asyn_tcp thread + sendResponseObject(); //otherwise not send in asyn_tcp thread if (!index) { USER_PRINTF("OTA Update Start\n"); @@ -515,13 +515,13 @@ void SysModWeb::serveUpdate(WebRequest *request, const String& filename, size_t Update.write(data, len); else { mdl->setValue("update", UINT16_MAX - 20); //fail - web->sendResponseObject(); //otherwise not send in asyn_tcp thread + sendResponseObject(); //otherwise not send in asyn_tcp thread } if (final) { bool success = Update.end(true); mdl->setValue("update", success?UINT16_MAX - 10:UINT16_MAX - 20); - web->sendResponseObject(); //otherwise not send in asyn_tcp thread + sendResponseObject(); //otherwise not send in asyn_tcp thread char message[64]; const char * instanceName = mdl->getValue("instanceName"); @@ -550,7 +550,7 @@ void SysModWeb::jsonHandler(WebRequest *request, JsonVariant json) { print->printJson("jsonHandler", json); - JsonObject responseObject = web->getResponseObject(); + JsonObject responseObject = getResponseObject(); ui->processJson(json); @@ -573,7 +573,7 @@ void SysModWeb::jsonHandler(WebRequest *request, JsonVariant json) { request->send(200, "application/json", F("{\"success\":true}")); } - web->sendResponseObject(); + sendResponseObject(); } void SysModWeb::clientsToJson(JsonArray array, bool nameOnly, const char * filter) { @@ -614,7 +614,7 @@ bool SysModWeb::captivePortal(WebRequest *request) JsonDocument * SysModWeb::getResponseDoc() { // USER_PRINTF("response wsevent core %d %s\n", xPortGetCoreID(), pcTaskGetTaskName(NULL)); - return strncmp(pcTaskGetTaskName(NULL), "loopTask", 8) == 0?web->responseDocLoopTask:web->responseDocAsyncTCP; + return strncmp(pcTaskGetTaskName(NULL), "loopTask", 8) == 0?responseDocLoopTask:responseDocAsyncTCP; } JsonObject SysModWeb::getResponseObject() { @@ -628,7 +628,7 @@ void SysModWeb::sendResponseObject(WebClient * client) { print->printJson("sendResponseObject", responseObject); print->printJDocInfo(" info", responseObject); } - web->sendDataWs(responseObject, client); + sendDataWs(responseObject, client); getResponseDoc()->to(); //recreate! } } diff --git a/src/SysModules.cpp b/src/SysModules.cpp index ce58c6b1..cefce7a7 100644 --- a/src/SysModules.cpp +++ b/src/SysModules.cpp @@ -23,6 +23,14 @@ void SysModules::setup() { module->setup(); } + //delete mdlTbl values if nr of modules has changed (new values created using module defaults) + for (JsonObject childVar: mdl->varChildren("mdlTbl")) { + if (!childVar["value"].isNull() && mdl->varValArray(childVar).size() != modules.size()) { + USER_PRINTF("mdlTbl clear (%s %s) %d %d\n", childVar["id"].as().c_str(), childVar["value"].as().c_str(), modules.size(), mdl->varValArray(childVar).size()); + childVar.remove("value"); + } + } + //do its own setup: will be shown as last module JsonObject parentVar = ui->initSysMod(parentVar, "Modules", 4203); @@ -60,6 +68,7 @@ void SysModules::setup() { ui->initCheckBox(tableVar, "mdlEnabled", UINT16_MAX, false, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun not readonly! (tbd) case f_ValueFun: //never a rowNr as parameter, set all + //execute only if var has not been set for (forUnsigned8 rowNr = 0; rowNr < modules.size(); rowNr++) mdl->setValue(var, modules[rowNr]->isEnabled, rowNr); return true; @@ -73,7 +82,7 @@ void SysModules::setup() { modules[rowNr]->enabledChanged(); } else { - USER_PRINTF(" no rowNr or > modules.size!!", rowNr); + USER_PRINTF(" no rowNr or %d > modules.size %d!!\n", rowNr, modules.size()); } // print->printJson(" ", var); return true; diff --git a/src/User/UserModE131.h b/src/User/UserModE131.h index f0fe18f0..c42a48c5 100644 --- a/src/User/UserModE131.h +++ b/src/User/UserModE131.h @@ -19,6 +19,7 @@ class UserModE131:public SysModule { public: UserModE131() :SysModule("E131") { + isEnabled = false; //default not enabled }; //setup filesystem diff --git a/src/User/UserModMDNS.h b/src/User/UserModMDNS.h index 73def6bc..8c177031 100644 --- a/src/User/UserModMDNS.h +++ b/src/User/UserModMDNS.h @@ -45,6 +45,8 @@ class UserModMDNS:public SysModule { } void resetMDNS() { + + if (!mdls->isConnected) return; //reset cmDNS const char * instanceName = mdl->getValue("instanceName");