Управление ошибками

This commit is contained in:
Dmitry Borisenko
2022-02-09 21:55:17 +01:00
parent a3c3e4055a
commit 02a087ccdd
6 changed files with 107 additions and 11 deletions

File diff suppressed because one or more lines are too long

View File

@@ -3,4 +3,5 @@
#include "WsServer.h"
#include "Utils/TimeUtils.h"
extern void periodicTasksInit();
extern void periodicTasksInit();
extern void printGlobalVarSize();

View File

@@ -24,3 +24,7 @@ extern bool jsonWriteStr_(String& json, String name, String value);
extern bool jsonWriteBool_(String& json, String name, bool value);
extern bool jsonWriteInt_(String& json, String name, int value);
extern bool jsonWriteFloat_(String& json, String name, float value);
void writeUint8tValueToJsonString(uint8_t* payload, size_t length, size_t headerLenth, String& json);
extern bool jsonMerge(String& json1, String& json2);
extern void jsonMergeDocs(JsonObject dest, JsonObjectConst src);
extern void jsonErrorDetected();

View File

@@ -7,6 +7,7 @@ void periodicTasksInit() {
// heap
String heap = prettyBytes(ESP.getFreeHeap());
SerialPrint(F("i"), F("HEAP"), heap);
printGlobalVarSize();
jsonWriteStr_(errorsHeapJson, F("heap"), heap);
// rssi
jsonWriteInt_(errorsHeapJson, F("rssi"), RSSIquality());
@@ -17,4 +18,22 @@ void periodicTasksInit() {
periodicWsSend();
},
nullptr, true);
}
void printGlobalVarSize() {
size_t settingsFlashJsonSize = settingsFlashJson.length();
SerialPrint(F("i"), F("settingsFlashJson"), String(settingsFlashJsonSize));
size_t errorsHeapJsonSize = errorsHeapJson.length();
SerialPrint(F("i"), F("settingsFlashJson"), String(errorsHeapJsonSize));
size_t paramsFlashJsonSize = paramsFlashJson.length();
SerialPrint(F("i"), F("settingsFlashJson"), String(paramsFlashJsonSize));
size_t paramsHeapJsonSize = paramsHeapJson.length();
SerialPrint(F("i"), F("settingsFlashJson"), String(paramsHeapJsonSize));
size_t halfBuffer = JSON_BUFFER_SIZE / 2;
if (settingsFlashJsonSize > halfBuffer || errorsHeapJsonSize > halfBuffer || paramsFlashJsonSize > halfBuffer || paramsHeapJsonSize > halfBuffer) {
SerialPrint(F("EE"), F("Json"), "Insufficient buffer size!!!");
jsonWriteInt(errorsHeapJson, "jsbuf", 1);
}
}

View File

@@ -100,7 +100,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length)
// system ===================================================================
//**сохранение**//
if (headerStr == "/rorre|") {
writeUint8tToString(payload, length, headerLenth, errorsHeapJson);
writeUint8tValueToJsonString(payload, length, headerLenth, errorsHeapJson);
}
// orders ===================================================================
if (headerStr == "/reboot|") {

View File

@@ -1,5 +1,4 @@
#include "Utils/JsonUtils.h"
#include "Utils/FileUtils.h"
// new================================================================================
@@ -18,9 +17,11 @@ bool jsonRead(String& json, String key, unsigned long& value) {
DeserializationError error = deserializeJson(doc, json);
if (error) {
SerialPrint("EE", F("jsonRead"), error.f_str());
jsonErrorDetected();
ret = false;
} else if (!doc.containsKey(key)) {
SerialPrint("EE", F("jsonRead"), key + " missing");
jsonErrorDetected();
ret = false;
}
value = doc[key].as<unsigned long>();
@@ -33,9 +34,11 @@ bool jsonRead(String& json, String key, float& value) {
DeserializationError error = deserializeJson(doc, json);
if (error) {
SerialPrint("EE", F("jsonRead"), error.f_str());
jsonErrorDetected();
ret = false;
} else if (!doc.containsKey(key)) {
SerialPrint("EE", F("jsonRead"), key + " missing");
jsonErrorDetected();
ret = false;
}
value = doc[key].as<float>();
@@ -48,9 +51,11 @@ bool jsonRead(String& json, String key, String& value) {
DeserializationError error = deserializeJson(doc, json);
if (error) {
SerialPrint("EE", F("jsonRead"), error.f_str());
jsonErrorDetected();
ret = false;
} else if (!doc.containsKey(key)) {
SerialPrint("EE", F("jsonRead"), key + " missing");
jsonErrorDetected();
ret = false;
}
value = doc[key].as<String>();
@@ -63,9 +68,11 @@ bool jsonRead(String& json, String key, bool& value) {
DeserializationError error = deserializeJson(doc, json);
if (error) {
SerialPrint("EE", F("jsonRead"), error.f_str());
jsonErrorDetected();
ret = false;
} else if (!doc.containsKey(key)) {
SerialPrint("EE", F("jsonRead"), key + " missing");
jsonErrorDetected();
ret = false;
}
value = doc[key].as<bool>();
@@ -78,9 +85,11 @@ bool jsonRead(String& json, String key, int& value) {
DeserializationError error = deserializeJson(doc, json);
if (error) {
SerialPrint("EE", F("jsonRead"), error.f_str());
jsonErrorDetected();
ret = false;
} else if (!doc.containsKey(key)) {
SerialPrint("EE", F("jsonRead"), key + " missing");
jsonErrorDetected();
ret = false;
}
value = doc[key].as<int>();
@@ -94,6 +103,7 @@ bool jsonWriteStr_(String& json, String key, String value) {
DeserializationError error = deserializeJson(doc, json);
if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str());
jsonErrorDetected();
ret = false;
}
doc[key] = value;
@@ -108,6 +118,7 @@ bool jsonWriteBool_(String& json, String key, bool value) {
DeserializationError error = deserializeJson(doc, json);
if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str());
jsonErrorDetected();
ret = false;
}
doc[key] = value;
@@ -122,6 +133,7 @@ bool jsonWriteInt_(String& json, String key, int value) {
DeserializationError error = deserializeJson(doc, json);
if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str());
jsonErrorDetected();
ret = false;
}
doc[key] = value;
@@ -136,6 +148,7 @@ bool jsonWriteFloat_(String& json, String key, float value) {
DeserializationError error = deserializeJson(doc, json);
if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str());
jsonErrorDetected();
ret = false;
}
doc[key] = value;
@@ -144,25 +157,66 @@ bool jsonWriteFloat_(String& json, String key, float value) {
return ret;
}
void writeUint8tValueToJsonString(uint8_t* payload, size_t length, size_t headerLenth, String& json) {
String payloadStr;
payloadStr.reserve(length + 1);
for (size_t i = headerLenth; i < length; i++) {
payloadStr += (char)payload[i];
}
jsonMerge(json, payloadStr);
}
bool jsonMerge(String& json1, String& json2) {
bool ret = true;
DynamicJsonDocument doc1(JSON_BUFFER_SIZE);
DeserializationError error1 = deserializeJson(doc1, json1);
DynamicJsonDocument doc2(JSON_BUFFER_SIZE);
DeserializationError error2 = deserializeJson(doc2, json2);
jsonMergeDocs(doc1.as<JsonObject>(), doc2.as<JsonObject>());
if (error1 || error2) {
SerialPrint("EE", F("json"), "jsonMerge error");
jsonErrorDetected();
ret = false;
}
json1 = "";
serializeJson(doc1, json1);
return ret;
}
void jsonMergeDocs(JsonObject dest, JsonObjectConst src) {
for (auto kvp : src) {
dest[kvp.key()] = kvp.value();
}
}
// depricated======================================================================
String jsonReadStr(String& json, String name) {
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) SerialPrint("EE", F("jsonRead"), error.f_str());
if (error) {
SerialPrint("EE", F("jsonRead"), error.f_str());
jsonErrorDetected();
}
return doc[name].as<String>();
}
boolean jsonReadBool(String& json, String name) {
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) SerialPrint("EE", F("jsonRead"), error.f_str());
if (error) {
SerialPrint("EE", F("jsonRead"), error.f_str());
jsonErrorDetected();
}
return doc[name].as<bool>();
}
int jsonReadInt(String& json, String name) {
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) SerialPrint("EE", F("jsonRead"), error.f_str());
if (error) {
SerialPrint("EE", F("jsonRead"), error.f_str());
jsonErrorDetected();
}
return doc[name].as<int>();
}
@@ -170,7 +224,10 @@ int jsonReadInt(String& json, String name) {
String jsonWriteStr(String& json, String name, String value) {
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) SerialPrint("EE", F("jsonWrite"), error.f_str());
if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str());
jsonErrorDetected();
}
doc[name] = value;
json = "";
serializeJson(doc, json);
@@ -180,7 +237,10 @@ String jsonWriteStr(String& json, String name, String value) {
String jsonWriteBool(String& json, String name, boolean value) {
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) SerialPrint("EE", F("jsonWrite"), error.f_str());
if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str());
jsonErrorDetected();
}
doc[name] = value;
json = "";
serializeJson(doc, json);
@@ -190,7 +250,10 @@ String jsonWriteBool(String& json, String name, boolean value) {
String jsonWriteInt(String& json, String name, int value) {
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) SerialPrint("EE", F("jsonWrite"), error.f_str());
if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str());
jsonErrorDetected();
}
doc[name] = value;
json = "";
serializeJson(doc, json);
@@ -200,9 +263,18 @@ String jsonWriteInt(String& json, String name, int value) {
String jsonWriteFloat(String& json, String name, float value) {
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) SerialPrint("EE", F("jsonWrite"), error.f_str());
if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str());
jsonErrorDetected();
}
doc[name] = value;
json = "";
serializeJson(doc, json);
return json;
}
void jsonErrorDetected() {
int number = jsonReadInt(errorsHeapJson, F("jserr"));
number++;
jsonWriteInt(errorsHeapJson, F("jserr"), number);
}