Skip to content

Commit

Permalink
StarLink: Support multiple Json types (incl arrays) in UDP messages
Browse files Browse the repository at this point in the history
AppModDemo: add bri(ghtness) slider (for StarLink tests)

SysModInstances:
- StarMod type bugfix
- HandleNotifications: use setValueJV
- UpdateInstance: use setValueJV

SysModModel: add setValueJV (for JsonVariants)

SysModUI:
- processJson: use setValueJV
  • Loading branch information
ewoudwijma committed May 19, 2024
1 parent 32f292f commit 4eba8e1
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 26 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ lib_deps =
build_flags =
-D APP=StarBase
-D PIOENV=$PIOENV
-D VERSION=24051519 ; Date and time (GMT!), update at every commit!!
-D VERSION=24051908 ; Date and time (GMT!), update at every commit!!
-D CONFIG_ASYNC_TCP_USE_WDT=0
-D LFS_THREADSAFE ; enables use of semaphores in LittleFS driver
-D STARBASE_DEVMODE
Expand Down
12 changes: 12 additions & 0 deletions src/App/AppModDemo.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ class AppModDemo: public SysModule {
}});
currentVar["dash"] = true;

//logarithmic slider (10)
currentVar = ui->initSlider(parentVar, "bri", 10, 0, 255, false, [](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case f_UIFun:
ui->setLabel(var, "Brightness");
return true;
case f_ChangeFun: {
return true; }
default: return false;
}});
currentVar["log"] = true; //logarithmic
currentVar["dash"] = true; //these values override model.json???

ui->initText(parentVar, "textField", "text");

ui->initPin(parentVar, "blinkPin", blinkPin, false, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
Expand Down
25 changes: 6 additions & 19 deletions src/Sys/SysModInstances.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,6 @@ class SysModInstances:public SysModule {
UDPStarMessage starMessage;
byte *udpIn = (byte *)&starMessage;
instanceUDP.read(udpIn, packetSize);
starMessage.sysData.type = (strcmp(_INIT(TOSTRING(APP)), "StarBase")==0)?1:(strcmp(_INIT(TOSTRING(APP)), "StarLeds")==0)?2:3; //1=StarBase,2=StarLeds, 3=StarFork

updateInstance(starMessage);
}
Expand All @@ -417,21 +416,9 @@ class SysModInstances:public SysModule {
JsonDocument message;
deserializeJson(message, buffer);

//see also processJson for same code (function?)
if (message["value"].is<const char *>())
mdl->setValue(message["id"].as<const char *>(), JsonString(message["value"], JsonString::Copied));
else if (message["value"].is<Coord3D>()) //otherwise it will be treated as JsonObject and toJson / fromJson will not be triggered!!!
mdl->setValue(message["id"].as<const char *>(), message["value"].as<Coord3D>());
else if (message["value"].is<int>())
mdl->setValue(message["id"].as<const char *>(), message["value"].as<int>());
else if (message["value"].is<bool>())
mdl->setValue(message["id"].as<const char *>(), message["value"].as<bool>());
else
ppf("dev handleNotifications type unknown %s", buffer);
// else
// mdl->setValue(message["id"].as<const char *>(), message["value"]);

// ppf("handleNotifications i:%d json message %s\n", instanceUDP.remoteIP()[3], buffer);
ppf("handleNotifications i:%d json message %s (%s)\n", instanceUDP.remoteIP()[3], buffer, message.as<String>().c_str());

mdl->setValueJV(message["id"].as<const char *>(), message["value"]);

}
else {
Expand Down Expand Up @@ -635,11 +622,11 @@ class SysModInstances:public SysModule {
if (groupOfName(instance.name, group1) && groupOfName(mdl->getValue("name"), group2) && strcmp(group1, group2) == 0) {
for (JsonPair pair: newData.as<JsonObject>()) {
ppf("updateInstance sync from i:%s k:%s v:%s\n", instance.name, pair.key().c_str(), pair.value().as<String>().c_str());
// if (mdl->getValue(pair.key().c_str) != pair.value())
mdl->setValue(pair.key().c_str(), pair.value());

mdl->setValueJV(pair.key().c_str(), pair.value());
}
instance.jsonData = newData; // deepcopy: https://github.com/bblanchon/ArduinoJson/issues/1023
// ppf("updateInstance json ip:%d", instance.ip[3]);
ppf("updateInstance json ip:%d", instance.ip[3]);
print->printJson(" d:", instance.jsonData);
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/Sys/SysModModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,25 @@ class SysModModel:public SysModule {
//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);

//setValue for JsonVariants (extract the StarMod supported types)
JsonObject setValueJV(const char * id, JsonVariant value, unsigned8 rowNr = UINT8_MAX) {
if (value.is<JsonArray>()) {
uint8_t rowNr = 0;
// ppf(" %s is Array\n", value.as<String>().c_str);
JsonObject var;
for (JsonVariant el: value.as<JsonArray>()) {
var = setValueJV(id, el, rowNr++);
}
return var;
}
else if (value.is<const char *>())
return setValue(id, JsonString(value, JsonString::Copied), rowNr);
else if (value.is<Coord3D>()) //otherwise it will be treated as JsonObject and toJson / fromJson will not be triggered!!!
return setValue(id, value.as<Coord3D>(), rowNr);
else
return setValue(id, value, rowNr);
}

//sets the value of var with id
template <typename Type>
JsonObject setValue(const char * id, Type value, unsigned8 rowNr = UINT8_MAX) {
Expand Down
7 changes: 1 addition & 6 deletions src/Sys/SysModUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,7 @@ void SysModUI::processJson(JsonVariant json) {
if (rowNr != UINT8_MAX) web->getResponseObject()[mdl->varID(var)]["rowNr"] = rowNr;
}
else {
if (newValue.is<const char *>())
mdl->setValue(var, JsonString(newValue, JsonString::Copied), rowNr);
else if (newValue.is<Coord3D>()) //otherwise it will be treated as JsonObject and toJson / fromJson will not be triggered!!!
mdl->setValue(var, newValue.as<Coord3D>(), rowNr);
else
mdl->setValue(var, newValue, rowNr);
mdl->setValueJV(mdl->varID(var), newValue, rowNr);
}
// json.remove(key); //key / var["id"] processed we don't need the key in the response
}
Expand Down
4 changes: 4 additions & 0 deletions tools/misc.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@

git log v0.14.1-beta.30..HEAD --pretty=format:'%ad,%<(20)%an,%s' --date=short


Copy System Modules from MoonModules/StarLeds to upstream
(as should only be modified in upstream)
============================================================
Expand Down

0 comments on commit 4eba8e1

Please sign in to comment.