Skip to content

Commit

Permalink
Introduce var.onLoop1s, refactor addResponse + optimalisations
Browse files Browse the repository at this point in the history
RAM:   [==        ]  17.5% (used 57396 bytes from 327680 bytes)
Flash: [========  ]  75.6% (used 1436177 bytes from 1900544 bytes)

index.js
- onAdd/onDelete: variable.pid
- add variable.pid to variable.id

newui:
- modules.js: add findVarP, generateData add pid

SysModModel
- postDetails: remove modelParentVar and loop1s
- remove setValueV, add setValue with argument list
- loop1s run walkThroughModel for onLoop1s
- add walkThroughModel

SysModUI:
- initVar: no modelParentVar
- findOptionsText: use pidid
- add onLoop1s

SysModWeb:
- remove addResponseA
- addResponse: use pidid and add format string

SysModPrint:
- printf: use Print.log
- fformat: return JsonString

SysModFiles, SysModInstances,
- addResponse use findVar or var

SysModNetwork, SysModSystem, SysModUI, SysModWeb, UserModLive, UserModMPU6050: move from loop1s to var.onLoop1s
  • Loading branch information
ewoudwijma committed Sep 30, 2024
1 parent 210ee6d commit bf11ce0
Show file tree
Hide file tree
Showing 21 changed files with 2,161 additions and 2,122 deletions.
12 changes: 7 additions & 5 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ function createHTML(json, parentNode = null, rowNr = UINT8_MAX) {

var command = {};
command.onAdd = {};
command.onAdd.id = variable.pid + "." + variable.id;
command.onAdd.pid = variable.pid;
command.onAdd.id = variable.id;
requestJson(command);
});
divNode.appendChild(buttonNode);
Expand Down Expand Up @@ -701,7 +702,8 @@ function genTableRowHTML(json, parentNode = null, rowNr = UINT8_MAX) {

var command = {};
command.onDelete = {};
command.onDelete.id = variable.pid + "." + variable.id;
command.onDelete.pid = variable.pid;
command.onDelete.id = variable.id;
command.onDelete.rowNr = rowNr;
requestJson(command);

Expand All @@ -710,7 +712,7 @@ function genTableRowHTML(json, parentNode = null, rowNr = UINT8_MAX) {
trNode.appendChild(tdNode);
}
flushOnUICommands();
if (variable.id == "insTbl")
if (variable.pid == "Instances" && variable.id == "insTbl")
setInstanceTableColumns();
}

Expand Down Expand Up @@ -810,7 +812,7 @@ function receiveData(json) {
} else if (key == "updRow") { //update the row of a table

let tableId = value.id;
let tableVar = controller.modules.findVar(tableId);
let tableVar = controller.modules.findVar(tableId); //TBD!!!
let rowNr = value.rowNr;
let tableRow = value.value;

Expand Down Expand Up @@ -1036,7 +1038,7 @@ function changeHTML(variable, commandJson, rowNr = UINT8_MAX) {

flushOnUICommands(); //make sure onUIs of new elements are called

if (variable.id == "insTbl")
if (variable.pid == "Instances" && variable.id == "insTbl")
setInstanceTableColumns();
}
else
Expand Down
4 changes: 2 additions & 2 deletions data/newui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ class Controller {
}

receiveData(json) {
// console.log("receiveData", json)
console.log("receiveData", json)
if (isObject(json)) {
for (let key of Object.keys(json)) {
let value = json[key]
let value = json[key] //contains pid.id

let variable = this.modules.findVar(key);
if (variable) {
Expand Down
11 changes: 10 additions & 1 deletion data/newui/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ class Modules {
})
}

//finds a var with id in the model, if found returns it
findVarP(pid, id) {
// console.log("findVar", id, parent, model);
return this.walkThroughModel(function(variable){
if (variable.pid == pid, variable.id == id) //found variable
return variable; //this stops the walkThrough
})
}

previewBoard(canvasNode, buffer) {
let boardColor;
if (controller.sysInfo.board == "esp32s2") boardColor = "purple";
Expand Down Expand Up @@ -304,7 +313,7 @@ class Variable {
//used by Live Server
generateData(custom = `"value":${Math.random() * 256}`) {
if (custom != "") custom += ", "
controller.receiveData(JSON.parse(`{"${this.variable.id}":{${custom}"comment":"${Math.random().toString(36).slice(2)}"}}`));
controller.receiveData(JSON.parse(`{"${this.variable.pid + "." + this.variable.id}":{${custom}"comment":"${Math.random().toString(36).slice(2)}"}}`));
}

} //class Variable
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ lib_deps =
build_flags =
-D APP=StarBase
-D PIOENV=$PIOENV
-D VERSION=24092320 ; Date and time (GMT!), update at every commit!!
-D VERSION=24093015 ; Date and time (GMT!), update at every commit!!
-D LFS_THREADSAFE ; enables use of semaphores in LittleFS driver
-D STARBASE_DEVMODE
-mtext-section-literals ;otherwise [UserModLive::setup()]+0xa17): dangerous relocation: l32r: literal target out of range (try using text-section-literals)
Expand Down
2 changes: 1 addition & 1 deletion src/Sys/SysModFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void SysModFiles::setup() {
return true;
case onChange:
var["max"] = files->totalBytes(); //makes sense?
web->addResponseV(var["id"], "comment", "%d / %d B", files->usedBytes(), files->totalBytes());
web->addResponse(var, "comment", "%d / %d B", files->usedBytes(), files->totalBytes());
return true;
default: return false;
}});
Expand Down
2 changes: 1 addition & 1 deletion src/Sys/SysModInstances.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class SysModInstances:public SysModule {
//do what setValue is doing except calling onChange
// insVar["value"][rowNrL] = instances[rowNrL].jsonData[Variable(var).id()]; //only int values...

web->addResponse(insVar["id"], "value", instances[rowNrL].jsonData[Variable(var).id()], rowNrL);
web->addResponse(insVar, "value", instances[rowNrL].jsonData[Variable(var).id()], rowNrL);

// mdl->setValue(insVar, instances[rowNrL].jsonData[Variable(var).id()], rowNr);
//send to ws?
Expand Down
35 changes: 28 additions & 7 deletions src/Sys/SysModModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ void SysModModel::loop20ms() {
}
}

void SysModModel::loop1s() {
mdl->walkThroughModel([](JsonObject var) {
ui->callVarFun(var, UINT8_MAX, onLoop1s);
return false; //don't stop
});
}

void SysModModel::cleanUpModel(JsonObject parent, bool oPos, bool ro) {

JsonArray vars;
Expand Down Expand Up @@ -149,13 +156,30 @@ void SysModModel::cleanUpModel(JsonObject parent, bool oPos, bool ro) {
}
}

JsonObject SysModModel::findVar(const char * id, JsonObject parent) {
bool SysModModel::walkThroughModel(std::function<bool(JsonObject)> fun, JsonObject parent) {
JsonArray root;
// print ->print("findVar %s %s\n", id, parent.isNull()?"root":"n");
if (parent.isNull()) {
if (parent.isNull())
root = model->as<JsonArray>();
modelParentVar = JsonObject();
else
root = parent["n"];

for (JsonObject var : root) {
// ppf(" %s", var["id"].as<String>());
if (fun(var)) return true;

if (!var["n"].isNull()) {
if (walkThroughModel(fun, var)) return true;
}
}
return false; //don't stop
}


JsonObject SysModModel::findVar(const char * id, JsonObject parent) {
JsonArray root;
// ppf("findVar %s %s\n", id, parent.isNull()?"root":"n");
if (parent.isNull())
root = model->as<JsonArray>();
else
root = parent["n"];

Expand Down Expand Up @@ -183,16 +207,13 @@ JsonObject SysModModel::findVar(const char * id, JsonObject parent) {
// ppf("🐍%s", id);
}


for (JsonObject var : root) {
// if (foundVar.isNull()) {
if ((pid[0]=='\0' || var["pid"] == pid) && var["id"] == id)
return var;
else if (!var["n"].isNull()) {
JsonObject foundVar = findVar(id, var);
if (!foundVar.isNull()) {
if (modelParentVar.isNull()) modelParentVar = var; //only recursive lowest assigns parentVar
// ppf("findvar parent of %s is %s\n", id, Variable(modelParentVar).id());
return foundVar;
}
}
Expand Down
33 changes: 10 additions & 23 deletions src/Sys/SysModModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ class Variable {
ppf("remove allnulls %s\n", childVariable.id());
children().remove(childVarIt);
}
web->addResponse("details", "rowNr", rowNr);
web->getResponseObject()["details"]["rowNr"] = rowNr;

}
else
print->printJson("dev array but not rowNr", var);
Expand All @@ -330,7 +331,7 @@ class Variable {
ppf("\n");

//post update details
web->addResponse("details", "var", var);
web->getResponseObject()["details"]["var"] = var;
}

}; //class Variable
Expand All @@ -343,8 +344,6 @@ class SysModModel:public SysModule {
RAM_Allocator allocator;
JsonDocument *model = nullptr;

JsonObject modelParentVar;

bool doWriteModel = false;

unsigned8 setValueRowNr = UINT8_MAX;
Expand All @@ -354,6 +353,7 @@ class SysModModel:public SysModule {
SysModModel();
void setup();
void loop20ms();
void loop1s();

//scan all vars in the model and remove vars where var["o"] is negative or positive, if ro then remove ro values
void cleanUpModel(JsonObject parent = JsonObject(), bool oPos = true, bool ro = false);
Expand Down Expand Up @@ -411,7 +411,7 @@ class SysModModel:public SysModule {
ppf("setValue changed %s %s -> %s\n", variable.id(), var["oldValue"].as<String>().c_str(), variable.valueString().c_str());
// else
// ppf("setValue changed %s %s\n", Variable(var).id(), var["value"].as<String>().c_str());
web->addResponse(variable.id(), "value", var["value"]);
web->addResponse(var, "value", var["value"]);
changed = true;
}
}
Expand All @@ -437,7 +437,7 @@ class SysModModel:public SysModule {
// ppf("notSame %d %d\n", rowNr, valueArray.size());
valueArray[rowNr] = value; //if valueArray[<rowNr] not exists it will be created
// ppf(" assigned %d %d %s\n", rowNr, valueArray.size(), valueArray[rowNr].as<String>().c_str());
web->addResponse(variable.id(), "value", var["value"]); //send the whole array to UI as response is in format value:<value> !!
web->addResponse(var, "value", var["value"]); //send the whole array to UI as response is in format value:<value> !!
changed = true;
}
}
Expand All @@ -451,21 +451,8 @@ class SysModModel:public SysModule {
return var;
}

//Set value with argument list with rowNr (rowNr cannot have a default)
JsonObject setValueV(const char * id, unsigned8 rowNr = UINT8_MAX, const char * format = nullptr, ...) {
va_list args;
va_start(args, format);

char value[128];
vsnprintf(value, sizeof(value)-1, format, args);

va_end(args);

ppf("setValueV %s[%d] = %s\n", id, rowNr, value);
return setValue(id, JsonString(value, JsonString::Copied), rowNr);
}

void setUIValueV(const char * id, const char * format, ...) {
//Set value with argument list
JsonObject setValue(JsonObject var, const char * format = nullptr, ...) {
va_list args;
va_start(args, format);

Expand All @@ -474,8 +461,7 @@ class SysModModel:public SysModule {

va_end(args);

//no print
web->addResponse(id, "value", JsonString(value, JsonString::Copied)); //setValue not necessary
return setValue(var, JsonString(value, JsonString::Copied));
}

JsonVariant getValue(const char * id, unsigned8 rowNr = UINT8_MAX) {
Expand Down Expand Up @@ -507,6 +493,7 @@ class SysModModel:public SysModule {
}

//returns the var defined by id (parent to recursively call findVar)
bool walkThroughModel(std::function<bool(JsonObject)> fun, JsonObject parent = JsonObject());
JsonObject findVar(const char * id, JsonObject parent = JsonObject());
// JsonObject findParentVar(const char * id, JsonObject parent = JsonObject());
void findVars(const char * id, bool value, FindFun fun, JsonArray parent = JsonArray());
Expand Down
47 changes: 25 additions & 22 deletions src/Sys/SysModNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ void SysModNetwork::setup() {
case onUI:
ui->setLabel(var, "Ethernet");
return true;
//initEthernet not done in onChange as initEthernet needs a bit of a delay
case onLoop1s:
//initEthernet not done in onChange as initEthernet needs a bit of a delay
if (!ethActive && mdl->getValue(var).as<bool>())
initEthernet();
default: return false;
}});

Expand Down Expand Up @@ -143,10 +146,13 @@ void SysModNetwork::setup() {
default: return false;
}});

ui->initText(currentVar, "etStatus", nullptr, 32, true, [](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
ui->initText(currentVar, "etStatus", nullptr, 32, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case onUI:
ui->setLabel(var, "Status");
return true;
case onLoop1s:
mdl->setValue(var, "%s %s", ethActive?ETH.localIP()[0]?"🟒":"🟠":"πŸ›‘", ethActive?ETH.localIP().toString().c_str():"inactive");
return true;
default: return false;
}});

Expand Down Expand Up @@ -192,13 +198,19 @@ void SysModNetwork::setup() {
case onUI:
ui->setLabel(var, "Signal");
return true;
case onLoop1s:
mdl->setValue(var, "%d dBm", WiFi.RSSI());
return true;
default: return false;
}});

ui->initText(currentVar, "wfStatus", nullptr, 32, true, [](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
ui->initText(currentVar, "wfStatus", nullptr, 32, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case onUI:
ui->setLabel(var, "Status");
return true;
case onLoop1s:
mdl->setValue(var, "%s %s (s:%d)", wfActive?WiFi.localIP()[0]?"🟒":"🟠":"πŸ›‘", wfActive?WiFi.localIP().toString().c_str():"inactive", WiFi.status());
return true;
default: return false;
}});

Expand All @@ -212,37 +224,28 @@ void SysModNetwork::setup() {
else
stopAP();
return true;
// case onLoop1s:
// if (!apActive && mdl->getValue(var).as<bool>()) {
// stopAP();
// initAP();
// }
// return true;
default: return false;
}});
ui->initText(currentVar, "apStatus", nullptr, 32, true, [](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
ui->initText(currentVar, "apStatus", nullptr, 32, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case onUI:
ui->setLabel(var, "Status");
return true;
case onLoop1s:
mdl->setValue(var, "%s %s", apActive?WiFi.softAPIP()[0]?"🟒":"🟠":"πŸ›‘", apActive?WiFi.softAPIP().toString().c_str():"inactive");
return true;
default: return false;
}});

}

void SysModNetwork::loop1s() {

#ifdef STARBASE_ETHERNET
mdl->setUIValueV("etStatus", "%s %s", ethActive?ETH.localIP()[0]?"🟒":"🟠":"πŸ›‘", ethActive?ETH.localIP().toString().c_str():"inactive");
#endif
mdl->setUIValueV("wfStatus", "%s %s (s:%d)", wfActive?WiFi.localIP()[0]?"🟒":"🟠":"πŸ›‘", wfActive?WiFi.localIP().toString().c_str():"inactive", WiFi.status());
mdl->setUIValueV("apStatus", "%s %s", apActive?WiFi.softAPIP()[0]?"🟒":"🟠":"πŸ›‘", apActive?WiFi.softAPIP().toString().c_str():"inactive");
mdl->setUIValueV("rssi", "%d dBm", WiFi.RSSI());

#ifdef STARBASE_ETHERNET
//not done in onChange as initEthernet needs a bit of a delay
if (!ethActive && mdl->getValue("ethOn").as<bool>())
initEthernet();
#endif

// if (!apActive && mdl->getValue("apOn").as<bool>()) {
// stopAP();
// initAP();
// }

if (apActive)
handleAP();
}
Expand Down
Loading

0 comments on commit bf11ce0

Please sign in to comment.