Skip to content

Commit

Permalink
A few changes
Browse files Browse the repository at this point in the history
SysModModel
- add Variable(pid,id) constructor
- add Variable.getOption

SysModFiles
- "r"/"w" -> FILE_READ / FILE_WRITE
- add writeObjectToFile
  • Loading branch information
ewowi committed Nov 27, 2024
1 parent 51b5dd8 commit a2dbade
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 82 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
"zlib": "^1.0.5"
},
"engines": {
"node": ">=20.0.0"
"node": ">=21.0.0"
}
}
28 changes: 13 additions & 15 deletions src/Sys/SysModFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ bool SysModFiles::nameToSeqNr(const char * fileName, size_t *seqNr, const char *

bool SysModFiles::readObjectFromFile(const char* path, JsonDocument* dest) {
// if (doCloseFile) closeFile();
File f = open(path, "r");
File f = open(path, FILE_READ);
if (!f) {
ppf("File %s open not successful\n", path);
return false;
Expand All @@ -253,20 +253,18 @@ bool SysModFiles::readObjectFromFile(const char* path, JsonDocument* dest) {
}
}

//candidate for deletion as taken over by StarJson
// bool SysModFiles::writeObjectToFile(const char* path, JsonDocument* dest) {
// File f = open(path, "w");
// if (f) {
// print->println(" success");
// serializeJson(*dest, f);
// f.close();
// filesChanged = true;
// return true;
// } else {
// print->println(" fail");
// return false;
// }
// }
bool SysModFiles::writeObjectToFile(const char* path, JsonDocument* dest) {
File f = open(path, FILE_WRITE);
if (f) {
serializeJson(*dest, f);
f.close();
filesChanged = true;
return true;
} else {
ppf("File %s open not successful\n", path);
return false;
}
}

void SysModFiles::removeFiles(const char * filter, bool reverse) {
File root = LittleFS.open("/");
Expand Down
3 changes: 1 addition & 2 deletions src/Sys/SysModFiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ class SysModFiles: public SysModule {

//write json into file
//name is copied from WLED but better to call it readJsonFrom file
//candidate for deletion as taken over by StarJson
// bool writeObjectToFile(const char* path, JsonDocument* dest);
bool writeObjectToFile(const char* path, JsonDocument* dest);

//remove files meeting filter condition, if no filter, all, if reverse then all but filter
void removeFiles(const char * filter = nullptr, bool reverse = false);
Expand Down
22 changes: 11 additions & 11 deletions src/Sys/SysModInstances.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class SysModInstances:public SysModule {
//extract the variable from insVariable.id()
char pid[32]; strlcpy(pid, insVariable.id() + 3, sizeof(pid)); //+3 : remove ins
char * id = strtok(pid, "_"); if (id != nullptr ) {strlcpy(pid, id, sizeof(pid)); id = strtok(nullptr, "_");} //split pid and id
Variable variable = Variable(mdl->findVar(pid, id));
Variable variable = Variable(pid, id);
switch (eventType) { //varEvent
case onSetValue:
//should not trigger onChange
Expand Down Expand Up @@ -436,7 +436,7 @@ class SysModInstances:public SysModule {
// Serial.println();

ppf("instances handleNotifications %d\n", notifierUdp.remoteIP()[3]);
for (JsonObject childVar: Variable(mdl->findVar("Instances", "instances")).children())
for (JsonObject childVar: Variable("Instances", "instances").children())
Variable(childVar).triggerEvent(onSetValue); //set the value (WIP) ); //rowNr //instance - instances.begin()

web->recvUDPCounter++;
Expand Down Expand Up @@ -508,7 +508,7 @@ class SysModInstances:public SysModule {
if (!message["id"].isNull() && !message["value"].isNull()) {
ppf("handleNotifications i:%d json message %.*s l:%d\n", instanceUDP.remoteIP()[3], packetSize, buffer, packetSize);

Variable(mdl->findVar(message["pid"].as<const char *>(), message["id"].as<const char *>())).setValueJV(message["value"]);
Variable(message["pid"].as<const char *>(), message["id"].as<const char *>()).setValueJV(message["value"]);
}
}
}
Expand Down Expand Up @@ -537,13 +537,13 @@ class SysModInstances:public SysModule {
}
if (erased) {
ppf("instances remove inactive instances\n");
for (JsonObject childVar: Variable(mdl->findVar("Instances", "instances")).children())
for (JsonObject childVar: Variable("Instances", "instances").children())
Variable(childVar).triggerEvent(onSetValue); //set the value (WIP)); //no rowNr so all rows updated

//tbd: pubsub mechanism
//LEDs specific
Variable(mdl->findVar("DDP", "instance")).triggerEvent(onUI); //rebuild options
// Variable(mdl->findVar("Artnet", "artInst")).triggerEvent(onUI); //rebuild options
Variable("DDP", "instance").triggerEvent(onUI); //rebuild options
// Variable("Artnet", "artInst").triggerEvent(onUI); //rebuild options
}
}

Expand Down Expand Up @@ -783,7 +783,7 @@ class SysModInstances:public SysModule {
id = strtok(nullptr, "."); //the rest after .
}

Variable(mdl->findVar(pid, id)).setValueJV(pair.value());
Variable(pid, id).setValueJV(pair.value());
}
instance.jsonData = newData; // deepcopy: https://github.com/bblanchon/ArduinoJson/issues/1023
// ppf("updateInstance json ip:%d", instance.ip[3]);
Expand All @@ -810,7 +810,7 @@ class SysModInstances:public SysModule {

// ppf("updateInstance updRow\n");

for (JsonObject childVar: Variable(mdl->findVar("Instances", "instances")).children())
for (JsonObject childVar: Variable("Instances", "instances").children())
Variable(childVar).triggerEvent(onSetValue); //set the value (WIP)); //rowNr instance - instances.begin()

//tbd: now done for all rows, should be done only for updated rows!
Expand All @@ -825,14 +825,14 @@ class SysModInstances:public SysModule {

//tbd: pubsub mechanism
//LEDs specific
Variable(mdl->findVar("DDP", "instance")).triggerEvent(onUI); //rebuild options
// Variable(mdl->findVar(Artnet", "artInst")).triggerEvent(onUI); //rebuild options
Variable("DDP", "instance").triggerEvent(onUI); //rebuild options
// Variable(Artnet", "artInst").triggerEvent(onUI); //rebuild options

// ui->processOnUI("instances");
//run though it sorted to find the right rowNr
// for (std::vector<InstanceInfo>::iterator instance=instances.begin(); instance!=instances.end(); ++instance) {
// if (instance->ip == messageIP) {
for (JsonObject childVar: Variable(mdl->findVar("Instances", "instances")).children()) {
for (JsonObject childVar: Variable("Instances", "instances").children()) {
Variable(childVar).triggerEvent(onSetValue); //set the value (WIP)); //no rowNr, update all
}
// }
Expand Down
36 changes: 29 additions & 7 deletions src/Sys/SysModModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@
#include "SysModUI.h"
#include "SysModInstances.h"

Variable::Variable() {
var = JsonObject(); //undefined variable
}

Variable::Variable(JsonObject var) {
this->var = var;
}

Variable::Variable(const char *pid, const char *id) {
var = mdl->findVar(pid, id);
}

String Variable::valueString(uint8_t rowNr) {
if (rowNr == UINT8_MAX)
return value().as<String>();
Expand Down Expand Up @@ -343,6 +355,16 @@
web->getResponseObject()[pidid].remove("options");
}

void Variable::getOption(char *option, uint8_t index) {
char pidid[64];
print->fFormat(pidid, sizeof(pidid), "%s.%s", var["pid"].as<const char *>(), var["id"].as<const char *>());
bool optionsExisted = !web->getResponseObject()[pidid]["options"].isNull();
JsonArray options = getOptions();
strlcpy(option, options[index], 64);
if (!optionsExisted)
clearOptions(); //if created here then also remove
}

//find options text in a hierarchy of options
void Variable::findOptionsText(uint8_t value, char * groupName, char * optionName) {
uint8_t startValue = 0;
Expand Down Expand Up @@ -560,7 +582,7 @@ void SysModModel::loop20ms() {

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

StarJson starJson("/model.json", "w"); //open fileName for deserialize
StarJson starJson("/model.json", FILE_WRITE); //open fileName for deserialize
//comment exclusions out in case of generating model.json for github
starJson.addExclusion("fun");
starJson.addExclusion("dash");
Expand All @@ -576,7 +598,7 @@ void SysModModel::loop20ms() {
}

void SysModModel::loop1s() {
mdl->walkThroughModel([](JsonObject parentVar, JsonObject var) {
walkThroughModel([](JsonObject parentVar, JsonObject var) {
Variable(var).triggerEvent(onLoop1s);
return JsonObject(); //don't stop
});
Expand All @@ -590,7 +612,7 @@ void SysModModel::cleanUpModel(Variable parent, bool oPos, bool ro) {
else
vars = Variable(parent).children();

bool showObsolete = mdl->getValue("Model", "showObsolete");
bool showObsolete = getValue("Model", "showObsolete");
for (JsonArray::iterator varV=vars.begin(); varV!=vars.end(); ++varV) {
// for (JsonVariant varV : vars) {
if (varV->is<JsonObject>()) {
Expand Down Expand Up @@ -633,14 +655,14 @@ void SysModModel::cleanUpModel(Variable parent, bool oPos, bool ro) {
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
JsonObject var = mdl->findVar(parentId, id);
JsonObject var = findVar(parentId, id);
Variable variable = Variable(var);

//create new var
if (var.isNull()) {
// ppf("initVar new %s: %s.%s\n", type, parentId, id); //parentId not null otherwise crash
if (parent.var.isNull()) {
JsonArray vars = mdl->model->as<JsonArray>();
JsonArray vars = model->as<JsonArray>();
var = vars.add<JsonObject>();
} else {
if (parent.var["n"].isNull()) parent.var["n"].to<JsonArray>(); //TO!!! if parent exist and no "n" array, create it
Expand Down Expand Up @@ -670,9 +692,9 @@ Variable SysModModel::initVar(Variable parent, const char * id, const char * typ
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( mdl->varCounter++); //redefine order
variable.order( varCounter++); //redefine order
else
variable.order( -mdl->varCounter++); //redefine order
variable.order( -varCounter++); //redefine order
}

//if varEvent, add it to the list
Expand Down
7 changes: 5 additions & 2 deletions src/Sys/SysModModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,10 @@ class Variable {

JsonObject var;

Variable() {this->var = JsonObject();} //undefined variable
explicit Variable(JsonObject var) {this->var = var;}
//constructors
Variable(); //undefined variable
Variable(JsonObject var);
Variable(const char *pid, const char *id);

//core methods
const char *pid() const {return var["pid"];}
Expand Down Expand Up @@ -254,6 +256,7 @@ class Variable {
//return the options from onUI (don't forget to clear responseObject)
JsonArray getOptions();
void clearOptions();
void getOption(char *option, uint8_t index);

//find options text in a hierarchy of options
void findOptionsText(uint8_t value, char * groupName, char * optionName);
Expand Down
2 changes: 1 addition & 1 deletion src/Sys/SysModPins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void SysModPins::loop20ms() {
if (pinsChanged) {
pinsChanged = false;

for (JsonObject childVar: Variable(mdl->findVar("Pins", "pins")).children())
for (JsonObject childVar: Variable("Pins", "pins").children())
Variable(childVar).triggerEvent(onSetValue); //set the value (WIP)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Sys/SysModSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ void SysModSystem::loop() {
void SysModSystem::loop10s() {
//heartbeat
if (sys->now < 60000)
ppf("❤️ %s\n", net->localIP().toString().c_str());
ppf("❤️ http://%s\n", net->localIP().toString().c_str());
else
ppf("❤️");
}
Expand Down
7 changes: 4 additions & 3 deletions src/Sys/SysModWeb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void SysModWeb::loop20ms() {
clientsChanged = false;

// ppf("SysModWeb clientsChanged\n");
for (JsonObject childVar: Variable(mdl->findVar("Web", "clients")).children())
for (JsonObject childVar: Variable("Web", "clients").children())
Variable(childVar).triggerEvent(onSetValue); //set the value (WIP)
}

Expand Down Expand Up @@ -490,7 +490,7 @@ void SysModWeb::serveUpload(WebRequest *request, const String& fileName, size_t
finalname = '/' + finalname; // prepend slash if missing
}

request->_tempFile = files->open(finalname.c_str(), "w");
request->_tempFile = files->open(finalname.c_str(), FILE_WRITE);
// DEBUG_PRINT("Uploading ");
// DEBUG_PRINTLN(finalname);
// if (finalname.equals("/presets.json")) presetsModifiedTime = toki.second();
Expand Down Expand Up @@ -608,7 +608,7 @@ void SysModWeb::jsonHandler(WebRequest *request, JsonVariant json) {
if (responseObject.size()) { //responseObject set by processJson e.g. onUI

char resStr[200];
serializeJson(responseObject, resStr, 200);
serializeJson(responseObject, resStr, sizeof(resStr));
ppf("processJsonUrl response %s\n", resStr);
request->send(200, "application/json", resStr);

Expand Down Expand Up @@ -659,6 +659,7 @@ bool SysModWeb::captivePortal(WebRequest *request)
JsonDocument * SysModWeb::getResponseDoc() {
// ppf("response wsevent core %d %s\n", xPortGetCoreID(), pcTaskGetTaskName(nullptr));

// return responseDocLoopTask;
return strncmp(pcTaskGetTaskName(nullptr), "loopTask", 9) == 0?responseDocLoopTask:responseDocAsyncTCP;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sys/SysStarJson.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class StarJson {

public:

explicit StarJson(const char * path, const char * mode = "r");
explicit StarJson(const char * path, const char * mode = FILE_READ);

~StarJson();

Expand Down
2 changes: 1 addition & 1 deletion src/SysModules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void SysModules::setup() {
}

//delete Modules values if nr of modules has changed (new values created using module defaults)
for (JsonObject childVar: Variable(mdl->findVar("Modules", "Modules")).children()) {
for (JsonObject childVar: Variable("Modules", "Modules").children()) {
Variable childVariable = Variable(childVar);
if (!childVariable.value().isNull() && childVariable.valArray().size() != modules.size()) {
ppf("Modules clear (%s.%s %s) %d %d\n", childVariable.pid(), childVariable.id(), childVariable.valueString().c_str(), modules.size(), childVariable.valArray().size());
Expand Down
2 changes: 1 addition & 1 deletion src/User/UserModE131.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class UserModE131:public SysModule {
variable.setComment("First channel");
return true;
case onChange:
for (JsonObject childVar: Variable(mdl->findVar("E131", "patches")).children())
for (JsonObject childVar: Variable("E131", "patches").children())
Variable(childVar).triggerEvent(onSetValue); //set the value (WIP)
return true;
default: return false;
Expand Down
6 changes: 3 additions & 3 deletions src/User/UserModLive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ static float _time(float j) {
}

void UserModLive::loop1s() {
for (JsonObject childVar: Variable(mdl->findVar("LiveScripts", "scripts")).children())
for (JsonObject childVar: Variable("LiveScripts", "scripts").children())
Variable(childVar).triggerEvent(onSetValue); //set the value (WIP)
}

Expand All @@ -369,10 +369,10 @@ static float _time(float j) {
uint8_t UserModLive::compile(const char * fileName, const char * post) {
ppf("live compile n:%s o:%s \n", fileName, this->fileName);

File f = files->open(fileName, "r");
File f = files->open(fileName, FILE_READ);
if (!f)
{
ppf("UserModLive setup script open %s for %s failed\n", fileName, "r");
ppf("UserModLive setup script open %s for %s failed\n", fileName, FILE_READ);
return UINT8_MAX;
}
else {
Expand Down
Loading

0 comments on commit a2dbade

Please sign in to comment.