Skip to content

Commit

Permalink
Bugfixes, stability and security improvements
Browse files Browse the repository at this point in the history
index.js
- combine select and derived types (pin and ip)
- receiveData: addRow: test if id and rowNr is present
- showHideColumn: test if column exists

AppModFixture: projectAndMap: remove first (WIP)

AppLeds.h:
- XY(Z) uint16_t
- destructor: doMap true

AppModLeds: loop only when !leds->doMap

SysModModel:
- cleanUpModel: bugfix: explicitly test for insTbl
- setValue: always store in model, also ro !!!

SysModUI
- add initIP (WIP)
- initVarAndUpdate: always store in model, also ro !!!
- initVar: set id, type and ro fix

SysModWeb
- add ws send/recv info

ddpInst, artInst and sma using initIP
  • Loading branch information
ewowi committed Feb 29, 2024
1 parent b1d0795 commit 4f179dc
Show file tree
Hide file tree
Showing 14 changed files with 1,776 additions and 1,761 deletions.
42 changes: 18 additions & 24 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,7 @@ function createHTML(json, parentNode = null, rowNr = UINT8_MAX) {
varNode = cE("th");
varNode.innerText = initCap(variable.id); //label uiFun response can change it

} else if (variable.type == "select") {

if (variable.ro) { //e.g. for reset/restart reason: do not show a select but only show the selected option
varNode = cE("span");
}
else {
varNode = cE("select");
varNode.addEventListener('change', (event) => {console.log("select change", event);sendValue(event.target);});
}

} else if (variable.type == "pin") {
} else if (variable.type == "select" || variable.type == "pin" || variable.type == "ip") {

if (variable.ro) { //e.g. for reset/restart reason: do not show a select but only show the selected option
varNode = cE("span");
Expand Down Expand Up @@ -558,19 +548,21 @@ function receiveData(json) {
else if (key == "addRow") { //update the row of a table
console.log("receiveData", key, value);

let tableId = value.id;
let tableVar = findVar(tableId);
let rowNr = value.rowNr;
if (value.id && value.rowNr) {
let tableId = value.id;
let rowNr = value.rowNr;

let tableNode = gId(tableId);

let tbodyNode = tableNode.querySelector("tbody");
let tableVar = findVar(tableId);
let tableNode = gId(tableId);
let tbodyNode = tableNode.querySelector("tbody");

console.log("addRow ", tableVar, tableNode, rowNr);
console.log("addRow ", tableVar, tableNode, rowNr);

let newRowNr = tbodyNode.querySelectorAll("tr").length;
let newRowNr = tbodyNode.querySelectorAll("tr").length;

genTableRowHTML(tableVar, tableNode, newRowNr);
genTableRowHTML(tableVar, tableNode, newRowNr);
}
else console.log("dev receiveData addRow no id and/or rowNr specified", key, value);

} else if (key == "delRow") { //update the row of a table

Expand Down Expand Up @@ -844,7 +836,7 @@ function changeHTML(variable, commandJson, rowNr = UINT8_MAX) {
if (commandJson.value) node.value = commandJson.value; //else the id / label is used as button label
}
else if (node.className == "coord3D") {
console.log("chHTML value coord3D", node, commandJson.value, rowNr);
// console.log("chHTML value coord3D", node, commandJson.value, rowNr);

if (commandJson.value) {
//tbd: support Coord3D as array (now only objects work)
Expand Down Expand Up @@ -874,7 +866,7 @@ function changeHTML(variable, commandJson, rowNr = UINT8_MAX) {
console.log(" dev value coord3D value not object[x,y,z]", variable.id, node.id, commandJson.value);
}
}
else if (node.className == "select") {
else if (node.className == "select" || node.className == "pin" || node.className == "ip") {
if (variable.ro) {
var index = 0;
if (variable.options && commandJson.value != null) { // not always the case e.g. data / table / uiFun. Then value set if uiFun returns
Expand Down Expand Up @@ -1283,9 +1275,11 @@ function setInstanceTableColumns() {

function showHideColumn(colNr, doHide) {
// console.log("showHideColumn", thead.parentNode.parentNode, colNr, doHide);
thead.querySelector("tr").childNodes[colNr].hidden = doHide;
if (colNr < thead.querySelector("tr").childNodes.length)
thead.querySelector("tr").childNodes[colNr].hidden = doHide;
for (let trNode of tbody.querySelectorAll("tr"))
trNode.childNodes[colNr].hidden = doHide;
if (colNr < trNode.childNodes.length)
trNode.childNodes[colNr].hidden = doHide;
}

// console.log("setInstanceTableColumns", tbl, thead, tbody);
Expand Down
708 changes: 353 additions & 355 deletions src/App/AppFixture.cpp

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions src/App/AppLeds.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ class Leds {

std::vector<std::vector<uint16_t>> mappingTable;

uint16_t XY( uint8_t x, uint8_t y) {
return x + y * size.x;
}
uint16_t XYZ( uint8_t x, uint8_t y, uint8_t z) {
return x + y * size.x + z * size.x * size.y;
uint16_t XY(uint16_t x, uint16_t y) {
return XYZ(x, y, 0);
}
uint16_t XYZ(Coord3D coord) {
return coord.x + coord.y * size.x + coord.z * size.x * size.y;
return XYZ(coord.x, coord.y, coord.z);
}
uint16_t XYZ(uint16_t x, uint16_t y, uint16_t z) {
return x + y * size.x + z * size.x * size.y;
}

uint16_t indexVLocal = 0; //set in operator[], used by operator=
Expand All @@ -125,6 +125,7 @@ class Leds {
~Leds() {
USER_PRINTF("Leds[%d] destructor\n", UINT8_MAX);
fadeToBlackBy(100);
doMap = true; // so loop is not running while deleting
for (std::vector<std::vector<uint16_t>> ::iterator physMap=mappingTable.begin(); physMap!=mappingTable.end(); ++physMap)
physMap->clear();
mappingTable.clear();
Expand Down
10 changes: 6 additions & 4 deletions src/App/AppModLeds.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,12 @@ class AppModLeds:public SysModule {
// for (std::vector<Leds *>::iterator leds=fixture.ledsList.begin(); leds!=fixture.ledsList.end(); ++leds) {
uint8_t rowNr = 0;
for (Leds *leds: fixture.ledsList) {
// USER_PRINTF(" %d %d,%d,%d - %d,%d,%d (%d,%d,%d)", leds->fx, leds->startPos.x, leds->startPos.y, leds->startPos.z, leds->endPos.x, leds->endPos.y, leds->endPos.z, leds->size.x, leds->size.y, leds->size.z );
mdl->contextRowNr = rowNr++;
effects.loop(*leds);
mdl->contextRowNr = UINT8_MAX;
if (!leds->doMap) { // don't run effect while remapping
// USER_PRINTF(" %d %d,%d,%d - %d,%d,%d (%d,%d,%d)", leds->fx, leds->startPos.x, leds->startPos.y, leds->startPos.z, leds->endPos.x, leds->endPos.y, leds->endPos.z, leds->size.x, leds->size.y, leds->size.z );
mdl->contextRowNr = rowNr++;
effects.loop(*leds);
mdl->contextRowNr = UINT8_MAX;
}
}

FastLED.show();
Expand Down
5 changes: 2 additions & 3 deletions src/Sys/SysModModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,8 @@ void SysModModel::cleanUpModel(JsonObject parent, bool oPos, bool ro) {
}

//remove ro values (ro vars cannot be deleted as SM uses these vars)
// remove if var is ro or table of var is ro (ro table can have non ro vars e.g. instance table)
if (ro && ((parent["type"] == "table" && varRO(parent)) || varRO(var))) {// && !var["value"].isNull())
// if (ro && varRO(var)) {// && !var["value"].isNull())
// remove if var is ro or table is instance table (exception here, values don't need to be saved)
if (ro && (parent["id"] == "insTbl" || varRO(var))) {// && !var["value"].isNull())
USER_PRINTF("remove ro value %s\n", varID(var));
var.remove("value");
}
Expand Down
12 changes: 0 additions & 12 deletions src/Sys/SysModModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,6 @@ class SysModModel:public SysModule {

template <typename Type>
JsonObject setValue(JsonObject var, Type value, uint8_t rowNr = UINT8_MAX) {
if (varRO(var)) {
// print->printJson("setValue ro to ws", web->getResponseObject());
web->addResponse(var["id"], "value", value, rowNr);
callChangeFun(var, rowNr); //always call change fun, if present
return var;
}
else
return setValueNoROCheck(var, value, rowNr);
}

template <typename Type>
JsonObject setValueNoROCheck(JsonObject var, Type value, uint8_t rowNr = UINT8_MAX) {

bool changed = false;

Expand Down
6 changes: 3 additions & 3 deletions src/Sys/SysModUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,19 @@ JsonObject SysModUI::initVar(JsonObject parent, const char * id, const char * ty
var = parent["n"].add<JsonObject>();
// serializeJson(model, Serial);Serial.println();
}
var["id"] = (char *)id; //JsonString(id, JsonString::Copied);
var["id"] = JsonString(id, JsonString::Copied);
}
// else {
// USER_PRINTF("initVar Var %s->%s already defined\n", modelParentId, id);
// }

if (!var.isNull()) {
if (var["type"].isNull() || var["type"] != type) {
var["type"] = (char *)type;//JsonString(type, JsonString::Copied);
var["type"] = JsonString(type, JsonString::Copied);
print->printJson("initVar set type", var);
}

if (mdl->varRO(var) != readOnly) mdl->varRO(var, readOnly);
if (var["ro"].isNull() || mdl->varRO(var) != readOnly) mdl->varRO(var, readOnly);

//set order. make order negative to check if not obsolete, see cleanUpModel
if (mdl->varOrder(var) >= 1000) //predefined! (modules)
Expand Down
14 changes: 7 additions & 7 deletions src/Sys/SysModUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "SysModule.h"
#include "SysModPrint.h"
#include "SysModModel.h"
#include "SysModules.h" //isConnected

enum FunIDs
{
Expand Down Expand Up @@ -130,7 +129,11 @@ class SysModUI:public SysModule {
return initVarAndUpdate<int>(parent, id, "select", value, 0, 0, readOnly, varFun);
}

//WIP
JsonObject initIP(JsonObject parent, const char * id, int value = UINT16_MAX, bool readOnly = false, VarFun varFun = nullptr) {
return initVarAndUpdate<int>(parent, id, "ip", value, 0, 255, readOnly, varFun);
}

//WIP pointer values
JsonObject initSelect(JsonObject parent, const char * id, uint8_t * value = nullptr, bool readOnly = false, VarFun varFun = nullptr) {
return initVarAndUpdate<uint8_t>(parent, id, "select", value, 0, 0, readOnly, varFun);
}
Expand All @@ -143,7 +146,7 @@ class SysModUI:public SysModule {
return initVarAndUpdate<const char *>(parent, id, "url", value, 0, 0, readOnly, varFun);
}

//WIP
//WIP pointer values
template <typename Type>
JsonObject initVarAndUpdate(JsonObject parent, const char * id, const char * type, Type * value, int min = 0, int max = 255, bool readOnly = true, VarFun varFun = nullptr) {
// JsonObject var = initVar(parent, id, type, readOnly, varFun);
Expand Down Expand Up @@ -180,10 +183,7 @@ class SysModUI:public SysModule {
valueFunExists = varFun(var, mdl->contextRowNr, f_ValueFun);
}
if (!valueFunExists) { //setValue provided (if not null)
if (mdl->varRO(var) && !mdls->isConnected) {
mdl->setValueNoROCheck(var, value, mdl->contextRowNr); //does changefun if needed, if var in table, update the table row
} else
mdl->setValue(var, value, mdl->contextRowNr); //does changefun if needed, if var in table, update the table row
mdl->setValue(var, value, mdl->contextRowNr); //does changefun if needed, if var in table, update the table row
}
}
else { //do changeFun on existing value
Expand Down
32 changes: 28 additions & 4 deletions src/Sys/SysModWeb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ JsonDocument * SysModWeb::responseDocLoopTask = nullptr;
JsonDocument * SysModWeb::responseDocAsyncTCP = nullptr;
bool SysModWeb::clientsChanged = false;

unsigned long SysModWeb::sendDataWsCounter = 0;
uint8_t SysModWeb::sendDataWsCounter = 0;
uint16_t SysModWeb::sendDataWsTBytes = 0;
uint16_t SysModWeb::sendDataWsBBytes = 0;
uint8_t SysModWeb::recvDataWsCounter = 0;
uint16_t SysModWeb::recvDataWsBytes = 0;

SemaphoreHandle_t SysModWeb::wsMutex = xSemaphoreCreateMutex();

Expand Down Expand Up @@ -131,9 +135,18 @@ void SysModWeb::setup() {
default: return false;
}});

ui->initText(parentVar, "wsCounter", nullptr, 16, true, [](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
ui->initText(parentVar, "wsSend", nullptr, 16, true, [](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
case f_UIFun:
ui->setComment(var, "#web socket calls");
ui->setLabel(var, "WS Send Info");
// ui->setComment(var, "web socket calls");
return true;
default: return false;
}});

ui->initText(parentVar, "wsRecv", nullptr, 16, true, [](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
case f_UIFun:
ui->setLabel(var, "WS Recv Info");
// ui->setComment(var, "web socket calls");
return true;
default: return false;
}});
Expand Down Expand Up @@ -166,8 +179,13 @@ void SysModWeb::loop1s() {
for (JsonObject childVar: mdl->varN("clTbl"))
ui->callVarFun(childVar, UINT8_MAX, f_ValueFun);

mdl->setUIValueV("wsCounter", "%lu /s", sendDataWsCounter);
mdl->setUIValueV("wsSend", "#: %d /s T: %d /Bs B:%d /Bs", sendDataWsCounter, sendDataWsTBytes, sendDataWsBBytes);
sendDataWsCounter = 0;
sendDataWsTBytes = 0;
sendDataWsBBytes = 0;
mdl->setUIValueV("wsRecv", "#: %d /s %d /Bs", recvDataWsCounter, recvDataWsBytes);
recvDataWsCounter = 0;
recvDataWsBytes = 0;

sendResponseObject(); //this sends all the loopTask responses once per second !!!
}
Expand Down Expand Up @@ -256,6 +274,8 @@ void SysModWeb::wsEvent(WebSocket * ws, WebClient * client, AwsEventType type, v
String msg = "";
// USER_PRINT_Async(" info %d %d %d=%d? %d %d\n", info->final, info->index, info->len, len, info->opcode, data[0]);
if (info->final && info->index == 0 && info->len == len) { //not multipart
recvDataWsCounter++;
recvDataWsBytes+=len;
printClient("WS event data", client);
// the whole message is in a single frame and we got all of its data (max. 1450 bytes)
if (info->opcode == WS_TEXT)
Expand Down Expand Up @@ -397,6 +417,10 @@ void SysModWeb::sendDataWs(std::function<void(AsyncWebSocketMessageBuffer *)> fi
if ((!isBinary || loopClient->queueLength() <= 3)) {
isBinary?loopClient->binary(wsBuf): loopClient->text(wsBuf);
sendDataWsCounter++;
if (isBinary)
sendDataWsBBytes+=len;
else
sendDataWsTBytes+=len;
}
}
else {
Expand Down
6 changes: 5 additions & 1 deletion src/Sys/SysModWeb.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ class SysModWeb:public SysModule {
static JsonDocument *responseDocLoopTask;
static JsonDocument *responseDocAsyncTCP;

static unsigned long sendDataWsCounter;
static uint8_t sendDataWsCounter;
static uint16_t sendDataWsTBytes;
static uint16_t sendDataWsBBytes;
static uint8_t recvDataWsCounter;
static uint16_t recvDataWsBytes;
};

static SysModWeb *web;
2 changes: 1 addition & 1 deletion src/User/UserModArtNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class UserModArtNet:public SysModule {
parentVar = ui->initUserMod(parentVar, name);
if (parentVar["o"] > -1000) parentVar["o"] = -3100; //set default order. Don't use auto generated order as order can be changed in the ui (WIP)

ui->initSelect(parentVar, "artInst", UINT16_MAX, false, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
ui->initIP(parentVar, "artInst", UINT16_MAX, false, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun

case f_UIFun: {
ui->setLabel(var, "Instance");
Expand Down
2 changes: 1 addition & 1 deletion src/User/UserModDDP.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class UserModDDP:public SysModule {
parentVar = ui->initUserMod(parentVar, name);
if (parentVar["o"] > -1000) parentVar["o"] = -3000; //set default order. Don't use auto generated order as order can be changed in the ui (WIP)

ui->initSelect(parentVar, "ddpInst", UINT16_MAX, false, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
ui->initIP(parentVar, "ddpInst", UINT16_MAX, false, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun

case f_UIFun: {
ui->setLabel(var, "Instance");
Expand Down
4 changes: 3 additions & 1 deletion src/User/UserModInstances.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class UserModInstances:public SysModule {
JsonObject currentVar;

//default is 0 / None
currentVar = ui->initSelect(parentVar, "sma", 0, false, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
currentVar = ui->initIP(parentVar, "sma", 0, false, [this](JsonObject var, uint8_t rowNr, uint8_t funType) { switch (funType) { //varFun
case f_UIFun: {
ui->setLabel(var, "Sync Master");
ui->setComment(var, "Instance to sync from");
Expand Down Expand Up @@ -501,6 +501,7 @@ class UserModInstances:public SysModule {

ui->callVarFun("ddpInst", UINT8_MAX, f_UIFun);
ui->callVarFun("artInst", UINT8_MAX, f_UIFun);
ui->callVarFun("sma", UINT8_MAX, f_UIFun);
}
else
++instance;
Expand Down Expand Up @@ -672,6 +673,7 @@ class UserModInstances:public SysModule {

ui->callVarFun("ddpInst", UINT8_MAX, f_UIFun); //UiFun as select changes
ui->callVarFun("artInst", UINT8_MAX, f_UIFun);
ui->callVarFun("sma", UINT8_MAX, f_UIFun);

// ui->processUiFun("insTbl");
//run though it sorted to find the right rowNr
Expand Down
Loading

0 comments on commit 4f179dc

Please sign in to comment.