Skip to content

Commit

Permalink
empty mdlTbl if nr of modules have been changed + small changes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ewoudwijma committed Apr 9, 2024
1 parent 0314b55 commit 65cbd00
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/Sys/SysModModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>().c_str(), rowNr);
return JsonVariant();
return JsonVariant(); // return null
}
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/Sys/SysModNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions src/Sys/SysModUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/Sys/SysModUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>().c_str());
Expand Down
24 changes: 12 additions & 12 deletions src/Sys/SysModWeb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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");
Expand Down Expand Up @@ -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;
Expand All @@ -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!"));

Expand All @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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);

Expand All @@ -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) {
Expand Down Expand Up @@ -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() {
Expand All @@ -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<JsonObject>(); //recreate!
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/SysModules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>().c_str(), childVar["value"].as<String>().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);

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/User/UserModE131.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class UserModE131:public SysModule {
public:

UserModE131() :SysModule("E131") {
isEnabled = false; //default not enabled
};

//setup filesystem
Expand Down
2 changes: 2 additions & 0 deletions src/User/UserModMDNS.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class UserModMDNS:public SysModule {
}

void resetMDNS() {

if (!mdls->isConnected) return;

//reset cmDNS
const char * instanceName = mdl->getValue("instanceName");
Expand Down

0 comments on commit 65cbd00

Please sign in to comment.