1
0
mirror of https://github.com/s00500/ESPUI.git synced 2025-06-14 02:30:41 +00:00

Chunk updates

This commit is contained in:
Ian Gray
2022-01-01 22:04:32 +00:00
committed by Lukas Bachschwell
parent 8331255355
commit 06460fcc4d
5 changed files with 63 additions and 39 deletions

View File

@ -947,9 +947,8 @@ Initially this function used to send the control element data individually.
Due to a change in the ESPAsyncWebserver library this had top be changed to be
sent as one blob at the beginning. Therefore a new type is used as well
*/
void ESPUIClass::jsonDom(AsyncWebSocketClient* client)
{
String json;
void ESPUIClass::jsonDom(AsyncWebSocketClient* client) {
DynamicJsonDocument document(jsonInitialDocumentSize);
document["type"] = (int)UI_INITIAL_GUI;
document["sliderContinuous"] = sliderContinuous;
@ -961,9 +960,38 @@ void ESPUIClass::jsonDom(AsyncWebSocketClient* client)
titleItem["type"] = (int)UI_TITLE;
titleItem["label"] = ui_title;
while (control != nullptr)
{
JsonObject item = items.createNestedObject();
while(1) {
control = prepareJSONChunk(client, control, &items);
String json;
serializeJson(document, json);
#if defined(DEBUG_ESPUI)
if (this->verbosity >= Verbosity::VerboseJSON) {
Serial.println("Sending elements --------->");
Serial.println(json);
}
#endif
if (client != nullptr) client->text(json);
else this->ws->textAll(json);
if(control == nullptr) break;
document.clear();
items.clear();
document["type"] = (int) UI_EXTEND_GUI;
items = document.createNestedArray("controls");
}
}
/* Prepare a chunk of elements as a single JSON string. If the allowed number of elements is greater than the total number
this will represent the entire UI and this function will return null. If a control pointer is returned then the limit
was reached, the currently serialised must be sent, and then processing resumed to send the next chunk. */
Control *ESPUIClass::prepareJSONChunk(AsyncWebSocketClient* client, Control* control, JsonArray *items) {
int elementcount = 0;
while (control != nullptr && elementcount < 10) {
JsonObject item = items->createNestedObject();
item["id"] = String(control->id);
item["type"] = (int)control->type;
@ -972,46 +1000,25 @@ void ESPUIClass::jsonDom(AsyncWebSocketClient* client)
item["color"] = (int)control->color;
item["visible"] = (int)control->visible;
if (control->parentControl != Control::noParent)
{
if (control->parentControl != Control::noParent) {
item["parentControl"] = String(control->parentControl);
}
// special case for selects: to preselect an option, you have to add
// "selected" to <option>
if (control->type == ControlType::Option)
{
if (ESPUI.getControl(control->parentControl)->value == control->value)
{
if (control->type == ControlType::Option) {
if (ESPUI.getControl(control->parentControl)->value == control->value) {
item["selected"] = "selected";
}
else
{
else {
item["selected"] = "";
}
}
control = control->next;
elementcount++;
}
// Send as one big bunch
serializeJson(document, json);
#if defined(DEBUG_ESPUI)
if (this->verbosity >= Verbosity::VerboseJSON)
{
Serial.println(json);
}
#endif
if (client != nullptr)
{
client->text(json);
}
else
{
this->ws->textAll(json);
}
return control;
}
void ESPUIClass::jsonReload()

View File

@ -77,11 +77,13 @@ enum ControlType : uint8_t
UpdateAccel,
InitialGui = 200,
Reload = 201
Reload = 201,
ExtendGUI = 210
};
#define UI_INITIAL_GUI ControlType::InitialGui
#define UI_RELOAD ControlType::Reload
#define UI_EXTEND_GUI ControlType::ExtendGUI
#define UI_TITLE ControlType::Title
#define UI_LABEL ControlType::Label
@ -294,6 +296,8 @@ private:
const char* basicAuthUsername = nullptr;
const char* basicAuthPassword = nullptr;
bool basicAuth = true;
Control *prepareJSONChunk(AsyncWebSocketClient* client, Control *control, JsonArray *items);
};
extern ESPUIClass ESPUI;

File diff suppressed because one or more lines are too long