mirror of
https://github.com/s00500/ESPUI.git
synced 2024-11-23 10:50:54 +00:00
Changes to support ArduinoJson 6 & 7
This commit is contained in:
parent
7dc51d1274
commit
75bd3dc378
@ -17,7 +17,8 @@ framework = arduino
|
|||||||
board_build.filesystem = littlefs
|
board_build.filesystem = littlefs
|
||||||
lib_extra_dirs = ../../
|
lib_extra_dirs = ../../
|
||||||
lib_deps =
|
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
|
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 =
|
lib_ignore =
|
||||||
@ -43,7 +44,9 @@ monitor_filters = esp32_exception_decoder
|
|||||||
board_build.flash_mode = dout
|
board_build.flash_mode = dout
|
||||||
build_flags =
|
build_flags =
|
||||||
-D TEST_HUGE_TEXT
|
-D TEST_HUGE_TEXT
|
||||||
|
-D TEST_FILEDISPLAY
|
||||||
; -D DEBUG_ESPUI
|
; -D DEBUG_ESPUI
|
||||||
|
|
||||||
lib_deps =
|
lib_deps =
|
||||||
${env.lib_deps}
|
${env.lib_deps}
|
||||||
me-no-dev/AsyncTCP
|
me-no-dev/AsyncTCP
|
||||||
|
@ -927,7 +927,7 @@ void ESPUIClass::clearGraph(uint16_t id, int clientId)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
DynamicJsonDocument document(jsonUpdateDocumentSize);
|
AllocateJsonDocument(document, jsonUpdateDocumentSize);
|
||||||
JsonObject root = document.to<JsonObject>();
|
JsonObject root = document.to<JsonObject>();
|
||||||
|
|
||||||
root[F("type")] = (int)ControlType::Graph + UpdateOffset;
|
root[F("type")] = (int)ControlType::Graph + UpdateOffset;
|
||||||
@ -949,7 +949,7 @@ void ESPUIClass::addGraphPoint(uint16_t id, int nValue, int clientId)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
DynamicJsonDocument document(jsonUpdateDocumentSize);
|
AllocateJsonDocument(document, jsonUpdateDocumentSize);
|
||||||
JsonObject root = document.to<JsonObject>();
|
JsonObject root = document.to<JsonObject>();
|
||||||
|
|
||||||
root[F("type")] = (int)ControlType::GraphPoint;
|
root[F("type")] = (int)ControlType::GraphPoint;
|
||||||
@ -961,7 +961,7 @@ void ESPUIClass::addGraphPoint(uint16_t id, int nValue, int clientId)
|
|||||||
} while (false);
|
} while (false);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ESPUIClass::SendJsonDocToWebSocket(ArduinoJson::DynamicJsonDocument& document, uint16_t clientId)
|
bool ESPUIClass::SendJsonDocToWebSocket(ArduinoJson::JsonDocument& document, uint16_t clientId)
|
||||||
{
|
{
|
||||||
bool Response = false;
|
bool Response = false;
|
||||||
|
|
||||||
|
15
src/ESPUI.h
15
src/ESPUI.h
@ -5,7 +5,20 @@
|
|||||||
#define WS_AUTHENTICATION false
|
#define WS_AUTHENTICATION false
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
#include <ArduinoJson.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>
|
#include <stdlib_noniso.h>
|
||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
|
#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 NotifyClients(ClientUpdateType_t newState);
|
||||||
void NotifyClient(uint32_t WsClientId, 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;
|
std::map<uint32_t, ESPUIclient*> MapOfClients;
|
||||||
|
|
||||||
|
@ -74,14 +74,14 @@ bool ESPUIclient::CanSend()
|
|||||||
return Response;
|
return Response;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ESPUIclient::FillInHeader(DynamicJsonDocument& document)
|
void ESPUIclient::FillInHeader(JsonDocument& document)
|
||||||
{
|
{
|
||||||
document[F("type")] = UI_EXTEND_GUI;
|
document[F("type")] = UI_EXTEND_GUI;
|
||||||
document[F("sliderContinuous")] = ESPUI.sliderContinuous;
|
document[F("sliderContinuous")] = ESPUI.sliderContinuous;
|
||||||
document[F("startindex")] = 0;
|
document[F("startindex")] = 0;
|
||||||
document[F("totalcontrols")] = ESPUI.controlCount;
|
document[F("totalcontrols")] = ESPUI.controlCount;
|
||||||
JsonArray items = document.createNestedArray(F("controls"));
|
JsonArray items = AllocateJsonArray(document, F("controls"));
|
||||||
JsonObject titleItem = items.createNestedObject();
|
JsonObject titleItem = AllocateJsonObject(items);
|
||||||
titleItem[F("type")] = (int)UI_TITLE;
|
titleItem[F("type")] = (int)UI_TITLE;
|
||||||
titleItem[F("label")] = ESPUI.ui_title;
|
titleItem[F("label")] = ESPUI.ui_title;
|
||||||
}
|
}
|
||||||
@ -104,7 +104,7 @@ bool ESPUIclient::SendClientNotification(ClientUpdateType_t value)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
DynamicJsonDocument document(ESPUI.jsonUpdateDocumentSize);
|
AllocateJsonDocument(document, ESPUI.jsonUpdateDocumentSize);
|
||||||
FillInHeader(document);
|
FillInHeader(document);
|
||||||
if(ClientUpdateType_t::ReloadNeeded == value)
|
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.
|
client will acknowledge receipt by requesting the next chunk.
|
||||||
*/
|
*/
|
||||||
uint32_t ESPUIclient::prepareJSONChunk(uint16_t startindex,
|
uint32_t ESPUIclient::prepareJSONChunk(uint16_t startindex,
|
||||||
DynamicJsonDocument & rootDoc,
|
JsonDocument & rootDoc,
|
||||||
bool InUpdateMode,
|
bool InUpdateMode,
|
||||||
String FragmentRequestString)
|
String FragmentRequestString)
|
||||||
{
|
{
|
||||||
@ -293,12 +293,15 @@ uint32_t ESPUIclient::prepareJSONChunk(uint16_t startindex,
|
|||||||
// this is actually a fragment or directed update request
|
// this is actually a fragment or directed update request
|
||||||
// parse the string we got from the UI and try to update that specific
|
// parse the string we got from the UI and try to update that specific
|
||||||
// control.
|
// control.
|
||||||
DynamicJsonDocument FragmentRequest(FragmentRequestString.length() * 3);
|
AllocateJsonDocument(FragmentRequest, FragmentRequestString.length() * 3);
|
||||||
|
/*
|
||||||
|
ArduinoJson::detail::sizeofObject(N);
|
||||||
if(0 >= FragmentRequest.capacity())
|
if(0 >= FragmentRequest.capacity())
|
||||||
{
|
{
|
||||||
Serial.println(F("ERROR:prepareJSONChunk:Fragmentation:Could not allocate memory for a fragmentation request. Skipping Response"));
|
Serial.println(F("ERROR:prepareJSONChunk:Fragmentation:Could not allocate memory for a fragmentation request. Skipping Response"));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
size_t FragmentRequestStartOffset = FragmentRequestString.indexOf("{");
|
size_t FragmentRequestStartOffset = FragmentRequestString.indexOf("{");
|
||||||
DeserializationError error = deserializeJson(FragmentRequest, FragmentRequestString.substring(FragmentRequestStartOffset));
|
DeserializationError error = deserializeJson(FragmentRequest, FragmentRequestString.substring(FragmentRequestStartOffset));
|
||||||
if(DeserializationError::Ok != error)
|
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: MaxMarshaledJsonSize: ")) + String(MaxMarshaledJsonSize));
|
||||||
// Serial.println(String(F("prepareJSONChunk: Cur EstimatedUsedMarshaledJsonSize: ")) + String(EstimatedUsedMarshaledJsonSize));
|
// Serial.println(String(F("prepareJSONChunk: Cur EstimatedUsedMarshaledJsonSize: ")) + String(EstimatedUsedMarshaledJsonSize));
|
||||||
|
|
||||||
JsonObject item = items.createNestedObject();
|
JsonObject item = AllocateJsonObject(items);
|
||||||
elementcount++;
|
elementcount++;
|
||||||
uint32_t RemainingSpace = (MaxMarshaledJsonSize - EstimatedUsedMarshaledJsonSize) - 100;
|
uint32_t RemainingSpace = (MaxMarshaledJsonSize - EstimatedUsedMarshaledJsonSize) - 100;
|
||||||
// Serial.println(String(F("prepareJSONChunk: RemainingSpace: ")) + String(RemainingSpace));
|
// 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("prepareJSONChunk: Control ")) + String(control->id) + F(" is too large to be sent to the browser."));
|
||||||
// Serial.println(String(F("ERROR: prepareJSONChunk: value: ")) + control->value);
|
// Serial.println(String(F("ERROR: prepareJSONChunk: value: ")) + control->value);
|
||||||
rootDoc.clear();
|
rootDoc.clear();
|
||||||
item = items.createNestedObject();
|
item = AllocateJsonObject(items);
|
||||||
control->MarshalErrorMessage(item);
|
control->MarshalErrorMessage(item);
|
||||||
elementcount = 0;
|
elementcount = 0;
|
||||||
}
|
}
|
||||||
@ -496,7 +499,7 @@ bool ESPUIclient::SendControlsToClient(uint16_t startidx, ClientUpdateType_t Tra
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
DynamicJsonDocument document(ESPUI.jsonInitialDocumentSize);
|
AllocateJsonDocument(document, ESPUI.jsonInitialDocumentSize);
|
||||||
FillInHeader(document);
|
FillInHeader(document);
|
||||||
document[F("startindex")] = startidx;
|
document[F("startindex")] = startidx;
|
||||||
document[F("totalcontrols")] = uint16_t(-1); // ESPUI.controlCount;
|
document[F("totalcontrols")] = uint16_t(-1); // ESPUI.controlCount;
|
||||||
@ -544,7 +547,7 @@ bool ESPUIclient::SendControlsToClient(uint16_t startidx, ClientUpdateType_t Tra
|
|||||||
return Response;
|
return Response;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ESPUIclient::SendJsonDocToWebSocket(DynamicJsonDocument& document)
|
bool ESPUIclient::SendJsonDocToWebSocket(JsonDocument& document)
|
||||||
{
|
{
|
||||||
bool Response = true;
|
bool Response = true;
|
||||||
|
|
||||||
|
@ -43,8 +43,8 @@ protected:
|
|||||||
// bool NeedsNotification() { return pCurrentFsmState != &fsm_EspuiClient_state_Idle_imp; }
|
// bool NeedsNotification() { return pCurrentFsmState != &fsm_EspuiClient_state_Idle_imp; }
|
||||||
|
|
||||||
bool CanSend();
|
bool CanSend();
|
||||||
void FillInHeader(ArduinoJson::DynamicJsonDocument& document);
|
void FillInHeader(ArduinoJson::JsonDocument& document);
|
||||||
uint32_t prepareJSONChunk(uint16_t startindex, DynamicJsonDocument& rootDoc, bool InUpdateMode, String value);
|
uint32_t prepareJSONChunk(uint16_t startindex, JsonDocument& rootDoc, bool InUpdateMode, String value);
|
||||||
bool SendControlsToClient(uint16_t startidx, ClientUpdateType_t TransferMode, String FragmentRequest);
|
bool SendControlsToClient(uint16_t startidx, ClientUpdateType_t TransferMode, String FragmentRequest);
|
||||||
|
|
||||||
bool SendClientNotification(ClientUpdateType_t value);
|
bool SendClientNotification(ClientUpdateType_t value);
|
||||||
@ -62,6 +62,6 @@ public:
|
|||||||
bool IsSyncronized();
|
bool IsSyncronized();
|
||||||
uint32_t id() { return client->id(); }
|
uint32_t id() { return client->id(); }
|
||||||
void SetState(ClientUpdateType_t value);
|
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("offset")] = StartingOffset;
|
||||||
_item[F("length")] = ValueLenToSend;
|
_item[F("length")] = ValueLenToSend;
|
||||||
_item[F("total")] = value.length();
|
_item[F("total")] = value.length();
|
||||||
item = _item.createNestedObject(F("control"));
|
AllocateNamedJsonObject(item, _item, F("control"));
|
||||||
}
|
}
|
||||||
|
|
||||||
item[F("id")] = id;
|
item[F("id")] = id;
|
||||||
|
Loading…
Reference in New Issue
Block a user