mirror of
				https://github.com/s00500/ESPUI.git
				synced 2025-11-04 04:13:23 +00:00 
			
		
		
		
	Changes to support ArduinoJson 6 & 7
This commit is contained in:
		@@ -927,7 +927,7 @@ void ESPUIClass::clearGraph(uint16_t id, int clientId)
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        DynamicJsonDocument document(jsonUpdateDocumentSize);
 | 
			
		||||
        AllocateJsonDocument(document, jsonUpdateDocumentSize);
 | 
			
		||||
        JsonObject root = document.to<JsonObject>();
 | 
			
		||||
 | 
			
		||||
        root[F("type")] = (int)ControlType::Graph + UpdateOffset;
 | 
			
		||||
@@ -949,7 +949,7 @@ void ESPUIClass::addGraphPoint(uint16_t id, int nValue, int clientId)
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        DynamicJsonDocument document(jsonUpdateDocumentSize);
 | 
			
		||||
        AllocateJsonDocument(document, jsonUpdateDocumentSize);
 | 
			
		||||
        JsonObject root = document.to<JsonObject>();
 | 
			
		||||
 | 
			
		||||
        root[F("type")] = (int)ControlType::GraphPoint;
 | 
			
		||||
@@ -961,7 +961,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;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										15
									
								
								src/ESPUI.h
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								src/ESPUI.h
									
									
									
									
									
								
							@@ -5,7 +5,20 @@
 | 
			
		||||
#define WS_AUTHENTICATION false
 | 
			
		||||
 | 
			
		||||
#include <Arduino.h>
 | 
			
		||||
 | 
			
		||||
#include <ArduinoJson.h>
 | 
			
		||||
#if ARDUINOJSON_VERSION_MAJOR > 6
 | 
			
		||||
    #define AllocateJsonDocument(name, size)    JsonDocument name
 | 
			
		||||
    #define AllocateJsonArray(doc, name)        doc[name].to<JsonArray>()
 | 
			
		||||
    #define AllocateJsonObject(doc)             doc.add<JsonObject>()
 | 
			
		||||
    #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 <stdlib_noniso.h>
 | 
			
		||||
#ifdef ESP32
 | 
			
		||||
	#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
 | 
			
		||||
@@ -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<uint32_t, ESPUIclient*> MapOfClients;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
{
 | 
			
		||||
@@ -293,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)
 | 
			
		||||
@@ -393,7 +396,7 @@ uint32_t ESPUIclient::prepareJSONChunk(uint16_t startindex,
 | 
			
		||||
            // Serial.println(String(F("prepareJSONChunk: MaxMarshaledJsonSize: ")) + String(MaxMarshaledJsonSize));
 | 
			
		||||
            // Serial.println(String(F("prepareJSONChunk: Cur EstimatedUsedMarshaledJsonSize: ")) + String(EstimatedUsedMarshaledJsonSize));
 | 
			
		||||
 | 
			
		||||
            JsonObject item = items.createNestedObject();
 | 
			
		||||
            JsonObject item = AllocateJsonObject(items);
 | 
			
		||||
            elementcount++;
 | 
			
		||||
            uint32_t RemainingSpace = (MaxMarshaledJsonSize - EstimatedUsedMarshaledJsonSize) - 100;
 | 
			
		||||
            // Serial.println(String(F("prepareJSONChunk: RemainingSpace: ")) + String(RemainingSpace));
 | 
			
		||||
@@ -418,7 +421,7 @@ uint32_t ESPUIclient::prepareJSONChunk(uint16_t startindex,
 | 
			
		||||
                    // 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;
 | 
			
		||||
                }
 | 
			
		||||
@@ -496,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;
 | 
			
		||||
@@ -544,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;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
@@ -133,7 +133,7 @@ bool Control::MarshalControl(JsonObject & _item,
 | 
			
		||||
        _item[F("offset")] = StartingOffset;
 | 
			
		||||
        _item[F("length")] = ValueLenToSend;
 | 
			
		||||
        _item[F("total")] = value.length();
 | 
			
		||||
        item = _item.createNestedObject(F("control"));
 | 
			
		||||
        AllocateNamedJsonObject(item, _item, F("control"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    item[F("id")]      = id;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user