Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
General
- replace setValueLossy by setValueV

AppModPreview and AppModPins
- use sendDataWs for canvas data

SysModModel: setValue
- readonly values, ws directly

SysModWeb
- sendDataWs: overload add buf function parameter + some improvements
- refactor addResponse using template
- sendDataWsCounter
- remove wsEvent2
- setup captivePortal (WIP)
  • Loading branch information
ewowi committed Jan 25, 2024
1 parent dedb982 commit 6b114f4
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 264 deletions.
10 changes: 5 additions & 5 deletions src/App/AppModLeds.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class AppModLeds:public SysModule {
// ui needs to load the file also initially
char fileName[32] = "";
if (files->seqNrToName(fileName, var["value"])) {
web->addResponse("pview", "file", fileName);
web->addResponse("pview", "file", JsonString(fileName, JsonString::Copied));
}
}, [this](JsonObject var, uint8_t) { //chFun

Expand All @@ -180,7 +180,7 @@ class AppModLeds:public SysModule {
responseDoc->clear(); //needed for deserializeJson?
JsonVariant responseVariant = responseDoc->as<JsonVariant>();

web->addResponse("pview", "file", fileName);
web->addResponse("pview", "file", JsonString(fileName, JsonString::Copied));
web->sendDataWs(responseVariant);
print->printJson("fixture chFun send ws done", responseVariant); //during server startup this is not send to a client, so client refresh should also trigger this
}
Expand All @@ -189,13 +189,13 @@ class AppModLeds:public SysModule {
ui->initText(tableVar, "dimensions", nullptr, 32, true, [](JsonObject var) { //uiFun
char details[32] = "";
print->fFormat(details, sizeof(details)-1, "P:%dx%dx%d V:%dx%dx%d", ledsV.widthP, ledsV.heightP, ledsV.depthP, ledsV.widthV, ledsV.heightV, ledsV.depthV);
web->addResponse(var["id"], "value", details);
web->addResponse(var["id"], "value", JsonString(details, JsonString::Copied));
});

ui->initText(tableVar, "nrOfLeds", nullptr, 32, true, [](JsonObject var) { //uiFun
char details[32] = "";
print->fFormat(details, sizeof(details)-1, "P:%d V:%d", ledsV.nrOfLedsP, ledsV.nrOfLedsV);
web->addResponse(var["id"], "value", details);
web->addResponse(var["id"], "value", JsonString(details, JsonString::Copied));
web->addResponseV(var["id"], "comment", "Max %d", NUM_LEDS_Max);
});

Expand Down Expand Up @@ -422,7 +422,7 @@ class AppModLeds:public SysModule {
} //loop

void loop1s() {
mdl->setValueLossy("realFps", "%lu /s", frameCounter);
mdl->setValueV("realFps", "%lu /s", frameCounter);
frameCounter = 0;
}

Expand Down
53 changes: 15 additions & 38 deletions src/App/AppModPreview.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,52 +30,29 @@ class AppModPreview:public SysModule {
// web->addResponse(var["id"], "comment", "Click to enlarge");
}, nullptr, [](JsonObject var, uint8_t rowNr) { //loopFun

xSemaphoreTake(web->wsMutex, portMAX_DELAY);

SysModWeb::ws->cleanupClients();
if (web->ws->count()) {
var["interval"] = max(ledsV.nrOfLedsP * web->ws->count()/200, 16U)*10; //interval in ms * 10, not too fast //from cs to ms

web->sendDataWs([](AsyncWebSocketMessageBuffer * wsBuf) {
uint8_t* buffer;
AsyncWebSocketMessageBuffer * wsBuf = SysModWeb::ws->makeBuffer(ledsV.nrOfLedsP * 3 + 5);
if (wsBuf) {//out of memory
wsBuf->lock();
buffer = wsBuf->get();

// send leds preview to clients
for (size_t i = 0; i < ledsV.nrOfLedsP; i++)
{
buffer[i*3+5] = ledsV.ledsPhysical[i].red;
buffer[i*3+5+1] = ledsV.ledsPhysical[i].green;
buffer[i*3+5+2] = ledsV.ledsPhysical[i].blue;
}
//new values
buffer[0] = 1; //userFun id
// buffer[1] = ledsV.nrOfLedsP/256;
// buffer[2] = ledsV.nrOfLedsP%256;
// buffer[4] = max(ledsV.nrOfLedsP * SysModWeb::ws->count()/200, 16U); //interval in ms * 10, not too fast

var["interval"] = max(ledsV.nrOfLedsP * SysModWeb::ws->count()/200, 16U)*10; //interval in ms * 10, not too fast //from cs to ms
buffer = wsBuf->get();

for (auto client:SysModWeb::ws->getClients()) {
if (client->status() == WS_CONNECTED && !client->queueIsFull() && client->queueLength() <= WS_MAX_QUEUED_MESSAGES / web->ws->count() / 2) {//lossy
client->binary(wsBuf);
web->wsSendBytesCounter++;
}
// else {
// web->clientsChanged = true; tbd: changed also if full status changes
// print->printClient("loopFun skip frame", client);
// }
}

wsBuf->unlock();
SysModWeb::ws->_cleanBuffers();
// send leds preview to clients
for (size_t i = 0; i < ledsV.nrOfLedsP; i++)
{
buffer[i*3+5] = ledsV.ledsPhysical[i].red;
buffer[i*3+5+1] = ledsV.ledsPhysical[i].green;
buffer[i*3+5+2] = ledsV.ledsPhysical[i].blue;
}
}
//new values
buffer[0] = 1; //userFun id
// buffer[1] = ledsV.nrOfLedsP/256;
// buffer[2] = ledsV.nrOfLedsP%256;
// buffer[4] = max(ledsV.nrOfLedsP * SysModWeb::ws->count()/200, 16U); //interval in ms * 10, not too fast

xSemaphoreGive(web->wsMutex);
}, ledsV.nrOfLedsP * 3 + 5, true);
});
}

};

static AppModPreview *pvw;
2 changes: 1 addition & 1 deletion src/Sys/SysModFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void SysModFiles::setup() {
ui->initText(parentVar, "drsize", nullptr, 32, true, [](JsonObject var) { //uiFun
char details[32] = "";
print->fFormat(details, sizeof(details)-1, "%d / %d B", files->usedBytes(), files->totalBytes());
web->addResponse(var["id"], "value", details);
web->addResponse(var["id"], "value", JsonString(details, JsonString::Copied));
web->addResponse(var["id"], "label", "Total FS size");
});

Expand Down
13 changes: 5 additions & 8 deletions src/Sys/SysModModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include "SysModModel.h"
#include "SysModule.h"
#include "SysModWeb.h"
#include "SysModFiles.h"
#include "SysJsonRDWS.h"

Expand Down Expand Up @@ -100,7 +99,7 @@ void SysModModel::setup() {
}
}
void SysModModel::loop1s() {
setValueLossy("mSize", "%d / %d B", model->memoryUsage(), model->capacity());
setValueV("mSize", "%d / %d B", model->memoryUsage(), model->capacity());
}

void SysModModel::cleanUpModel(JsonArray vars, bool oPos, bool ro) {
Expand Down Expand Up @@ -148,27 +147,25 @@ JsonObject SysModModel::setValueV(const char * id, const char * format, ...) {
va_list args;
va_start(args, format);

// size_t len = vprintf(format, args);
char value[128];
vsnprintf(value, sizeof(value)-1, format, args);

va_end(args);

return setValue<JsonString>(id, JsonString(value, JsonString::Copied));
return setValue(id, JsonString(value, JsonString::Copied));
}

JsonObject SysModModel::setValueP(const char * id, const char * format, ...) {
va_list args;
va_start(args, format);

// size_t len = vprintf(format, args);
char value[128];
vsnprintf(value, sizeof(value)-1, format, args);
USER_PRINTF("%s\n", value);

va_end(args);

return setValue<JsonString>(id, JsonString(value, JsonString::Copied));
return setValue(id, JsonString(value, JsonString::Copied));
}

void SysModModel::setValueLossy(const char * id, const char * format, ...) {
Expand All @@ -186,10 +183,10 @@ void SysModModel::setValueLossy(const char * id, const char * format, ...) {
responseDoc->clear(); //needed for deserializeJson?
JsonVariant responseVariant = responseDoc->as<JsonVariant>();

web->addResponse(id, "value", value);
web->addResponse(id, "value", JsonString(value, JsonString::Copied));

bool isOk = true;
for (auto client:SysModWeb::ws->getClients()) {
for (auto client:web->ws->getClients()) {
if (client->status() != WS_CONNECTED || client->queueIsFull() || client->queueLength()>WS_MAX_QUEUED_MESSAGES / web->ws->count() / 2) //lossy
isOk = false;
}
Expand Down
21 changes: 14 additions & 7 deletions src/Sys/SysModModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "SysModule.h"
#include "SysModPrint.h"
#include "SysModUI.h"
#include "SysModWeb.h"

#include "ArduinoJson.h"

Expand Down Expand Up @@ -121,14 +122,20 @@ class SysModModel:public SysModule {
JsonObject setValue(JsonObject var, Type value, uint8_t rowNr = UINT8_MAX) {
// print->printJson("setValueB", var);
if (rowNr == UINT8_MAX) { //normal situation
bool notSame;
// if (std::is_same<Type, const char *>::value)
// same = var["value"] != value;
// else
notSame = var["value"].as<Type>() != value;
if (var["value"].isNull() || notSame) {
USER_PRINTF("setValue changed %s (%d) %s->..\n", var["id"].as<const char *>(), rowNr, var["value"].as<String>().c_str());//, value?"true":"false");
if (var["ro"].as<bool>()) {
//read only vars not in the model (tbd: modify setChFunAndWs)
JsonDocument *responseDoc = web->getResponseDoc();
responseDoc->clear(); //needed for deserializeJson?
JsonVariant responseVariant = responseDoc->as<JsonVariant>();

web->addResponse(var["id"], "value", value);

web->sendDataWs(responseVariant);
}
else if (var["value"].isNull() || var["value"].as<Type>() != value) { //const char * will be JsonString so comparison works
USER_PRINTF("setValue changed %s (%d) %s->", var["id"].as<const char *>(), rowNr, var["value"].as<String>().c_str()); //old value
var["value"] = value;
USER_PRINTF("%s\n", var["value"].as<String>().c_str()); //newvalue
ui->setChFunAndWs(var, rowNr);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Sys/SysModNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void SysModNetwork::loop() {
}

void SysModNetwork::loop1s() {
mdl->setValueLossy("rssi", "%d dBm", WiFi.RSSI());
mdl->setValueV("rssi", "%d dBm", WiFi.RSSI());
}

void SysModNetwork::handleConnection() {
Expand Down
56 changes: 16 additions & 40 deletions src/Sys/SysModPins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,50 +70,26 @@ void SysModPins::setup() {
web->addResponse(var["id"], "comment", "WIP");
}, nullptr, [](JsonObject var, uint8_t rowNr) { //loopFun

xSemaphoreTake(web->wsMutex, portMAX_DELAY);

SysModWeb::ws->cleanupClients();
if (web->ws->count()) {
var["interval"] = 10*10*10; //every 10 sec from cs to ms

web->sendDataWs([](AsyncWebSocketMessageBuffer * wsBuf) {
uint8_t* buffer;
AsyncWebSocketMessageBuffer * wsBuf = SysModWeb::ws->makeBuffer(20 * 3 + 5);
if (wsBuf) {//out of memory
wsBuf->lock();
buffer = wsBuf->get();

// send leds preview to clients
for (size_t i = 0; i < 20; i++)
{
buffer[i*3+5] = random(256);// (digitalRead(i)+1) * 50;
buffer[i*3+5+1] = random(256);
buffer[i*3+5+2] = random(256);
}
//new values
buffer[0] = 0; //userFun id
// buffer[1] = 0; //0 * 256
// buffer[2] = 20; //20 pins
// buffer[4] = 10*10; //every 10 sec

var["interval"] = 10*10*10; //every 10 sec from cs to ms

for (auto client:SysModWeb::ws->getClients()) {
if (client->status() == WS_CONNECTED && !client->queueIsFull() && client->queueLength() <= WS_MAX_QUEUED_MESSAGES / web->ws->count() / 2) {//lossy
client->binary(wsBuf);
web->wsSendBytesCounter++;
}
// else {
// web->clientsChanged = true; tbd: changed also if full status changes
// print->printClient("loopFun skip frame", client);
// }
}

wsBuf->unlock();
SysModWeb::ws->_cleanBuffers();
}
}

xSemaphoreGive(web->wsMutex);
buffer = wsBuf->get();

// send leds preview to clients
for (size_t i = 0; i < 20; i++)
{
buffer[i*3+5] = random(256);// (digitalRead(i)+1) * 50;
buffer[i*3+5+1] = random(256);
buffer[i*3+5+2] = random(256);
}
//new values
buffer[0] = 0; //userFun id
// buffer[1] = 0; //0 * 256
// buffer[2] = 20; //20 pins
// buffer[4] = 10*10; //every 10 sec
}, 20 * 3 + 5, true);
});

// ui->initCheckBox(parentVar, "pin2", true, false, nullptr, updateGPIO);
Expand Down
12 changes: 6 additions & 6 deletions src/Sys/SysModSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,20 @@ void SysModSystem::loop() {
loopCounter++;
}
void SysModSystem::loop1s() {
mdl->setValueLossy("upTime", "%lu s", millis()/1000);
mdl->setValueLossy("loops", "%lu /s", loopCounter);
mdl->setValueV("upTime", "%lu s", millis()/1000);
mdl->setValueV("loops", "%lu /s", loopCounter);

loopCounter = 0;
}
void SysModSystem::loop10s() {
mdl->setValue("version", JsonString(version)); //make sure ui shows the right version !!!never do this as it interupts with uiFun sendDataWS!!

mdl->setValueLossy("chip", "%s %s c#:%d %d mHz f:%d KB %d mHz %d", ESP.getChipModel(), ESP.getSdkVersion(), ESP.getChipCores(), ESP.getCpuFreqMHz(), ESP.getFlashChipSize()/1024, ESP.getFlashChipSpeed()/1000000, ESP.getFlashChipMode());
mdl->setValueV("chip", "%s %s c#:%d %d mHz f:%d KB %d mHz %d", ESP.getChipModel(), ESP.getSdkVersion(), ESP.getChipCores(), ESP.getCpuFreqMHz(), ESP.getFlashChipSize()/1024, ESP.getFlashChipSpeed()/1000000, ESP.getFlashChipMode());

mdl->setValueLossy("heap", "%d / %d (%d) B", ESP.getFreeHeap(), ESP.getHeapSize(), ESP.getMaxAllocHeap());
mdl->setValueLossy("stack", "%d B", uxTaskGetStackHighWaterMark(NULL));
mdl->setValueV("heap", "%d / %d (%d) B", ESP.getFreeHeap(), ESP.getHeapSize(), ESP.getMaxAllocHeap());
mdl->setValueV("stack", "%d B", uxTaskGetStackHighWaterMark(NULL));
if (psramFound()) {
mdl->setValueLossy("psram", "%d / %d (%d) B", ESP.getFreePsram(), ESP.getPsramSize(), ESP.getMinFreePsram());
mdl->setValueV("psram", "%d / %d (%d) B", ESP.getFreePsram(), ESP.getPsramSize(), ESP.getMinFreePsram());
}
USER_PRINTF("❤️"); //heartbeat
}
Expand Down
10 changes: 5 additions & 5 deletions src/Sys/SysModUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,21 +193,21 @@ void SysModUI::setChFunAndWs(JsonObject var, uint8_t rowNr, const char * value)
JsonVariant responseVariant = responseDoc->as<JsonVariant>();

if (value)
web->addResponse(var["id"], "value", value);
web->addResponse(var["id"], "value", JsonString(value, JsonString::Copied));
else {
if (var["value"].is<int>())
web->addResponseI(var["id"], "value", var["value"].as<int>());
web->addResponse(var["id"], "value", var["value"].as<int>());
else if (var["value"].is<bool>())
web->addResponseB(var["id"], "value", var["value"].as<bool>());
web->addResponse(var["id"], "value", var["value"].as<bool>());
else if (var["value"].is<const char *>())
web->addResponse(var["id"], "value", var["value"].as<const char *>());
else if (var["value"].is<JsonArray>()) {
USER_PRINTF("setChFunAndWs %s JsonArray %s\n", var["id"].as<const char *>(), var["value"].as<String>().c_str());
web->addResponseArray(var["id"], "value", var["value"].as<JsonArray>());
web->addResponse(var["id"], "value", var["value"].as<JsonArray>());
}
else {
USER_PRINTF("setChFunAndWs %s unknown type for %s\n", var["id"].as<const char *>(), var["value"].as<String>().c_str());
web->addResponse(var["id"], "value", var["value"]);
// web->addResponse(var["id"], "value", var["value"]);
}
}

Expand Down
Loading

0 comments on commit 6b114f4

Please sign in to comment.