diff --git a/data/js/controls.js b/data/js/controls.js index 908dd1a..6d9fff0 100644 --- a/data/js/controls.js +++ b/data/js/controls.js @@ -225,7 +225,7 @@ function handleVisibilityChange() { function start() { let location = window.location.hostname; let port = window.location.port; -// let location = "192.168.10.219"; +// let location = "192.168.10.198"; // let port = ""; document.addEventListener("visibilitychange", handleVisibilityChange, false); @@ -655,6 +655,7 @@ function start() { break; case UI_FRAGMENT: + // console.info("Starting Fragment Processing"); let FragmentLen = data.length; let FragementOffset = data.offset; let NextFragmentOffset = FragementOffset + FragmentLen; @@ -671,6 +672,7 @@ function start() { if (!data.hasOwnProperty('control')) { console.error("UI_FRAGMENT:Missing control record, skipping control"); + // console.info("Done Fragment Processing"); break; } let control = data.control; @@ -686,7 +688,8 @@ function start() { StartFragmentAssemblyTimer(control.id); let TotalRequest = JSON.stringify({ 'id' : control.id, 'offset' : NextFragmentOffset }); websock.send("uifragmentok:" + 0 + ": " + TotalRequest + ":"); - // console.info("asked for fragment 2"); + // console.info("asked for fragment " + TotalRequest); + // console.info("Done Fragment Processing"); break; } @@ -698,7 +701,8 @@ function start() { StartFragmentAssemblyTimer(control.id); let TotalRequest = JSON.stringify({ 'id' : control.id, 'offset' : 0 }); websock.send("uifragmentok:" + 0 + ": " + TotalRequest + ":"); - // console.info("asked for fragment 1"); + // console.info("asked for fragment " + TotalRequest); + // console.info("Done Fragment Processing"); break; } @@ -709,7 +713,8 @@ function start() { StartFragmentAssemblyTimer(control.id); let TotalRequest = JSON.stringify({ 'id' : control.id, 'offset' : controlAssemblyArray[control.id].length + controlAssemblyArray[control.id].offset }); websock.send("uifragmentok:" + 0 + ": " + TotalRequest + ":"); - // console.info("asked for the expected fragment"); + // console.info("asked for the expected fragment: " + TotalRequest); + // console.info("Done Fragment Processing"); break; } @@ -733,7 +738,9 @@ function start() { StartFragmentAssemblyTimer(control.id); let TotalRequest = JSON.stringify({ 'id' : control.id, 'offset' : NextFragmentOffset}); websock.send("uifragmentok:" + 0 + ": " + TotalRequest + ":"); + // console.info("asked for the next fragment: " + TotalRequest); } + // console.info("Done Fragment Processing"); break; default: diff --git a/pio_examples/gui/platformio.ini b/pio_examples/gui/platformio.ini index 68ba0fd..5c8dc03 100644 --- a/pio_examples/gui/platformio.ini +++ b/pio_examples/gui/platformio.ini @@ -17,7 +17,8 @@ framework = arduino board_build.filesystem = littlefs lib_extra_dirs = ../../ lib_deps = - bblanchon/ArduinoJson @ ^6.18.5 +; bblanchon/ArduinoJson @ ^6.18.5 + bblanchon/ArduinoJson @ ^7.0.4 https://github.com/bmedici/ESPAsyncWebServer ; Use a fork of the library that has a bugfix for the compile.... https://github.com/esphome/ESPAsyncWebServer/pull/17 lib_ignore = @@ -41,11 +42,12 @@ platform = espressif32 board = esp32dev monitor_filters = esp32_exception_decoder board_build.flash_mode = dout -;build_flags = +build_flags = ; -D DEBUG_ESPUI + lib_deps = ${env.lib_deps} me-no-dev/AsyncTCP -upload_port = COM9 -monitor_port = COM9 +upload_port = COM6 +monitor_port = COM6 monitor_speed = 115200 diff --git a/pio_examples/gui/src/gui.ino b/pio_examples/gui/src/gui.ino index 670a4d6..c479db6 100644 --- a/pio_examples/gui/src/gui.ino +++ b/pio_examples/gui/src/gui.ino @@ -11,17 +11,23 @@ DNSServer dnsServer; #include #endif -const char* ssid = "ESPUI"; -const char* password = "espui"; +const char* ssid = "YourNetworkName"; +const char* password = "YourNetworkPassphrase"; const char* hostname = "espui"; String DisplayTestFileName = "/FileName.txt"; +int fileDisplayId = Control::noParent; + int statusLabelId = Control::noParent; + +#ifdef TEST_GRAPH int graphId = Control::noParent; +#endif // def TEST_GRAPH int millisLabelId = Control::noParent; int testSwitchId = Control::noParent; -int fileDisplayId = Control::noParent; + +char HugeText[1025]; void numberCall(Control* sender, int type) { @@ -166,6 +172,9 @@ void setup(void) ESPUI.setVerbosity(Verbosity::VerboseJSON); Serial.begin(115200); + memset(HugeText, 0x0, sizeof(HugeText)); + memset(HugeText, 'a', sizeof(HugeText)-1); + #if defined(ESP32) WiFi.setHostname(hostname); #else @@ -239,10 +248,16 @@ void setup(void) ESPUI.slider("Slider one", &slider, ControlColor::Alizarin, 30, 0, 30); ESPUI.slider("Slider two", &slider, ControlColor::None, 100); ESPUI.text("Text Test:", &textCall, ControlColor::Alizarin, "a Text Field"); + + ESPUI.text("Huge Text Test:", &textCall, ControlColor::Alizarin, HugeText); + ESPUI.number("Numbertest", &numberCall, ControlColor::Alizarin, 5, 0, 10); + fileDisplayId = ESPUI.fileDisplay("Filetest", ControlColor::Turquoise, DisplayTestFileName); +#ifdef TEST_GRAPH graphId = ESPUI.graph("Graph Test", ControlColor::Wetasphalt); +#endif // def TEST_GRAPH /* * .begin loads and serves all files from PROGMEM directly. @@ -261,15 +276,17 @@ void setup(void) * password, for example begin("ESPUI Control", "username", "password") */ ESPUI.sliderContinuous = true; - ESPUI.prepareFileSystem(); - ESPUI.beginLITTLEFS("ESPUI Control"); - // create a text file - ESPUI.writeFile("/DisplayFile.txt", "Test Line\n"); + ESPUI.prepareFileSystem(); + + ESPUI.beginLITTLEFS("ESPUI Control"); // these files are used by browsers to auto config a connection. ESPUI.writeFile("/wpad.dat", " "); ESPUI.writeFile("/connecttest.txt", " "); + + // create a text file + ESPUI.writeFile("/DisplayFile.txt", "Test Line\n"); } void loop(void) @@ -284,7 +301,9 @@ void loop(void) { ESPUI.print(millisLabelId, String(millis())); +#ifdef TEST_GRAPH ESPUI.addGraphPoint(graphId, random(1, 50)); +#endif // def TEST_GRAPH testSwitchState = !testSwitchState; ESPUI.updateSwitcher(testSwitchId, testSwitchState); @@ -298,7 +317,7 @@ void loop(void) { testFile.write((const uint8_t*)TestLine.c_str(), TestLine.length()); ESPUI.updateControl(fileDisplayId); - + TestLine += String("filesize: ") + String(filesize); // Serial.println(TestLine); } diff --git a/src/ESPUI.cpp b/src/ESPUI.cpp index eb1abfe..8852750 100644 --- a/src/ESPUI.cpp +++ b/src/ESPUI.cpp @@ -932,7 +932,7 @@ void ESPUIClass::clearGraph(uint16_t id, int clientId) break; } - DynamicJsonDocument document(jsonUpdateDocumentSize); + AllocateJsonDocument(document, jsonUpdateDocumentSize); JsonObject root = document.to(); root[F("type")] = (int)ControlType::Graph + UpdateOffset; @@ -954,7 +954,7 @@ void ESPUIClass::addGraphPoint(uint16_t id, int nValue, int clientId) break; } - DynamicJsonDocument document(jsonUpdateDocumentSize); + AllocateJsonDocument(document, jsonUpdateDocumentSize); JsonObject root = document.to(); root[F("type")] = (int)ControlType::GraphPoint; @@ -966,7 +966,7 @@ void ESPUIClass::addGraphPoint(uint16_t id, int nValue, int clientId) } while (false); } -bool ESPUIClass::SendJsonDocToWebSocket(ArduinoJson::DynamicJsonDocument& document, uint16_t clientId) +bool ESPUIClass::SendJsonDocToWebSocket(ArduinoJson::JsonDocument& document, uint16_t clientId) { bool Response = false; diff --git a/src/ESPUI.h b/src/ESPUI.h index 9baa561..0e7bcc4 100644 --- a/src/ESPUI.h +++ b/src/ESPUI.h @@ -5,7 +5,20 @@ #define WS_AUTHENTICATION false #include + #include +#if ARDUINOJSON_VERSION_MAJOR > 6 + #define AllocateJsonDocument(name, size) JsonDocument name + #define AllocateJsonArray(doc, name) doc[name].to() + #define AllocateJsonObject(doc) doc.add() + #define AllocateNamedJsonObject(t, s, n) t[n] = s +#else + #define AllocateJsonDocument(name, size) DynamicJsonDocument name(size) + #define AllocateJsonArray(doc, name) doc.createNestedArray(name) + #define AllocateJsonObject(doc) doc.createNestedObject() + #define AllocateNamedJsonObject(t, s, n) t = s.createNestedObject(n) +#endif + #include #ifdef ESP32 #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 @@ -34,10 +47,10 @@ #include #include -#define FILE_WRITING "w" - #endif +#define FILE_WRITING "w" + // Message Types (and control types) enum MessageTypes : uint8_t @@ -271,7 +284,7 @@ protected: void NotifyClients(ClientUpdateType_t newState); void NotifyClient(uint32_t WsClientId, ClientUpdateType_t newState); - bool SendJsonDocToWebSocket(ArduinoJson::DynamicJsonDocument& document, uint16_t clientId); + bool SendJsonDocToWebSocket(ArduinoJson::JsonDocument& document, uint16_t clientId); std::map MapOfClients; diff --git a/src/ESPUIclient.cpp b/src/ESPUIclient.cpp index a0b6cc3..0becacf 100644 --- a/src/ESPUIclient.cpp +++ b/src/ESPUIclient.cpp @@ -74,14 +74,14 @@ bool ESPUIclient::CanSend() return Response; } -void ESPUIclient::FillInHeader(DynamicJsonDocument& document) +void ESPUIclient::FillInHeader(JsonDocument& document) { document[F("type")] = UI_EXTEND_GUI; document[F("sliderContinuous")] = ESPUI.sliderContinuous; document[F("startindex")] = 0; document[F("totalcontrols")] = ESPUI.controlCount; - JsonArray items = document.createNestedArray(F("controls")); - JsonObject titleItem = items.createNestedObject(); + JsonArray items = AllocateJsonArray(document, F("controls")); + JsonObject titleItem = AllocateJsonObject(items); titleItem[F("type")] = (int)UI_TITLE; titleItem[F("label")] = ESPUI.ui_title; } @@ -104,7 +104,7 @@ bool ESPUIclient::SendClientNotification(ClientUpdateType_t value) break; } - DynamicJsonDocument document(ESPUI.jsonUpdateDocumentSize); + AllocateJsonDocument(document, ESPUI.jsonUpdateDocumentSize); FillInHeader(document); if(ClientUpdateType_t::ReloadNeeded == value) { @@ -259,7 +259,7 @@ number this will represent the entire UI. More likely, it will represent a small client will acknowledge receipt by requesting the next chunk. */ uint32_t ESPUIclient::prepareJSONChunk(uint16_t startindex, - DynamicJsonDocument & rootDoc, + JsonDocument & rootDoc, bool InUpdateMode, String FragmentRequestString) { @@ -267,8 +267,12 @@ uint32_t ESPUIclient::prepareJSONChunk(uint16_t startindex, xSemaphoreTake(ESPUI.ControlsSemaphore, portMAX_DELAY); #endif // def ESP32 - // Serial.println(String("prepareJSONChunk: Start. InUpdateMode: ") + String(InUpdateMode)); + // Serial.println(String("prepareJSONChunk: Start. InUpdateMode: ") + String(InUpdateMode)); + // Serial.println(String("prepareJSONChunk: Start. startindex: ") + String(startindex)); + // Serial.println(String("prepareJSONChunk: Start. FragmentRequestString: '") + FragmentRequestString + "'"); int elementcount = 0; + uint32_t MaxMarshaledJsonSize = (!InUpdateMode) ? ESPUI.jsonInitialDocumentSize: ESPUI.jsonUpdateDocumentSize; + uint32_t EstimatedUsedMarshaledJsonSize = 0; do // once { @@ -289,12 +293,15 @@ uint32_t ESPUIclient::prepareJSONChunk(uint16_t startindex, // this is actually a fragment or directed update request // parse the string we got from the UI and try to update that specific // control. - DynamicJsonDocument FragmentRequest(FragmentRequestString.length() * 3); + AllocateJsonDocument(FragmentRequest, FragmentRequestString.length() * 3); +/* + ArduinoJson::detail::sizeofObject(N); if(0 >= FragmentRequest.capacity()) { Serial.println(F("ERROR:prepareJSONChunk:Fragmentation:Could not allocate memory for a fragmentation request. Skipping Response")); break; } +*/ size_t FragmentRequestStartOffset = FragmentRequestString.indexOf("{"); DeserializationError error = deserializeJson(FragmentRequest, FragmentRequestString.substring(FragmentRequestStartOffset)); if(DeserializationError::Ok != error) @@ -386,19 +393,35 @@ uint32_t ESPUIclient::prepareJSONChunk(uint16_t startindex, } } - JsonObject item = items.createNestedObject(); + // Serial.println(String(F("prepareJSONChunk: MaxMarshaledJsonSize: ")) + String(MaxMarshaledJsonSize)); + // Serial.println(String(F("prepareJSONChunk: Cur EstimatedUsedMarshaledJsonSize: ")) + String(EstimatedUsedMarshaledJsonSize)); + + JsonObject item = AllocateJsonObject(items); elementcount++; - control->MarshalControl(item, InUpdateMode, DataOffset); - - if (rootDoc.overflowed() || (ESPUI.jsonChunkNumberMax > 0 && (elementcount % ESPUI.jsonChunkNumberMax) == 0)) + uint32_t RemainingSpace = (MaxMarshaledJsonSize - EstimatedUsedMarshaledJsonSize) - 100; + // Serial.println(String(F("prepareJSONChunk: RemainingSpace: ")) + String(RemainingSpace)); + uint32_t SpaceUsedByMarshaledControl = 0; + bool ControlIsFragmented = control->MarshalControl(item, + InUpdateMode, + DataOffset, + RemainingSpace, + SpaceUsedByMarshaledControl); + // Serial.println(String(F("prepareJSONChunk: SpaceUsedByMarshaledControl: ")) + String(SpaceUsedByMarshaledControl)); + EstimatedUsedMarshaledJsonSize += SpaceUsedByMarshaledControl; + // Serial.println(String(F("prepareJSONChunk: New EstimatedUsedMarshaledJsonSize: ")) + String(EstimatedUsedMarshaledJsonSize)); + // Serial.println(String(F("prepareJSONChunk: ControlIsFragmented: ")) + String(ControlIsFragmented)); + + // did the control get added to the doc? + if (0 == SpaceUsedByMarshaledControl || + (ESPUI.jsonChunkNumberMax > 0 && (elementcount % ESPUI.jsonChunkNumberMax) == 0)) { - // String("prepareJSONChunk: too much data in the message. Remove the last entry"); + // Serial.println( String("prepareJSONChunk: too much data in the message. Remove the last entry")); if (1 == elementcount) { - Serial.println(String(F("ERROR: prepareJSONChunk: Control ")) + String(control->id) + F(" is too large to be sent to the browser.")); + // Serial.println(String(F("prepareJSONChunk: Control ")) + String(control->id) + F(" is too large to be sent to the browser.")); // Serial.println(String(F("ERROR: prepareJSONChunk: value: ")) + control->value); rootDoc.clear(); - item = items.createNestedObject(); + item = AllocateJsonObject(items); control->MarshalErrorMessage(item); elementcount = 0; } @@ -413,13 +436,16 @@ uint32_t ESPUIclient::prepareJSONChunk(uint16_t startindex, // exit the loop control = nullptr; } - else if (SingleControl) + else if ((SingleControl) || + (ControlIsFragmented) || + (MaxMarshaledJsonSize < (EstimatedUsedMarshaledJsonSize + 100))) { - // Serial.println("prepareJSONChunk: exit loop"); + // Serial.println("prepareJSONChunk: Doc is Full, Fragmented Control or Single Control. exit loop"); control = nullptr; } else { + // Serial.println("prepareJSONChunk: Next Control"); control = control->next; } } // end while (control != nullptr) @@ -430,7 +456,7 @@ uint32_t ESPUIclient::prepareJSONChunk(uint16_t startindex, xSemaphoreGive(ESPUI.ControlsSemaphore); #endif // def ESP32 - // Serial.println(String("prepareJSONChunk: elementcount: ") + String(elementcount)); + // Serial.println(String("prepareJSONChunk: END: elementcount: ") + String(elementcount)); return elementcount; } @@ -473,7 +499,7 @@ bool ESPUIclient::SendControlsToClient(uint16_t startidx, ClientUpdateType_t Tra break; } - DynamicJsonDocument document(ESPUI.jsonInitialDocumentSize); + AllocateJsonDocument(document, ESPUI.jsonInitialDocumentSize); FillInHeader(document); document[F("startindex")] = startidx; document[F("totalcontrols")] = uint16_t(-1); // ESPUI.controlCount; @@ -521,7 +547,7 @@ bool ESPUIclient::SendControlsToClient(uint16_t startidx, ClientUpdateType_t Tra return Response; } -bool ESPUIclient::SendJsonDocToWebSocket(DynamicJsonDocument& document) +bool ESPUIclient::SendJsonDocToWebSocket(JsonDocument& document) { bool Response = true; diff --git a/src/ESPUIclient.h b/src/ESPUIclient.h index 444725b..b013f5e 100644 --- a/src/ESPUIclient.h +++ b/src/ESPUIclient.h @@ -43,8 +43,8 @@ protected: // bool NeedsNotification() { return pCurrentFsmState != &fsm_EspuiClient_state_Idle_imp; } bool CanSend(); - void FillInHeader(ArduinoJson::DynamicJsonDocument& document); - uint32_t prepareJSONChunk(uint16_t startindex, DynamicJsonDocument& rootDoc, bool InUpdateMode, String value); + void FillInHeader(ArduinoJson::JsonDocument& document); + uint32_t prepareJSONChunk(uint16_t startindex, JsonDocument& rootDoc, bool InUpdateMode, String value); bool SendControlsToClient(uint16_t startidx, ClientUpdateType_t TransferMode, String FragmentRequest); bool SendClientNotification(ClientUpdateType_t value); @@ -62,6 +62,6 @@ public: bool IsSyncronized(); uint32_t id() { return client->id(); } void SetState(ClientUpdateType_t value); - bool SendJsonDocToWebSocket(ArduinoJson::DynamicJsonDocument& document); + bool SendJsonDocToWebSocket(ArduinoJson::JsonDocument& document); }; diff --git a/src/ESPUIcontrol.cpp b/src/ESPUIcontrol.cpp index 44002ac..36647bf 100644 --- a/src/ESPUIcontrol.cpp +++ b/src/ESPUIcontrol.cpp @@ -48,20 +48,70 @@ void Control::DeleteControl() callback = nullptr; } -void Control::MarshalControl(JsonObject & _item, bool refresh, uint32_t StartingOffset) +bool Control::MarshalControl(JsonObject & _item, + bool refresh, + uint32_t StartingOffset, + uint32_t AvailMarshaledLength, + uint32_t &EstimatedMarshaledLength) { - JsonObject & item = _item; - uint32_t length = value.length(); - uint32_t maxLength = uint32_t(double(ESPUI.jsonInitialDocumentSize) * 0.75); - if((length > maxLength) || StartingOffset) - { - /* - Serial.println(String("MarshalControl:Start Fragment Processing")); - Serial.println(String("MarshalControl:id: ") + String(id)); - Serial.println(String("MarshalControl:length: ") + String(length)); - Serial.println(String("MarshalControl:StartingOffset: ") + String(StartingOffset)); - Serial.println(String("MarshalControl:maxLength: ") + String(maxLength)); + // this code assumes MaxMarshaledLength > JsonMarshalingRatio + // Serial.println(String("MarshalControl: StartingOffset: ") + String(StartingOffset)); + // Serial.println(String("MarshalControl: AvailMarshaledLength: ") + String(AvailMarshaledLength)); + // Serial.println(String("MarshalControl: Control ID: ") + String(id)); + bool ControlIsFragmented = false; + // create a new item in the response document + JsonObject & item = _item; + + // how much space do we expect to use? + uint32_t ValueMarshaledLength = (value.length() - StartingOffset) * JsonMarshalingRatio; + uint32_t LabelMarshaledLength = strlen(label) * JsonMarshalingRatio; + uint32_t MinimumMarshaledLength = LabelMarshaledLength + JsonMarshaledOverhead; + uint32_t MaximumMarshaledLength = ValueMarshaledLength + MinimumMarshaledLength; + uint32_t SpaceForMarshaledValue = AvailMarshaledLength - MinimumMarshaledLength; + // Serial.println(String("MarshalControl: value.length(): ") + String(value.length())); + // Serial.println(String("MarshalControl: ValueMarshaledLength: ") + String(ValueMarshaledLength)); + // Serial.println(String("MarshalControl: LabelMarshaledLength: ") + String(LabelMarshaledLength)); + // Serial.println(String("MarshalControl: MaximumMarshaledLength: ") + String(MaximumMarshaledLength)); + // Serial.println(String("MarshalControl: MinimumMarshaledLength: ") + String(MinimumMarshaledLength)); + // Serial.println(String("MarshalControl: SpaceForMarshaledValue: ") + String(SpaceForMarshaledValue)); + + // will the item fit in the remaining space? Fragment if not + if (AvailMarshaledLength < MinimumMarshaledLength) + { + // Serial.println(String("MarshalControl: Cannot Marshal control. Not enough space for basic headers.")); + EstimatedMarshaledLength = 0; + return false; + } + + uint32_t MaxValueLength = (SpaceForMarshaledValue / JsonMarshalingRatio); + // Serial.println(String("MarshalControl: MaxValueLength: ") + String(MaxValueLength)); + + uint32_t ValueLenToSend = min((value.length() - StartingOffset), MaxValueLength); + // Serial.println(String("MarshalControl: ValueLenToSend: ") + String(ValueLenToSend)); + + uint32_t AdjustedMarshaledLength = (ValueLenToSend * JsonMarshalingRatio) + MinimumMarshaledLength; + // Serial.println(String("MarshalControl: AdjustedMarshaledLength: ") + String(AdjustedMarshaledLength)); + + bool NeedToFragment = (ValueLenToSend < value.length()); + // Serial.println(String("MarshalControl: NeedToFragment: ") + String(NeedToFragment)); + + if ((AdjustedMarshaledLength > AvailMarshaledLength) && (0 != ValueLenToSend)) + { + // Serial.println(String("MarshalControl: Cannot Marshal control. Not enough space for marshaled control.")); + EstimatedMarshaledLength = 0; + return false; + } + + EstimatedMarshaledLength = AdjustedMarshaledLength; + + // are we fragmenting? + if(NeedToFragment || StartingOffset) + { + // Serial.println(String("MarshalControl:Start Fragment Processing")); + // Serial.println(String("MarshalControl:id: ") + String(id)); + // Serial.println(String("MarshalControl:StartingOffset: ") + String(StartingOffset)); +/* if(0 == StartingOffset) { Serial.println(String("MarshalControl: New control to fragement. ID: ") + String(id)); @@ -70,19 +120,20 @@ void Control::MarshalControl(JsonObject & _item, bool refresh, uint32_t Starting { Serial.println(String("MarshalControl: Next fragement. ID: ") + String(id)); } - */ - +*/ // indicate that no additional controls should be sent + ControlIsFragmented = true; + + // fill in the fragment header _item[F("type")] = uint32_t(ControlType::Fragment); _item[F("id")] = id; - length = min((length - StartingOffset), maxLength); // Serial.println(String("MarshalControl:Final length: ") + String(length)); _item[F("offset")] = StartingOffset; - _item[F("length")] = length; + _item[F("length")] = ValueLenToSend; _item[F("total")] = value.length(); - item = _item.createNestedObject(F("control")); + AllocateNamedJsonObject(item, _item, F("control")); } item[F("id")] = id; @@ -97,7 +148,7 @@ void Control::MarshalControl(JsonObject & _item, bool refresh, uint32_t Starting } item[F("label")] = label; - item[F ("value")] = (ControlType::Password == type) ? F ("--------") : value.substring(StartingOffset, length + StartingOffset); + item[F ("value")] = (ControlType::Password == type) ? F ("--------") : value.substring(StartingOffset, StartingOffset + ValueLenToSend); item[F("visible")] = visible; item[F("color")] = (int)color; item[F("enabled")] = enabled; @@ -130,6 +181,9 @@ void Control::MarshalControl(JsonObject & _item, bool refresh, uint32_t Starting item[F("selected")] = ""; } } + + // Serial.println(String("MarshalControl:Done")); + return ControlIsFragmented; } void Control::MarshalErrorMessage(JsonObject & item) @@ -280,4 +334,3 @@ void Control::onWsEvent(String & cmd, String& data) } } while (false); } - diff --git a/src/ESPUIcontrol.h b/src/ESPUIcontrol.h index d075353..1701ae0 100644 --- a/src/ESPUIcontrol.h +++ b/src/ESPUIcontrol.h @@ -83,18 +83,23 @@ public: void SendCallback(int type); bool HasCallback() { return (nullptr != callback); } - void MarshalControl(ArduinoJson::JsonObject& item, bool refresh, uint32_t DataOffset); + bool MarshalControl(ArduinoJson::JsonObject& item, bool refresh, uint32_t DataOffset, uint32_t MaxLength, uint32_t & EstimmatedUsedSpace); void MarshalErrorMessage(ArduinoJson::JsonObject& item); void DeleteControl(); void onWsEvent(String& cmd, String& data); inline bool ToBeDeleted() { return _ToBeDeleted; } inline bool NeedsSync(uint32_t lastControlChangeID) {return (false == _ToBeDeleted) && (lastControlChangeID < ControlChangeID);} void SetControlChangedId(uint32_t value) {ControlChangeID = value;} - + private: bool _ToBeDeleted = false; uint32_t ControlChangeID = 0; String OldValue = emptyString; + + // multiplier for converting a typical controller label or value to a Json object + #define JsonMarshalingRatio 3 + // Marshaed Control overhead length + #define JsonMarshaledOverhead 64 }; #define UI_TITLE ControlType::Title