Skip to content

Commit

Permalink
Button has no value (in model, it has in HTML)
Browse files Browse the repository at this point in the history
index.js: sendValue: button: send value true

SysModFiles: fileTbl: chFun: use responseObject

SysModUI:
- initButton: remove value
- initVarAndUpdate: no value if type == button

SysModWeb: jsonHandler: processJson if not v
  • Loading branch information
ewowi committed Feb 8, 2024
1 parent cae2f8f commit 427a0f2
Show file tree
Hide file tree
Showing 11 changed files with 892 additions and 889 deletions.
2 changes: 2 additions & 0 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,8 @@ function sendValue(varNode) {
command[varId].value = varNode.querySelector("input").checked;
else if (varNode.nodeName.toLocaleLowerCase() == "span")
command[varId].value = varNode.innerText;
else if (varNode.className == "button")
command[varId].value = true;
else if (varNode.className == "coord3D") {
let coord = {};
coord.x = parseInt(varNode.childNodes[0].value);
Expand Down
2 changes: 1 addition & 1 deletion src/App/AppModFixtureGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ class AppModFixtureGen:public SysModule {
web->addResponse(var["id"], "comment", "One or more e.g. 12,13,14");
});

ui->initButton(parentVar, "generate", nullptr, false, [](JsonObject var) { //uiFun
ui->initButton(parentVar, "generate", false, [](JsonObject var) { //uiFun
}, [this](JsonObject var, uint8_t) { //chFun
generateChFun(var);

Expand Down
6 changes: 3 additions & 3 deletions src/Sys/SysModFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ void SysModFiles::setup() {
if (strcmp(var["value"], "delRow") == 0) {
USER_PRINTF("fileTbl chFun %s %d %s\n", var["id"].as<const char *>(), rowNr, var["value"].as<String>().c_str());
if (rowNr != UINT8_MAX) {
// call uiFun of tbl to fill responseVariant with files
// call uiFun of tbl to fill responseObject with files
ui->uFunctions[var["uiFun"]](var);
//trick to get the table values tbd: use column values
JsonVariant responseVariant = web->getResponseDoc()->as<JsonVariant>();
JsonArray row = responseVariant["fileTbl"]["value"][rowNr];
JsonObject responseObject = web->getResponseDoc()->as<JsonObject>(); //not to as already created by callChFunAndWs
JsonArray row = responseObject["fileTbl"]["value"][rowNr];
if (!row.isNull()) {
const char * fileName = row[0]; //first column
print->printJson("delete file", row);
Expand Down
4 changes: 2 additions & 2 deletions src/Sys/SysModModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void SysModModel::setup() {
web->addResponse(var["id"], "label", "Size");
});

ui->initButton(parentVar, "saveModel", nullptr, false, [](JsonObject var) { //uiFun
ui->initButton(parentVar, "saveModel", false, [](JsonObject var) { //uiFun
web->addResponse(var["id"], "comment", "Write to model.json (manual save only currently)");
}, [this](JsonObject var, uint8_t) { //chFun
doWriteModel = true;
Expand All @@ -54,7 +54,7 @@ void SysModModel::setup() {
doShowObsolete = var["value"];
});

ui->initButton(parentVar, "deleteObsolete", nullptr, false, [](JsonObject var) { //uiFun
ui->initButton(parentVar, "deleteObsolete", false, [](JsonObject var) { //uiFun
web->addResponse(var["id"], "label", "Delete obsolete variables");
web->addResponse(var["id"], "comment", "WIP");
});
Expand Down
1 change: 1 addition & 0 deletions src/Sys/SysModModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct Coord3D {

//assignments
Coord3D operator=(Coord3D rhs) {
USER_PRINTF("Coord3D assign %d,%d,%d\n", rhs.x, rhs.y, rhs.z);
this->x = rhs.x;
this->y = rhs.y;
this->z = rhs.z;
Expand Down
2 changes: 1 addition & 1 deletion src/Sys/SysModNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void SysModNetwork::setup() {
web->addResponse(var["id"], "label", "Password");
});

ui->initButton(parentVar, "connect", nullptr, false, [](JsonObject var) { //uiFun
ui->initButton(parentVar, "connect", false, [](JsonObject var) { //uiFun
web->addResponse(var["id"], "comment", "Force reconnect (loose current connection)");
}, [this](JsonObject var, uint8_t) { //chFun
// mdl->doWriteModel = true; //saves the model
Expand Down
2 changes: 1 addition & 1 deletion src/Sys/SysModSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void SysModSystem::setup() {
web->addResponseV(var["id"], "comment", "%d B", uxTaskGetStackHighWaterMark(NULL));
});

ui->initButton(parentVar, "reboot", nullptr, false, nullptr, [](JsonObject var, uint8_t) { //chFun
ui->initButton(parentVar, "reboot", false, nullptr, [](JsonObject var, uint8_t) { //chFun
web->ws->closeAll(1012);

// mdls->reboot(); //not working yet
Expand Down
6 changes: 1 addition & 5 deletions src/Sys/SysModUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,7 @@ void SysModUI::processJson(JsonVariant json) {
JsonVariant value = pair.value();

// commands
if (pair.key() == "v") {
// do nothing as it is no real var but the verbose command of WLED
USER_PRINTF("processJson v type %s\n", pair.value().as<String>().c_str());
}
else if (pair.key() == "view" || pair.key() == "canvasData" || pair.key() == "theme") { //save the chosen view in System (see index.js)
if (pair.key() == "view" || pair.key() == "canvasData" || pair.key() == "theme") { //save the chosen view in System (see index.js)
JsonObject var = mdl->findVar("System");
USER_PRINTF("processJson %s v:%s n: %d s:%s\n", pair.key().c_str(), pair.value().as<String>().c_str(), var.isNull(), var["id"].as<const char *>());
var[JsonString(key, JsonString::Copied)] = JsonString(value, JsonString::Copied);
Expand Down
33 changes: 18 additions & 15 deletions src/Sys/SysModUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ class SysModUI:public SysModule {
return initVarAndUpdate<int>(parent, id, "checkbox", value, rowNr, 0, 0, readOnly, uiFun, chFun, loopFun, count, valueFun);
}

JsonObject initButton(JsonObject parent, const char * id, const char * value = nullptr, bool readOnly = false, UFun uiFun = nullptr, CFun chFun = nullptr, CFun loopFun = nullptr, uint8_t count = 0, CFun valueFun = nullptr) {
return initVarAndUpdate<const char *>(parent, id, "button", value, UINT8_MAX, 0, 0, readOnly, uiFun, chFun, loopFun, count, valueFun);
//a button never gets a value
JsonObject initButton(JsonObject parent, const char * id, bool readOnly = false, UFun uiFun = nullptr, CFun chFun = nullptr, CFun loopFun = nullptr, uint8_t count = 0, CFun valueFun = nullptr) {
return initVarAndUpdate<bool>(parent, id, "button", false, UINT8_MAX, 0, 0, readOnly, uiFun, chFun, loopFun, count, valueFun);
}

JsonObject initSelect(JsonObject parent, const char * id, int value = UINT16_MAX, uint8_t rowNr = UINT8_MAX, bool readOnly = false, UFun uiFun = nullptr, CFun chFun = nullptr, CFun loopFun = nullptr, uint8_t count = 0, CFun valueFun = nullptr) {
Expand All @@ -131,20 +132,22 @@ class SysModUI:public SysModule {
if (max && max != UINT16_MAX) var["max"] = max;

bool valueNeedsUpdate = false;
if (var["value"].isNull()) {
valueNeedsUpdate = true;
// print->printJson("initVarAndUpdate uiFun value is null", var);
} else if (var["value"].is<JsonArray>()) {
JsonArray valueArray = var["value"].as<JsonArray>();
if (rowNr != UINT8_MAX) {
if (rowNr >= valueArray.size())
valueNeedsUpdate = true;
else if (valueArray[rowNr].isNull())
valueNeedsUpdate = true;
}
else if (count && valueArray.size() != count) {
print->printJson("initVarAndUpdate uiFun value array wrong size", var);
if (strcmp(type, "button") != 0) { //button never gets a value
if (var["value"].isNull()) {
valueNeedsUpdate = true;
// print->printJson("initVarAndUpdate uiFun value is null", var);
} else if (var["value"].is<JsonArray>()) {
JsonArray valueArray = var["value"].as<JsonArray>();
if (rowNr != UINT8_MAX) {
if (rowNr >= valueArray.size())
valueNeedsUpdate = true;
else if (valueArray[rowNr].isNull())
valueNeedsUpdate = true;
}
else if (count && valueArray.size() != count) {
print->printJson("initVarAndUpdate uiFun value array wrong size", var);
valueNeedsUpdate = true;
}
}
}

Expand Down
27 changes: 14 additions & 13 deletions src/Sys/SysModWeb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,26 +517,27 @@ void SysModWeb::serveFiles(WebRequest *request) {

void SysModWeb::jsonHandler(WebRequest *request, JsonVariant json) {

JsonDocument *responseDoc = web->getResponseDoc();
responseDoc->clear(); //needed for deserializeJson?
JsonVariant responseVariant = responseDoc->as<JsonVariant>();
JsonObject responseObject = web->getResponseDoc()->to<JsonObject>();

print->printJson("AsyncCallbackJsonWebHandler", json);
ui->processJson(json); //processJson
print->printJson("jsonHandler", json);

//WLED compatibility
if (json["v"]) { //WLED compatibility: verbose response
serveJson (request);
}
else if (responseVariant.size()) { //responseVariant set by processFunc
char resStr[200];
serializeJson(responseVariant, resStr, 200);
USER_PRINT_Async("processJsonUrl response %s\n", resStr);
request->send(200, "application/json", resStr);
else {
ui->processJson(json);
if (responseObject.size()) { //responseVariant set by processJson e.g. uiFun

char resStr[200];
serializeJson(responseObject, resStr, 200);
USER_PRINT_Async("processJsonUrl response %s\n", resStr);
request->send(200, "application/json", resStr);
}
else
// request->send(200, "text/plain", "OK");
request->send(200, "application/json", F("{\"success\":true}"));
}
else
// request->send(200, "text/plain", "OK");
request->send(200, "application/json", F("{\"success\":true}"));
}

void SysModWeb::clientsToJson(JsonArray array, bool nameOnly, const char * filter) {
Expand Down
Loading

0 comments on commit 427a0f2

Please sign in to comment.