Skip to content

Commit

Permalink
move value assignments from UIFun to ValueFun
Browse files Browse the repository at this point in the history
move value assignments from UIFun to ValueFun

SysModModel:
- setValue: incl chFun
- rename callChFunAndWs to callChangeFun: no ws (done later)

SysModUI
- callVarFun default valueFun
- callVarFun: if UIFun and var is ro, call valueFun (to send the value to the client as it is not stored in the model)
  • Loading branch information
ewowi committed Feb 14, 2024
1 parent 1d4cbd4 commit a47dc70
Show file tree
Hide file tree
Showing 14 changed files with 156 additions and 94 deletions.
8 changes: 6 additions & 2 deletions src/App/AppModFixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,22 @@ class AppModFixture:public SysModule {
}}); //fixture

ui->initCoord3D(parentVar, "fixSize", lds->fixture.size, 0, UINT16_MAX, true, [](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
case f_ValueFun:
mdl->setValue(var, lds->fixture.size);
return true;
case f_UIFun:
web->addResponse(var["id"], "label", "Size");
web->addResponse(var["id"], "value", lds->fixture.size);
return true;
default: return false;
}});

ui->initNumber(parentVar, "fixCount", lds->fixture.nrOfLeds, 0, UINT16_MAX, true, [](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
case f_ValueFun:
mdl->setValue(var, lds->fixture.nrOfLeds);
return true;
case f_UIFun:
web->addResponse(var["id"], "label", "Count");
web->addResponseV(var["id"], "comment", "Max %d", NUM_LEDS_Max);
web->addResponse(var["id"], "value", lds->fixture.nrOfLeds);
return true;
default: return false;
}});
Expand Down
10 changes: 5 additions & 5 deletions src/App/AppModLeds.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,16 @@ class AppModLeds:public SysModule {
}});

ui->initText(tableVar, "fxSize", nullptr, 32, true, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
case f_UIFun:
web->addResponse(var["id"], "label", "Size");
//set value for this ro var
case f_ValueFun:
for (uint8_t rowNr = 0; rowNr < fixture.ledsList.size(); rowNr++) {
Leds leds = fixture.ledsList[rowNr];
char message[32];
print->fFormat(message, sizeof(message)-1, "%d x %d x %d = %d", leds.size.x, leds.size.y, leds.size.z, leds.nrOfLeds);
web->addResponse(var["id"], "value", JsonString(message, JsonString::Copied), rowNr);
mdl->setValue(var, JsonString(message, JsonString::Copied), rowNr);
}

return true;
case f_UIFun:
web->addResponse(var["id"], "label", "Size");
return true;
default: return false;
}});
Expand Down
4 changes: 3 additions & 1 deletion src/Sys/SysModFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@ void SysModFiles::setup() {
}});

ui->initProgress(parentVar, "drsize", UINT16_MAX, 0, files->totalBytes(), true, [](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
case f_ValueFun:
mdl->setValue(var, files->usedBytes());
return true;
case f_UIFun:
web->addResponse(var["id"], "label", "FS Size");
return true;
case f_ChangeFun:
var["max"] = files->totalBytes(); //makes sense?
web->addResponse(var["id"], "value", files->usedBytes());
web->addResponseV(var["id"], "comment", "%d / %d B", files->usedBytes(), files->totalBytes());
return true;
default: return false;
Expand Down
13 changes: 6 additions & 7 deletions src/Sys/SysModModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ void SysModModel::setup() {
if (parentVar["o"] > -1000) parentVar["o"] = -4000; //set default order. Don't use auto generated order as order can be changed in the ui (WIP)

ui->initProgress(parentVar, "mSize", UINT16_MAX, 0, model->capacity(), true, [](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
case f_ValueFun:
mdl->setValue(var, model->memoryUsage());
return true;
case f_UIFun:
web->addResponse(var["id"], "label", "Size");
return true;
case f_ChangeFun:
var["max"] = model->capacity(); //makes sense?
web->addResponse(var["id"], "value", model->memoryUsage());
web->addResponseV(var["id"], "comment", "%d / %d B", model->memoryUsage(), model->capacity());
return true;
default: return false;
Expand Down Expand Up @@ -210,16 +212,13 @@ void SysModModel::varToValues(JsonObject var, JsonArray row) {
}
}

//run the change function and send response to all websocket clients
void SysModModel::callChFunAndWs(JsonObject var, uint8_t rowNr) {

web->addResponse(var["id"], "value", var["value"]);
void SysModModel::callChangeFun(JsonObject var, uint8_t rowNr) {

//done here as ui cannot be used in SysModModel.h
if (var["stage"])
ui->stageVarChanged = true;

ui->callVarFun(var, rowNr, f_ChangeFun); //if no rowNr use rowNr 0
ui->callVarFun(var, rowNr, f_ChangeFun);

web->sendResponseObject();
// web->sendResponseObject();
}
19 changes: 11 additions & 8 deletions src/Sys/SysModModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,13 @@ class SysModModel:public SysModule {
template <typename Type>
JsonObject setValue(JsonObject var, Type value, uint8_t rowNr = UINT8_MAX) {
// print->printJson("setValueB", var);
bool changed = false;
if (var["ro"].as<bool>()) {
web->addResponse(var["id"], "value", value, rowNr);
// print->printJson("setValue ro to ws", web->getResponseObject());
web->sendResponseObject();
return var;
web->addResponse(var["id"], "value", value, rowNr);
changed = true;
}

if (rowNr == UINT8_MAX) { //normal situation
else if (rowNr == UINT8_MAX) { //normal situation
if (var["value"].isNull() || var["value"].as<Type>() != value) { //const char * will be JsonString so comparison works
JsonString oldValue = JsonString(var["value"], JsonString::Copied);
var["value"] = value;
Expand All @@ -175,7 +174,8 @@ class SysModModel:public SysModule {
}
else {
USER_PRINTF("setValue changed %s %s -> %s\n", jsonToChar(var, "id"), oldValue.c_str(), var["value"].as<String>().c_str()); //old value
callChFunAndWs(var);
web->addResponse(var["id"], "value", var["value"], rowNr);
changed = true;
}
}
}
Expand All @@ -202,13 +202,16 @@ class SysModModel:public SysModule {
// USER_PRINTF("notSame %d %d\n", rowNr, valueArray.size());
valueArray[rowNr] = value; //if valueArray[<rowNr] not exists it will be created
// USER_PRINTF(" assigned %d %d %s\n", rowNr, valueArray.size(), valueArray[rowNr].as<String>().c_str());
callChFunAndWs(var, rowNr);
web->addResponse(var["id"], "value", var["value"], rowNr);
changed = true;
}
}
else {
USER_PRINTF("setValue %s could not create value array\n", jsonToChar(var, "id"));
}
}

if (changed) callChangeFun(var, rowNr);

return var;
}
Expand Down Expand Up @@ -275,7 +278,7 @@ class SysModModel:public SysModule {
void varToValues(JsonObject var, JsonArray values);

//run the change function and send response to all? websocket clients
static void callChFunAndWs(JsonObject var, uint8_t rowNr = UINT8_MAX);
static void callChangeFun(JsonObject var, uint8_t rowNr = UINT8_MAX);

const char * jsonToChar(JsonObject var, const char * name) {
return var[name].as<const char *>();
Expand Down
19 changes: 12 additions & 7 deletions src/Sys/SysModPins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,34 @@ void SysModPins::setup() {
}});

ui->initNumber(tableVar, "pinNr", UINT16_MAX, 0, NUM_PINS, true, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
case f_ValueFun:
for (uint8_t rowNr = 0; rowNr < getNrOfAllocatedPins(); rowNr++)
mdl->setValue(var, getPinNr(rowNr), rowNr);
return true;
case f_UIFun:
web->addResponse(var["id"], "label", "Pin");
for (uint8_t rowNr = 0; rowNr < getNrOfAllocatedPins(); rowNr++) {
web->addResponse(var["id"], "value", getPinNr(rowNr), rowNr);
}
return true;
default: return false;
}});

ui->initText(tableVar, "pinOwner", nullptr, 32, true, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
case f_ValueFun:
for (uint8_t rowNr = 0; rowNr < getNrOfAllocatedPins(); rowNr++)
mdl->setValue(var, JsonString(getNthAllocatedPinObject(rowNr).owner, JsonString::Copied));
return true;
case f_UIFun:
web->addResponse(var["id"], "label", "Owner");
for (uint8_t rowNr = 0; rowNr < getNrOfAllocatedPins(); rowNr++)
web->addResponse(var["id"], "value", JsonString(getNthAllocatedPinObject(rowNr).owner, JsonString::Copied), rowNr);
return true;
default: return false;
}});

ui->initText(tableVar, "pinDetails", nullptr, 256, true, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
case f_ValueFun:
for (uint8_t rowNr = 0; rowNr < getNrOfAllocatedPins(); rowNr++)
mdl->setValue(var, JsonString(getNthAllocatedPinObject(rowNr).details, JsonString::Copied));
return true;
case f_UIFun:
web->addResponse(var["id"], "label", "Details");
for (uint8_t rowNr = 0; rowNr < getNrOfAllocatedPins(); rowNr++)
web->addResponse(var["id"], "value", JsonString(getNthAllocatedPinObject(rowNr).details, JsonString::Copied), rowNr);
return true;
default: return false;
}});
Expand Down
24 changes: 18 additions & 6 deletions src/Sys/SysModSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,60 +61,72 @@ void SysModSystem::setup() {
ui->initText(parentVar, "chip", nullptr, 16, true);

ui->initProgress(parentVar, "heap", UINT16_MAX, 0, ESP.getHeapSize()/1000, true, [](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
case f_ValueFun:
mdl->setValue(var, (ESP.getHeapSize()-ESP.getFreeHeap()) / 1000);
return true;
case f_ChangeFun:
var["max"] = ESP.getHeapSize()/1000; //makes sense?
web->addResponse(var["id"], "value", (ESP.getHeapSize()-ESP.getFreeHeap()) / 1000);
web->addResponseV(var["id"], "comment", "f:%d / t:%d (l:%d) B", ESP.getFreeHeap(), ESP.getHeapSize(), ESP.getMaxAllocHeap());
return true;
default: return false;
}});

if (psramFound()) {
ui->initProgress(parentVar, "psram", UINT16_MAX, 0, ESP.getPsramSize()/1000, true, [](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
case f_ValueFun:
mdl->setValue(var, (ESP.getPsramSize()-ESP.getFreePsram()) / 1000);
return true;
case f_ChangeFun:
var["max"] = ESP.getPsramSize()/1000; //makes sense?
web->addResponse(var["id"], "value", (ESP.getPsramSize()-ESP.getFreePsram()) / 1000);
web->addResponseV(var["id"], "comment", "%d / %d (%d) B", ESP.getFreePsram(), ESP.getPsramSize(), ESP.getMinFreePsram());
return true;
default: return false;
}});
}

ui->initProgress(parentVar, "stack", UINT16_MAX, 0, 4096, true, [](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
case f_ValueFun:
mdl->setValue(var, uxTaskGetStackHighWaterMark(NULL));
return true;
case f_ChangeFun:
web->addResponse(var["id"], "value", uxTaskGetStackHighWaterMark(NULL));
web->addResponseV(var["id"], "comment", "%d / 4096 B", uxTaskGetStackHighWaterMark(NULL));
return true;
default: return false;
}});

ui->initSelect(parentVar, "reset0", (int)rtc_get_reset_reason(0), true, [](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
case f_ValueFun:
mdl->setValue(var, rtc_get_reset_reason(0));
return true;
case f_UIFun:
web->addResponse(var["id"], "label", "Reset 0");
web->addResponse(var["id"], "comment", "Reason Core 0");
sys->addResetReasonsSelect(web->addResponseA(var["id"], "options"));
web->addResponse(var["id"], "value", rtc_get_reset_reason(0)); //tmp as ro values not saved
return true;
default: return false;
}});

if (ESP.getChipCores() > 1)
ui->initSelect(parentVar, "reset1", (int)rtc_get_reset_reason(1), true, [](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
case f_ValueFun:
mdl->setValue(var, rtc_get_reset_reason(1));
return true;
case f_UIFun:
web->addResponse(var["id"], "label", "Reset 1");
web->addResponse(var["id"], "comment", "Reason Core 1");
sys->addResetReasonsSelect(web->addResponseA(var["id"], "options"));
web->addResponse(var["id"], "value", rtc_get_reset_reason(1)); //tmp as ro values not saved
return true;
default: return false;
}});

ui->initSelect(parentVar, "restartReason", (int)esp_reset_reason(), true, [](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
case f_ValueFun:
mdl->setValue(var, esp_reset_reason());
return true;
case f_UIFun:
web->addResponse(var["id"], "label", "Restart");
web->addResponse(var["id"], "comment", "Reason restart");
sys->addRestartReasonsSelect(web->addResponseA(var["id"], "options"));
web->addResponse(var["id"], "value", esp_reset_reason()); //tmp as ro values not saved
return true;
default: return false;
}});
Expand Down
12 changes: 8 additions & 4 deletions src/Sys/SysModUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,23 @@ void SysModUI::setup() {
}});

initText(tableVar, "vlVar", nullptr, 32, true, [](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
case f_ValueFun:
for (uint8_t rowNr = 0; rowNr < loopFunctions.size(); rowNr++)
mdl->setValue(var, JsonString(loopFunctions[rowNr].var["id"], JsonString::Copied), rowNr);
return true;
case f_UIFun:
web->addResponse(var["id"], "label", "Name");
for (uint8_t rowNr = 0; rowNr < loopFunctions.size(); rowNr++)
web->addResponse(var["id"], "value", JsonString(loopFunctions[rowNr].var["id"], JsonString::Copied), rowNr);
return true;
default: return false;
}});

initNumber(tableVar, "vlLoopps", UINT16_MAX, 0, 999, true, [](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
case f_ValueFun:
for (uint8_t rowNr = 0; rowNr < loopFunctions.size(); rowNr++)
mdl->setValue(var, loopFunctions[rowNr].counter, rowNr);
return true;
case f_UIFun:
web->addResponse(var["id"], "label", "Loops p s");
for (uint8_t rowNr = 0; rowNr < loopFunctions.size(); rowNr++)
web->addResponse(var["id"], "value", loopFunctions[rowNr].counter, rowNr);
return true;
default: return false;
}});
Expand Down
10 changes: 8 additions & 2 deletions src/Sys/SysModUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class SysModUI:public SysModule {

public:
static bool stageVarChanged;// = false; //tbd: move mechanism to UserModInstances as there it will be used
static std::vector<VarFun> varFunctions; //static because of static functions callChFunAndWs, processJson...
static std::vector<VarFun> varFunctions; //static because of static functions callChangeFun, processJson...

static uint8_t parentRowNr;

Expand Down Expand Up @@ -203,7 +203,7 @@ class SysModUI:public SysModule {
return callVarFun(var, rowNr, funType);
}

uint8_t callVarFun(JsonObject var, uint8_t rowNr = UINT8_MAX, uint8_t funType = f_ChangeFun) {
uint8_t callVarFun(JsonObject var, uint8_t rowNr = UINT8_MAX, uint8_t funType = f_ValueFun) {
uint8_t result = false;

if (!var["fun"].isNull()) {//isNull needed here!
Expand All @@ -224,6 +224,12 @@ class SysModUI:public SysModule {
else
USER_PRINTF("dev callVarFun function nr %s outside bounds %d >= %d\n", mdl->jsonToChar(var, "id"), funNr, varFunctions.size());
}

//for ro variables, call valueFun to add also the value in responseDoc (as it is not stored in the model)
if (funType == f_UIFun && var["ro"].as<bool>()) {
callVarFun(var, rowNr, f_ValueFun);
}

return result;
}

Expand Down
Loading

0 comments on commit a47dc70

Please sign in to comment.