Skip to content

Commit

Permalink
Refactor pre/postDetails / controls, model cleanup, ordering
Browse files Browse the repository at this point in the history
index.js
- don't show variables without order
- changehtml node search: only search on pidid (not class)

SysModNetwork
- back to preDetails

SysModModel
- remove defaultOrder
- remove pre/postDetails2
- preDetail: remove order of details
- postDetails: remove if no order
- remove cleanUpModel
- deleteObsolete: use iterator for delete
- loop20ms: remove cleanUpModel, dowriteModel: use walkThrougModel to remove ro
- initVar: set order without -

SysModWeb
- init state and info (serveJson bugfix)
  • Loading branch information
ewowi committed Dec 15, 2024
1 parent 22244af commit 2f8c09b
Show file tree
Hide file tree
Showing 7 changed files with 1,493 additions and 1,533 deletions.
11 changes: 9 additions & 2 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,12 @@ function createHTML(json, parentNode = null, rowNr = UINT8_MAX) {

//if root (type module) add the html to one of the mdlColumns
if (parentNode == null) {
parentNode = gId("mdlColumn" + variable.o%nrOfMdlColumns);
if (variable.o)
parentNode = gId("mdlColumn" + variable.o%nrOfMdlColumns);
else {
console.log("Variable obsolete", variable);
return null; //do not create ui for variable
}
}
let parentNodeType = parentNode.nodeName.toLocaleLowerCase();

Expand Down Expand Up @@ -852,7 +857,9 @@ function changeHTML(variable, commandJson, rowNr = UINT8_MAX) {

if (!node) {
//we should find all nodes
let rowNodes = document.querySelectorAll(`${variable.type}[id^="${variable.pid + "." + variable.id}#"]`); //find nodes from variable.type class with id + #nr (^: starting with)
let rowNodes = document.querySelectorAll(`[id^="${variable.pid + "." + variable.id}#"]`); //find nodes from variable.type class with id + #nr (^: starting with)
if (variable.id == "xFrequency")
console.log(" rowNodes", rowNodes);
for (let subNode of rowNodes) {
let rowNr = parseInt(subNode.id.substring((variable.pid + "." + variable.id).length + 1));
// console.log("changeHTML found row nodes !", variable.id, subNode.id, commandJson, rowNr);
Expand Down
108 changes: 31 additions & 77 deletions src/Sys/SysModModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
return value()[rowNr].as<String>();
}

JsonArray Variable::children() {
return var["n"];
}

void Variable::removeValuesForRow(uint8_t rowNr) {
for (JsonObject childVar: children()) {
Variable childVariable = Variable(childVar);
Expand Down Expand Up @@ -71,9 +75,7 @@

void Variable::preDetails() {
for (JsonObject varChild: children()) { //for all controls
if (Variable(varChild).order() >= 0) { //post init
Variable(varChild).order( -Variable(varChild).order()); // set all negative
}
varChild.remove("o");
}
ppf("preDetails %s.%s post ", pid(), id());
print->printVar(var);
Expand All @@ -89,20 +91,16 @@
//check if post init added: parent is already >=0
if (order() >= 0) {
for (JsonArray::iterator childVarIt=children().begin(); childVarIt!=children().end(); ++childVarIt) { //use iterator to make .remove work!!!
// for (JsonObject &childVarIt: children) { //use iterator to make .remove work!!!
JsonObject childVar = *childVarIt;
Variable childVariable = Variable(childVar);
JsonArray valArray = childVariable.valArray();
if (!valArray.isNull())
{
if (rowNr != UINT8_MAX) {
if (childVariable.order() <= 0) { //if not updated, or order == 0 (which should not happen so better delete also)
if (childVar["o"].isNull()) { //if not updated, or order == 0 (which should not happen so better delete also)
valArray[rowNr] = (char*)0; // set element in valArray to 0 (is content deleted from memory?)

ppf("varPostDetails %s.%s[%d] <- null\n", id(), childVariable.id(), rowNr);
// setValue(var, -99, rowNr); //set value -99
childVariable.order(-childVariable.order());
//if some values in array are not -99
}

//if all values null, remove value
Expand All @@ -121,11 +119,8 @@
print->printJson("dev array but not rowNr", var);
}
else {
if (childVariable.order() < 0) { //if not updated
// childVariable.value() = (char*)0;
if (childVar["o"].isNull()) { //if not updated
ppf("varPostDetails %s.%s <- null\n", id(), childVariable.id());
// setValue(var, -99, rowNr); //set value -99
// childVariable.order(-childVariable.order());
print->printJson("remove", childVar);
children().remove(childVarIt);
}
Expand All @@ -138,13 +133,6 @@
ppf("\n");

//post update details
}

void Variable::preDetails2() {
var["n"].to<JsonArray>(); //clean array, old array removed?
}
void Variable::postDetails2(uint8_t rowNr) {
web->getResponseObject()["details"]["rowNr"] = rowNr;
web->getResponseObject()["details"]["var"] = var;
}

Expand Down Expand Up @@ -574,19 +562,20 @@ void SysModModel::setup() {

ui->initButton(parentVar, "deleteObsolete", false, [this](EventArguments) { switch (eventType) {
case onUI:
variable.setComment("Delete obsolete variables 🚧");
variable.setComment("Delete unused variables");
return true;
case onChange:
walkThroughModel([](JsonObject parentVar, JsonObject var) {
walkThroughModel([this](JsonObject parentVar, JsonObject var) {

Variable parentVariable = Variable(parentVar);
Variable variable = Variable(var);
JsonArray vars = parentVariable.children();
JsonArray vars = parentVar.isNull()?model->as<JsonArray>() : parentVariable.children();

//no cleanup of o in case of ro value removal
if (var["o"].isNull() || variable.order() < 0) {
ppf("cleanUpModel remove var %s.%s (""o""<0)\n", variable.pid(), variable.id());
vars.remove(var); //remove the obsolete var (no o or o is negative - not cleanedUp)
if (var["o"].isNull()) { //!variable.var.isNull() && || variable.order() <= 0
ppf("deleteObsolete remove var %s.%s (no order)\n", variable.pid()?variable.pid():"-", variable.id());
// vars.remove(var); //remove the obsolete var (no o or )
for (JsonArray::iterator it=vars.begin(); it!=vars.end(); ++it) if ((*it)["id"] == var["id"]) vars.remove(it); //use iterator to make .remove work!!!
}
return JsonObject(); //don't stop
});
Expand All @@ -609,17 +598,24 @@ void SysModModel::setup() {

void SysModModel::loop20ms() {

if (!cleanUpModelDone) { //do after all setups, which make all orders of found vars negative ...
cleanUpModelDone = true;
cleanUpModel(); //entire model, if order > 0, do not remove ro values
}

if (doWriteModel) {
ppf("Writing model to /model.json... (serializeConfig)\n");

// files->writeObjectToFile("/model.json", model);

cleanUpModel(false, true); //remove if var["o"] is negative (not cleanedUp) and remove ro values
walkThroughModel([](JsonObject parentVar, JsonObject var) {

Variable variable = Variable(var);

//remove ro values (ro vars cannot be deleted as SM uses these vars)
// remove if var is ro or table is instance table (exception here, values don't need to be saved)
if (parentVar["id"] == "instances" || variable.readOnly()) {// && !value().isNull())
// ppf("remove ro value %s.%s\n", variable.pid(), variable.id());
var.remove("value");
}

return JsonObject(); //don't stop
});

StarJson starJson("/model.json", FILE_WRITE); //open fileName for deserialize
//comment exclusions out in case of generating model.json for github
Expand Down Expand Up @@ -647,43 +643,6 @@ void SysModModel::loop1s() {
});
}

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

walkThroughModel([this, oPos, ro](JsonObject parentVar, JsonObject var) {

Variable parentVariable = Variable(parentVar);
Variable variable = Variable(var);
JsonArray vars = parentVariable.children();

// no cleanup of o in case of ro value removal
if (!ro) {
if (oPos) {
if (var["o"].isNull() || (variable.order() >= 0)) { //not set negative in initVar parentVariable.order() >= 0 &&
ppf("obsolete found %s.%s\n", variable.pid(), variable.id());
// if (!showObsolete)
// vars.remove(var); //remove the obsolete var (no o or )
}
variable.order( -variable.order()); //make it possitive
} else { //!oPos
if (var["o"].isNull() || variable.order() < 0) {
ppf("cleanUpModel remove var %s.%s (""o""<0)\n", variable.pid(), variable.id());
vars.remove(var); //remove the obsolete var (no o or o is negative - not cleanedUp)
}
}
}

//remove ro values (ro vars cannot be deleted as SM uses these vars)
// remove if var is ro or table is instance table (exception here, values don't need to be saved)
if (ro && (parentVar["id"] == "instances" || variable.readOnly())) {// && !value().isNull())
// ppf("remove ro value %s\n", variable.id());
var.remove("value");
}

return JsonObject(); //don't stop
});

}

Variable SysModModel::initVar(Variable parent, const char * id, const char * type, bool readOnly, const VarEvent &varEvent) {
const char * parentId = parent.var["id"];
if (!parentId) parentId = "m"; //m=module
Expand Down Expand Up @@ -719,15 +678,9 @@ Variable SysModModel::initVar(Variable parent, const char * id, const char * typ

if (var["ro"].isNull() || variable.readOnly() != readOnly) variable.readOnly(readOnly);

//set order. make order negative to check if not obsolete, see cleanUpModel
if (variable.order() >= 1000) //predefined! (modules) - positive as saved in model.json
variable.order( -variable.order()); //leave the order as is
else {
if (!parent.var.isNull() && Variable(parent).order() >= 0) // if checks on the parent already done so vars added later, e.g. controls, will be autochecked
variable.order( varCounter++); //redefine order
else
variable.order( -varCounter++); //redefine order
}
//set order
if (variable.order() < 1000) //predefined! (modules) - positive as saved in model.json
variable.order( varCounter++); //redefine order

//if varEvent, add it to the list
if (varEvent) {
Expand Down Expand Up @@ -825,6 +778,7 @@ JsonObject SysModModel::findModule(const char * pid, const char * id) {
if (pididFound) return moduleVar;
}

return JsonObject();
}

void SysModModel::findVars(const char * property, bool value, FindFun fun, JsonObject parentVar) {
Expand Down
18 changes: 7 additions & 11 deletions src/Sys/SysModModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,10 @@ class Variable {
bool readOnly() const {return var["ro"];}
void readOnly(bool value) {var["ro"] = value;}

JsonArray children() const {return var["n"];}
//children (n) of variable
JsonArray children();

void defaultOrder(int value) const {if (order() > -1000) order(- value); } //set default order (in range >=1000). Don't use auto generated order as order can be changed in the ui (WIP)
// void defaultOrder(int value) const {order(value); } //set default order (in range >=1000). Don't use auto generated order as order can be changed in the ui (WIP)

//recursively remove all value[rowNr] from children of var
void removeValuesForRow(uint8_t rowNr);
Expand All @@ -265,8 +266,6 @@ class Variable {

void preDetails();
void postDetails(uint8_t rowNr);
void preDetails2();
void postDetails2(uint8_t rowNr);

//checks if var has fun of type eventType implemented by calling it and checking result (for onUI on RO var, also onSetValue is called)
//onChange: sends dash var change to udp (if init), sets pointer if pointer var and run onChange
Expand Down Expand Up @@ -325,19 +324,19 @@ class Variable {
if (var["value"].is<JsonArray>()) {
JsonArray valueArray = valArray();
//set the right value in the array (if array did not contain values yet, all values before rownr are set to false)
bool notSame = true; //rowNr >= size
changed = true; //rowNr >= size

if (rowNr < valueArray.size())
notSame = valueArray[rowNr].isNull() || valueArray[rowNr].as<Type>() != value;
changed = valueArray[rowNr].isNull() || valueArray[rowNr].as<Type>() != value;

if (changed) {

if (notSame) {
// if (rowNr >= valueArray.size())
// 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());
JsonVariant value = var["value"];
web->addResponse(var, "value", value); //send the whole array to UI as response is in format value:<value> !!
changed = true;
}
}
else {
Expand Down Expand Up @@ -396,9 +395,6 @@ class SysModModel: public SysModule {
//adds a variable to the model
Variable initVar(Variable parent, const char * id, const char * type, bool readOnly = true, const VarEvent &varEvent = nullptr);

//scan all vars in the model and remove vars where var["o"] is negative or positive, if ro then remove ro values
void cleanUpModel(bool oPos = true, bool ro = false);

//sets the value of var with id
template <typename Type>
void setValue(const char * pid, const char * id, Type value, uint8_t rowNr = UINT8_MAX) {
Expand Down
4 changes: 2 additions & 2 deletions src/Sys/SysModNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void SysModNetwork::setup() {
return true;
}
case onChange:
variable.preDetails2();
variable.preDetails();
mdl->setValueRowNr = rowNr;

if (variable.value() == 0) {//manual
Expand Down Expand Up @@ -160,7 +160,7 @@ void SysModNetwork::setup() {
}});
}

variable.postDetails2(rowNr);
variable.postDetails(rowNr);
mdl->setValueRowNr = UINT8_MAX;

ethActive = false;
Expand Down
6 changes: 3 additions & 3 deletions src/Sys/SysModUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ class SysModUI: public SysModule {
//order: order%4 determines the column (WIP)
Variable initAppMod(Variable parent, const char * id, int order = 0) {
Variable variable = initVarAndValue<const char *>(parent, id, "appmod", (const char *)nullptr);
if (order) variable.defaultOrder(order + 1000);
if (order) variable.order(order); //add 1000 to be sure it will be predefined
return variable;
}
Variable initSysMod(Variable parent, const char * id, int order = 0) {
const Variable variable = initVarAndValue<const char *>(parent, id, "sysmod", (const char *)nullptr);
if (order) variable.defaultOrder(order + 1000);
if (order) variable.order(order); //add 1000 to be sure it will be predefined
return variable;
}
Variable initUserMod(Variable parent, const char * id, int order = 0) {
const Variable variable = initVarAndValue<const char *>(parent, id, "usermod", (const char *)nullptr);
if (order) variable.defaultOrder(order + 1000);
if (order) variable.order(order); //add 1000 to be sure it will be predefined
return variable;
}

Expand Down
1 change: 1 addition & 0 deletions src/Sys/SysModWeb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ void SysModWeb::serveJson(WebRequest *request) {
serializeInfo(root);
}
else {
root["state"] = ""; root["info"] = ""; //init otherwise result is {}
serializeState(root["state"]);
serializeInfo(root["info"]);
}
Expand Down
Loading

0 comments on commit 2f8c09b

Please sign in to comment.