json обмен между вебом и esp

This commit is contained in:
Dmitry Borisenko
2021-12-25 00:19:58 +01:00
parent 875f5009c2
commit 11120a873f
5 changed files with 67 additions and 68 deletions

File diff suppressed because one or more lines are too long

View File

@@ -77,6 +77,6 @@ extern String settingsFlashJson;
extern String paramsFlashJson;
extern String paramsHeapJson;
extern DynamicJsonDocument settingsFlashJsonDoc;
extern DynamicJsonDocument paramsFlashJsonDoc;
extern DynamicJsonDocument paramsHeapJsonDoc;
// extern DynamicJsonDocument settingsFlashJsonDoc;
// extern DynamicJsonDocument paramsFlashJsonDoc;
// extern DynamicJsonDocument paramsHeapJsonDoc;

View File

@@ -32,6 +32,6 @@ String settingsFlashJson = "{}"; //переменная в которой хр
String paramsFlashJson = "{}"; //переменная в которой хранятся все параметры, находится в оперативной памяти и синхронизированна с flash памятью
String paramsHeapJson = "{}"; //переменная в которой хранятся все параметры, находится в оперативной памяти только
DynamicJsonDocument settingsFlashJsonDoc(JSON_BUFFER_SIZE);
DynamicJsonDocument paramsFlashJsonDoc(JSON_BUFFER_SIZE);
DynamicJsonDocument paramsHeapJsonDoc(JSON_BUFFER_SIZE);
// DynamicJsonDocument settingsFlashJsonDoc(JSON_BUFFER_SIZE);
// DynamicJsonDocument paramsFlashJsonDoc(JSON_BUFFER_SIZE);
// DynamicJsonDocument paramsHeapJsonDoc(JSON_BUFFER_SIZE);

View File

@@ -14,7 +14,8 @@ void StreamJsonArray::sendFile(String path, uint8_t num) {
void StreamJsonArray::loop() {
// if (ws.availableForWriteAll()) {
if (file.available()) {
String jsonArrayElement = file.readStringUntil('}') + "}";
String jsonArrayElement = _path + file.readStringUntil('}') + "}";
//jsonArrayElement.replace("]}", "]");
// Serial.println(jsonArrayElement);
standWebSocket.sendTXT(_num, jsonArrayElement);
// ws.textAll(jsonArrayElement);

View File

@@ -2,7 +2,7 @@
#include "Utils/FileUtils.h"
//================================================================================
// new================================================================================
String jsonReadStrDoc(DynamicJsonDocument& doc, String name) {
return doc[name].as<String>();
}
@@ -57,6 +57,63 @@ bool jsonRead(String& json, String key, int& value) {
return ret;
}
// new==============================================================================
bool jsonWriteStr_(String& json, String key, String value) {
bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str());
ret = false;
}
doc[key] = value;
json = "";
serializeJson(doc, json);
return ret;
}
bool jsonWriteBool_(String& json, String key, bool value) {
bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str());
ret = false;
}
doc[key] = value;
json = "";
serializeJson(doc, json);
return ret;
}
bool jsonWriteInt_(String& json, String key, int value) {
bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str());
ret = false;
}
doc[key] = value;
json = "";
serializeJson(doc, json);
return ret;
}
bool jsonWriteFloat_(String& json, String key, float value) {
bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str());
ret = false;
}
doc[key] = value;
json = "";
serializeJson(doc, json);
return ret;
}
// depricated======================================================================
String jsonReadStr(String& json, String name) {
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
@@ -119,62 +176,3 @@ String jsonWriteFloat(String& json, String name, float value) {
serializeJson(doc, json);
return json;
}
// new==============================================================================
bool jsonWriteStr_(String& json, String key, String value) {
bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str());
ret = false;
}
doc[key] = value;
json = "";
serializeJson(doc, json);
return ret;
}
bool jsonWriteBool_(String& json, String key, bool value) {
bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str());
ret = false;
}
doc[key] = value;
json = "";
serializeJson(doc, json);
return ret;
}
bool jsonWriteInt_(String& json, String key, int value) {
bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str());
ret = false;
}
doc[key] = value;
json = "";
serializeJson(doc, json);
return ret;
}
bool jsonWriteFloat_(String& json, String key, float value) {
bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str());
ret = false;
}
doc[key] = value;
json = "";
serializeJson(doc, json);
return ret;
}
//=================================================================================