убрали лишние ошибки json

This commit is contained in:
Dmitry Borisenko
2022-02-22 16:40:46 +01:00
parent f4f93a32db
commit 993b2b25fd
12 changed files with 208 additions and 137 deletions

View File

@@ -106,6 +106,8 @@ extern String mqttPass;
extern unsigned long mqttUptime; extern unsigned long mqttUptime;
extern unsigned long flashWriteNumber; extern unsigned long flashWriteNumber;
extern unsigned long wifiUptime;
extern String mqttRootDevice; extern String mqttRootDevice;
extern String chipId; extern String chipId;
extern String prex; extern String prex;

View File

@@ -31,3 +31,6 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length);
void handleMqttStatus(bool send); void handleMqttStatus(bool send);
void handleMqttStatus(bool send, int state); void handleMqttStatus(bool send, int state);
const String getStateStr(int e); const String getStateStr(int e);
void mqttUptimeCalc();
void wifiUptimeCalc();

View File

@@ -5,26 +5,26 @@
extern String jsonReadStrDoc(DynamicJsonDocument& doc, String name); extern String jsonReadStrDoc(DynamicJsonDocument& doc, String name);
extern void jsonWriteStrDoc(DynamicJsonDocument& doc, String name, String value); extern void jsonWriteStrDoc(DynamicJsonDocument& doc, String name, String value);
extern String jsonWriteStr(String& json, String name, String value); extern String jsonWriteStr(String& json, String name, String value, bool e = true);
extern String jsonWriteInt(String& json, String name, int value); extern String jsonWriteInt(String& json, String name, int value, bool e = true);
extern String jsonWriteFloat(String& json, String name, float value); extern String jsonWriteFloat(String& json, String name, float value, bool e = true);
extern String jsonWriteBool(String& json, String name, boolean value); extern String jsonWriteBool(String& json, String name, boolean value, bool e = true);
extern bool jsonRead(String& json, String key, unsigned long& value); extern bool jsonRead(String& json, String key, unsigned long& value, bool e = true);
extern bool jsonRead(String& json, String key, float& value); extern bool jsonRead(String& json, String key, float& value, bool e = true);
extern bool jsonRead(String& json, String key, String& value); extern bool jsonRead(String& json, String key, String& value, bool e = true);
extern bool jsonRead(String& json, String key, bool& value); extern bool jsonRead(String& json, String key, bool& value, bool e = true);
extern bool jsonRead(String& json, String key, int& value); extern bool jsonRead(String& json, String key, int& value, bool e = true);
extern String jsonReadStr(String& json, String name); extern String jsonReadStr(String& json, String name, bool e = true);
extern int jsonReadInt(String& json, String name); extern int jsonReadInt(String& json, String name, bool e = true);
extern boolean jsonReadBool(String& json, String name); extern boolean jsonReadBool(String& json, String name, bool e = true);
extern bool jsonWriteStr_(String& json, String name, String value); extern bool jsonWriteStr_(String& json, String name, String value, bool e = true);
extern bool jsonWriteBool_(String& json, String name, bool value); extern bool jsonWriteBool_(String& json, String name, bool value, bool e = true);
extern bool jsonWriteInt_(String& json, String name, int value); extern bool jsonWriteInt_(String& json, String name, int value, bool e = true);
extern bool jsonWriteFloat_(String& json, String name, float value); extern bool jsonWriteFloat_(String& json, String name, float value, bool e = true);
void writeUint8tValueToJsonString(uint8_t* payload, size_t length, size_t headerLenth, String& json); void writeUint8tValueToJsonString(uint8_t* payload, size_t length, size_t headerLenth, String& json);
extern bool jsonMergeObjects(String& json1, String& json2); extern bool jsonMergeObjects(String& json1, String& json2, bool e = true);
extern void jsonMergeDocs(JsonObject dest, JsonObjectConst src); extern void jsonMergeDocs(JsonObject dest, JsonObjectConst src);
extern void jsonErrorDetected(); extern void jsonErrorDetected();

View File

@@ -52,13 +52,15 @@ void asyncUdpInit() {
}); });
} }
//будем отправлять каждые 60 секунд презентацию данного устройства //будем отправлять каждые 30 секунд презентацию данного устройства
ts.add( ts.add(
UDP, 30000, [&](void*) { UDP, 30000, [&](void*) {
SerialPrint("i", F("UDP"), F("Broadcast device presentation")); if (isNetworkActive()) {
asyncUdp.broadcastTo(getThisDevice().c_str(), 4210); SerialPrint("i", F("UDP"), F("Broadcast device presentation"));
// asyncUdp.broadcast("test"); asyncUdp.broadcastTo(getThisDevice().c_str(), 4210);
// asyncUdp.print("Hello Server!"); // asyncUdp.broadcast("test");
// asyncUdp.print("Hello Server!");
}
}, },
nullptr, true); nullptr, true);

View File

@@ -57,6 +57,8 @@ String mqttPass = "";
unsigned long mqttUptime = 0; unsigned long mqttUptime = 0;
unsigned long flashWriteNumber = 0; unsigned long flashWriteNumber = 0;
unsigned long wifiUptime = 0;
String chipId = ""; String chipId = "";
String prex = ""; String prex = "";
String all_widgets = ""; String all_widgets = "";

View File

@@ -7,14 +7,12 @@ void mqttInit() {
[&](void*) { [&](void*) {
if (WiFi.status() == WL_CONNECTED) { if (WiFi.status() == WL_CONNECTED) {
SerialPrint("i", F("WIFI"), F("OK")); SerialPrint("i", F("WIFI"), F("OK"));
wifiUptimeCalc();
if (mqtt.connected()) { if (mqtt.connected()) {
SerialPrint("i", F("MQTT"), "OK"); SerialPrint("i", F("MQTT"), "OK");
mqttUptimeCalc();
handleMqttStatus(false); handleMqttStatus(false);
static unsigned int prevMillis;
mqttUptime = mqttUptime + (millis() - prevMillis);
prevMillis = millis();
// setLedStatus(LED_OFF); // setLedStatus(LED_OFF);
} else { } else {
SerialPrint("E", F("MQTT"), F("✖ Connection lost")); SerialPrint("E", F("MQTT"), F("✖ Connection lost"));
@@ -25,6 +23,7 @@ void mqttInit() {
} else { } else {
SerialPrint("E", F("WIFI"), F("✖ Lost WiFi connection")); SerialPrint("E", F("WIFI"), F("✖ Lost WiFi connection"));
ts.remove(WIFI_MQTT_CONNECTION_CHECK); ts.remove(WIFI_MQTT_CONNECTION_CHECK);
wifiUptime = 0;
startAPMode(); startAPMode();
} }
}, },
@@ -347,3 +346,15 @@ const String getStateStr(int e) {
break; break;
} }
} }
void mqttUptimeCalc() {
static unsigned int prevMillis;
mqttUptime = mqttUptime + (millis() - prevMillis);
prevMillis = millis();
}
void wifiUptimeCalc() {
static unsigned int prevMillis;
wifiUptime = wifiUptime + (millis() - prevMillis);
prevMillis = millis();
}

View File

@@ -14,11 +14,14 @@ void periodicTasksInit() {
// uptime // uptime
jsonWriteStr_(errorsHeapJson, F("upt"), prettyMillis(millis())); jsonWriteStr_(errorsHeapJson, F("upt"), prettyMillis(millis()));
jsonWriteStr_(errorsHeapJson, F("uptm"), prettyMillis(mqttUptime)); jsonWriteStr_(errorsHeapJson, F("uptm"), prettyMillis(mqttUptime));
jsonWriteStr_(errorsHeapJson, F("uptw"), prettyMillis(wifiUptime));
// flash // flash
jsonWriteInt_(errorsHeapJson, F("fl"), flashWriteNumber); jsonWriteInt_(errorsHeapJson, F("fl"), flashWriteNumber);
// build ver // build ver
jsonWriteStr_(errorsHeapJson, F("bver"), String(FIRMWARE_VERSION)); jsonWriteStr_(errorsHeapJson, F("bver"), String(FIRMWARE_VERSION));
jsonWriteStr_(errorsHeapJson, F("bn"), String(FIRMWARE_NAME)); jsonWriteStr_(errorsHeapJson, F("bn"), String(FIRMWARE_NAME));
// reset reason
jsonWriteStr_(errorsHeapJson, F("rst"), ESP_getResetReason());
periodicWsSend(); periodicWsSend();
}, },
nullptr, true); nullptr, true);
@@ -35,15 +38,15 @@ void handleError(String errorId, int errorValue) {
void printGlobalVarSize() { void printGlobalVarSize() {
size_t settingsFlashJsonSize = settingsFlashJson.length(); size_t settingsFlashJsonSize = settingsFlashJson.length();
SerialPrint(F("i"), F("settingsFlashJson"), String(settingsFlashJsonSize)); // SerialPrint(F("i"), F("settingsFlashJson"), String(settingsFlashJsonSize));
size_t errorsHeapJsonSize = errorsHeapJson.length(); size_t errorsHeapJsonSize = errorsHeapJson.length();
SerialPrint(F("i"), F("errorsHeapJson"), String(errorsHeapJsonSize)); // SerialPrint(F("i"), F("errorsHeapJson"), String(errorsHeapJsonSize));
size_t paramsFlashJsonSize = paramsFlashJson.length(); size_t paramsFlashJsonSize = paramsFlashJson.length();
SerialPrint(F("i"), F("paramsFlashJson"), String(paramsFlashJsonSize)); // SerialPrint(F("i"), F("paramsFlashJson"), String(paramsFlashJsonSize));
size_t paramsHeapJsonSize = paramsHeapJson.length(); size_t paramsHeapJsonSize = paramsHeapJson.length();
SerialPrint(F("i"), F("paramsHeapJson"), String(paramsHeapJsonSize)); // SerialPrint(F("i"), F("paramsHeapJson"), String(paramsHeapJsonSize));
size_t devListHeapJsonSize = devListHeapJson.length(); size_t devListHeapJsonSize = devListHeapJson.length();
SerialPrint(F("i"), F("devListHeapJson"), String(devListHeapJsonSize)); // SerialPrint(F("i"), F("devListHeapJson"), String(devListHeapJsonSize));
size_t halfBuffer = JSON_BUFFER_SIZE / 2; size_t halfBuffer = JSON_BUFFER_SIZE / 2;

View File

@@ -6,16 +6,16 @@
#include "ESPConfiguration.h" #include "ESPConfiguration.h"
IoTItem::IoTItem(String parameters) { IoTItem::IoTItem(String parameters) {
jsonRead(parameters, "int", _interval); jsonRead(parameters, F("int"), _interval);
_interval = _interval * 1000; _interval = _interval * 1000;
jsonRead(parameters, "subtype", _subtype); jsonRead(parameters, F("subtype"), _subtype);
jsonRead(parameters, "id", _id); jsonRead(parameters, F("id"), _id);
jsonRead(parameters, "multiply", _multiply); jsonRead(parameters, F("multiply"), _multiply, false);
jsonRead(parameters, "plus", _plus); jsonRead(parameters, F("plus"), _plus, false);
jsonRead(parameters, "round", _round); jsonRead(parameters, F("round"), _round, false);
String map; String map;
jsonRead(parameters, "map", map); jsonRead(parameters, F("map"), map, false);
if (map != "") { if (map != "") {
_map1 = selectFromMarkerToMarker(map, ",", 0).toInt(); _map1 = selectFromMarkerToMarker(map, ",", 0).toInt();
_map2 = selectFromMarkerToMarker(map, ",", 1).toInt(); _map2 = selectFromMarkerToMarker(map, ",", 1).toInt();
@@ -63,7 +63,7 @@ void IoTItem::regEvent(float value, String consoleInfo = "") {
value = value / _round; value = value / _round;
} }
//value = (float)value / (_round ? pow(10, (int)_round) : 1); // todo: решить как указывать округление, количество знаков после запятой или десятые сотые ... // value = (float)value / (_round ? pow(10, (int)_round) : 1); // todo: решить как указывать округление, количество знаков после запятой или десятые сотые ...
} }
if (_map1 != _map2) value = map(value, _map1, _map2, _map3, _map4); if (_map1 != _map2) value = map(value, _map1, _map2, _map3, _map4);

View File

@@ -4,14 +4,12 @@
https://github.com/adafruit/Adafruit_BME280_Library https://github.com/adafruit/Adafruit_BME280_Library
******************************************************************/ ******************************************************************/
#include "Global.h" #include "Global.h"
#include "classes/IoTItem.h" #include "classes/IoTItem.h"
#include <Adafruit_BME280.h> #include <Adafruit_BME280.h>
#include <map> #include <map>
std::map<String, Adafruit_BME280*> bmes; std::map<String, Adafruit_BME280*> bmes;
class Bme280t : public IoTItem { class Bme280t : public IoTItem {
@@ -19,45 +17,47 @@ class Bme280t : public IoTItem {
Adafruit_BME280* _bme; Adafruit_BME280* _bme;
public: public:
Bme280t(Adafruit_BME280* bme, String parameters): IoTItem(parameters) { Bme280t(Adafruit_BME280* bme, String parameters) : IoTItem(parameters) {
_bme = bme; _bme = bme;
} }
void doByInterval() { void doByInterval() {
value.valD = _bme->readTemperature(); value.valD = _bme->readTemperature();
if (value.valD < 145) regEvent(value.valD, "Bme280t"); if (value.valD < 145)
else SerialPrint("E", "Sensor Bme280t", "Error"); regEvent(value.valD, "Bme280t");
else
SerialPrint("E", "Sensor Bme280t", "Error");
} }
~Bme280t(); ~Bme280t();
}; };
class Bme280h : public IoTItem { class Bme280h : public IoTItem {
private: private:
Adafruit_BME280* _bme; Adafruit_BME280* _bme;
public: public:
Bme280h(Adafruit_BME280* bme, String parameters): IoTItem(parameters) { Bme280h(Adafruit_BME280* bme, String parameters) : IoTItem(parameters) {
_bme = bme; _bme = bme;
} }
void doByInterval() { void doByInterval() {
value.valD = _bme->readHumidity(); value.valD = _bme->readHumidity();
if (value.valD < 100) regEvent(value.valD, "Bme280h"); if (value.valD < 100)
else SerialPrint("E", "Sensor Bme280h", "Error"); regEvent(value.valD, "Bme280h");
else
SerialPrint("E", "Sensor Bme280h", "Error");
} }
~Bme280h(); ~Bme280h();
}; };
class Bme280p : public IoTItem { class Bme280p : public IoTItem {
private: private:
Adafruit_BME280* _bme; Adafruit_BME280* _bme;
public: public:
Bme280p(Adafruit_BME280* bme, String parameters): IoTItem(parameters) { Bme280p(Adafruit_BME280* bme, String parameters) : IoTItem(parameters) {
_bme = bme; _bme = bme;
} }
@@ -66,13 +66,13 @@ class Bme280p : public IoTItem {
if (value.valD > 0) { if (value.valD > 0) {
value.valD = value.valD / 1.333224 / 100; value.valD = value.valD / 1.333224 / 100;
regEvent(value.valD, "Bme280p"); regEvent(value.valD, "Bme280p");
} else SerialPrint("E", "Sensor Bme280p", "Error"); } else
SerialPrint("E", "Sensor Bme280p", "Error");
} }
~Bme280p(); ~Bme280p();
}; };
void* getAPI_Bme280(String subtype, String param) { void* getAPI_Bme280(String subtype, String param) {
String addr; String addr;
jsonRead(param, "addr", addr); jsonRead(param, "addr", addr);

View File

@@ -12,13 +12,13 @@ std::map<int, DallasTemperature*> sensorsTemperatureArray;
class Ds18b20 : public IoTItem { class Ds18b20 : public IoTItem {
private: private:
//для работы библиотеки с несколькими линиями необходимо обеспечить каждый экземпляр класса ссылками на объекты настроенные на эти линии //для работы библиотеки с несколькими линиями необходимо обеспечить каждый экземпляр класса ссылками на объекты настроенные на эти линии
OneWire* oneWire; OneWire* oneWire;
DallasTemperature* sensors; DallasTemperature* sensors;
//описание параметров передаваемых из настроек датчика из веба //описание параметров передаваемых из настроек датчика из веба
String _addr; String _addr;
int _pin; int _pin;
int _index; int _index;
public: public:
//======================================================================================================= //=======================================================================================================
@@ -27,10 +27,10 @@ class Ds18b20 : public IoTItem {
//Такие как ...begin и подставлять в них параметры полученные из web интерфейса. //Такие как ...begin и подставлять в них параметры полученные из web интерфейса.
//Все параметры хранятся в перемененной parameters, вы можете прочитать любой параметр используя jsonRead функции: //Все параметры хранятся в перемененной parameters, вы можете прочитать любой параметр используя jsonRead функции:
// jsonReadStr, jsonReadBool, jsonReadInt // jsonReadStr, jsonReadBool, jsonReadInt
Ds18b20(String parameters): IoTItem(parameters) { Ds18b20(String parameters) : IoTItem(parameters) {
jsonRead(parameters, "pin", _pin); jsonRead(parameters, "pin", _pin);
jsonRead(parameters, "index", _index); jsonRead(parameters, "index", _index, false);
jsonRead(parameters, "addr", _addr); jsonRead(parameters, "addr", _addr, false);
//учитываем, что библиотека может работать с несколькими линиями на разных пинах, поэтому инициируем библиотеку, если линия ранее не использовалась //учитываем, что библиотека может работать с несколькими линиями на разных пинах, поэтому инициируем библиотеку, если линия ранее не использовалась
if (oneWireTemperatureArray.find(_pin) == oneWireTemperatureArray.end()) { if (oneWireTemperatureArray.find(_pin) == oneWireTemperatureArray.end()) {
@@ -74,8 +74,10 @@ class Ds18b20 : public IoTItem {
char addrStr[20] = ""; char addrStr[20] = "";
hex2string(deviceAddress, 8, addrStr); hex2string(deviceAddress, 8, addrStr);
if (value.valD != -127) regEvent(value.valD, "addr: " + String(addrStr)); //обязательный вызов для отправки результата работы if (value.valD != -127)
else SerialPrint("E", "Sensor Ds18b20", "Error"); regEvent(value.valD, "addr: " + String(addrStr)); //обязательный вызов для отправки результата работы
else
SerialPrint("E", "Sensor Ds18b20", "Error");
} }
//======================================================================================================= //=======================================================================================================

View File

@@ -11,85 +11,105 @@ void jsonWriteStrDoc(DynamicJsonDocument& doc, String name, String value) {
} }
// new============================================================================== // new==============================================================================
bool jsonRead(String& json, String key, unsigned long& value) { bool jsonRead(String& json, String key, unsigned long& value, bool e) {
bool ret = true; bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json); DeserializationError error = deserializeJson(doc, json);
if (error) { if (error) {
SerialPrint("EE", F("jsonRead"), error.f_str()); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("jsonRead"), error.f_str());
jsonErrorDetected();
}
ret = false; ret = false;
} else if (!doc.containsKey(key)) { } else if (!doc.containsKey(key)) {
SerialPrint("EE", F("jsonRead"), key + " missing"); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("jsonRead"), key + " missing");
jsonErrorDetected();
}
ret = false; ret = false;
} }
value = doc[key].as<unsigned long>(); value = doc[key].as<unsigned long>();
return ret; return ret;
} }
bool jsonRead(String& json, String key, float& value) { bool jsonRead(String& json, String key, float& value, bool e) {
bool ret = true; bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json); DeserializationError error = deserializeJson(doc, json);
if (error) { if (error) {
SerialPrint("EE", F("jsonRead"), error.f_str()); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("jsonRead"), error.f_str());
jsonErrorDetected();
}
ret = false; ret = false;
} else if (!doc.containsKey(key)) { } else if (!doc.containsKey(key)) {
SerialPrint("EE", F("jsonRead"), key + " missing"); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("jsonRead"), key + " missing");
jsonErrorDetected();
}
ret = false; ret = false;
} }
value = doc[key].as<float>(); value = doc[key].as<float>();
return ret; return ret;
} }
bool jsonRead(String& json, String key, String& value) { bool jsonRead(String& json, String key, String& value, bool e) {
bool ret = true; bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json); DeserializationError error = deserializeJson(doc, json);
if (error) { if (error) {
SerialPrint("EE", F("jsonRead"), error.f_str()); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("jsonRead"), error.f_str());
jsonErrorDetected();
}
ret = false; ret = false;
} else if (!doc.containsKey(key)) { } else if (!doc.containsKey(key)) {
SerialPrint("EE", F("jsonRead"), key + " missing"); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("jsonRead"), key + " missing");
jsonErrorDetected();
}
ret = false; ret = false;
} }
value = doc[key].as<String>(); value = doc[key].as<String>();
return ret; return ret;
} }
bool jsonRead(String& json, String key, bool& value) { bool jsonRead(String& json, String key, bool& value, bool e) {
bool ret = true; bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json); DeserializationError error = deserializeJson(doc, json);
if (error) { if (error) {
SerialPrint("EE", F("jsonRead"), error.f_str()); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("jsonRead"), error.f_str());
jsonErrorDetected();
}
ret = false; ret = false;
} else if (!doc.containsKey(key)) { } else if (!doc.containsKey(key)) {
SerialPrint("EE", F("jsonRead"), key + " missing"); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("jsonRead"), key + " missing");
jsonErrorDetected();
}
ret = false; ret = false;
} }
value = doc[key].as<bool>(); value = doc[key].as<bool>();
return ret; return ret;
} }
bool jsonRead(String& json, String key, int& value) { bool jsonRead(String& json, String key, int& value, bool e) {
bool ret = true; bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json); DeserializationError error = deserializeJson(doc, json);
if (error) { if (error) {
SerialPrint("EE", F("jsonRead"), error.f_str()); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("jsonRead"), error.f_str());
jsonErrorDetected();
}
ret = false; ret = false;
} else if (!doc.containsKey(key)) { } else if (!doc.containsKey(key)) {
SerialPrint("EE", F("jsonRead"), key + " missing"); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("jsonRead"), key + " missing");
jsonErrorDetected();
}
ret = false; ret = false;
} }
value = doc[key].as<int>(); value = doc[key].as<int>();
@@ -97,13 +117,15 @@ bool jsonRead(String& json, String key, int& value) {
} }
// new============================================================================== // new==============================================================================
bool jsonWriteStr_(String& json, String key, String value) { bool jsonWriteStr_(String& json, String key, String value, bool e) {
bool ret = true; bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json); DeserializationError error = deserializeJson(doc, json);
if (error) { if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str()); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("jsonWrite"), error.f_str());
jsonErrorDetected();
}
ret = false; ret = false;
} }
doc[key] = value; doc[key] = value;
@@ -112,13 +134,15 @@ bool jsonWriteStr_(String& json, String key, String value) {
return ret; return ret;
} }
bool jsonWriteBool_(String& json, String key, bool value) { bool jsonWriteBool_(String& json, String key, bool value, bool e) {
bool ret = true; bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json); DeserializationError error = deserializeJson(doc, json);
if (error) { if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str()); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("jsonWrite"), error.f_str());
jsonErrorDetected();
}
ret = false; ret = false;
} }
doc[key] = value; doc[key] = value;
@@ -127,13 +151,15 @@ bool jsonWriteBool_(String& json, String key, bool value) {
return ret; return ret;
} }
bool jsonWriteInt_(String& json, String key, int value) { bool jsonWriteInt_(String& json, String key, int value, bool e) {
bool ret = true; bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json); DeserializationError error = deserializeJson(doc, json);
if (error) { if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str()); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("jsonWrite"), error.f_str());
jsonErrorDetected();
}
ret = false; ret = false;
} }
doc[key] = value; doc[key] = value;
@@ -142,13 +168,15 @@ bool jsonWriteInt_(String& json, String key, int value) {
return ret; return ret;
} }
bool jsonWriteFloat_(String& json, String key, float value) { bool jsonWriteFloat_(String& json, String key, float value, bool e) {
bool ret = true; bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json); DeserializationError error = deserializeJson(doc, json);
if (error) { if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str()); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("jsonWrite"), error.f_str());
jsonErrorDetected();
}
ret = false; ret = false;
} }
doc[key] = value; doc[key] = value;
@@ -166,7 +194,7 @@ void writeUint8tValueToJsonString(uint8_t* payload, size_t length, size_t header
jsonMergeObjects(json, payloadStr); jsonMergeObjects(json, payloadStr);
} }
bool jsonMergeObjects(String& json1, String& json2) { bool jsonMergeObjects(String& json1, String& json2, bool e) {
bool ret = true; bool ret = true;
DynamicJsonDocument doc1(JSON_BUFFER_SIZE); DynamicJsonDocument doc1(JSON_BUFFER_SIZE);
DeserializationError error1 = deserializeJson(doc1, json1); DeserializationError error1 = deserializeJson(doc1, json1);
@@ -174,8 +202,10 @@ bool jsonMergeObjects(String& json1, String& json2) {
DeserializationError error2 = deserializeJson(doc2, json2); DeserializationError error2 = deserializeJson(doc2, json2);
jsonMergeDocs(doc1.as<JsonObject>(), doc2.as<JsonObject>()); jsonMergeDocs(doc1.as<JsonObject>(), doc2.as<JsonObject>());
if (error1 || error2) { if (error1 || error2) {
SerialPrint("EE", F("json"), "jsonMergeObjects error"); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("json"), "jsonMergeObjects error");
jsonErrorDetected();
}
ret = false; ret = false;
} }
json1 = ""; json1 = "";
@@ -190,43 +220,51 @@ void jsonMergeDocs(JsonObject dest, JsonObjectConst src) {
} }
// depricated====================================================================== // depricated======================================================================
String jsonReadStr(String& json, String name) { String jsonReadStr(String& json, String name, bool e) {
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json); DeserializationError error = deserializeJson(doc, json);
if (error) { if (error) {
SerialPrint("EE", F("jsonRead"), error.f_str()); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("jsonRead"), error.f_str());
jsonErrorDetected();
}
} }
return doc[name].as<String>(); return doc[name].as<String>();
} }
boolean jsonReadBool(String& json, String name) { boolean jsonReadBool(String& json, String name, bool e) {
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json); DeserializationError error = deserializeJson(doc, json);
if (error) { if (error) {
SerialPrint("EE", F("jsonRead"), error.f_str()); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("jsonRead"), error.f_str());
jsonErrorDetected();
}
} }
return doc[name].as<bool>(); return doc[name].as<bool>();
} }
int jsonReadInt(String& json, String name) { int jsonReadInt(String& json, String name, bool e) {
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json); DeserializationError error = deserializeJson(doc, json);
if (error) { if (error) {
SerialPrint("EE", F("jsonRead"), error.f_str()); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("jsonRead"), error.f_str());
jsonErrorDetected();
}
} }
return doc[name].as<int>(); return doc[name].as<int>();
} }
// depricated======================================================================== // depricated========================================================================
String jsonWriteStr(String& json, String name, String value) { String jsonWriteStr(String& json, String name, String value, bool e) {
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json); DeserializationError error = deserializeJson(doc, json);
if (error) { if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str()); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("jsonWrite"), error.f_str());
jsonErrorDetected();
}
} }
doc[name] = value; doc[name] = value;
json = ""; json = "";
@@ -234,12 +272,14 @@ String jsonWriteStr(String& json, String name, String value) {
return json; return json;
} }
String jsonWriteBool(String& json, String name, boolean value) { String jsonWriteBool(String& json, String name, boolean value, bool e) {
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json); DeserializationError error = deserializeJson(doc, json);
if (error) { if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str()); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("jsonWrite"), error.f_str());
jsonErrorDetected();
}
} }
doc[name] = value; doc[name] = value;
json = ""; json = "";
@@ -247,12 +287,14 @@ String jsonWriteBool(String& json, String name, boolean value) {
return json; return json;
} }
String jsonWriteInt(String& json, String name, int value) { String jsonWriteInt(String& json, String name, int value, bool e) {
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json); DeserializationError error = deserializeJson(doc, json);
if (error) { if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str()); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("jsonWrite"), error.f_str());
jsonErrorDetected();
}
} }
doc[name] = value; doc[name] = value;
json = ""; json = "";
@@ -260,12 +302,14 @@ String jsonWriteInt(String& json, String name, int value) {
return json; return json;
} }
String jsonWriteFloat(String& json, String name, float value) { String jsonWriteFloat(String& json, String name, float value, bool e) {
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json); DeserializationError error = deserializeJson(doc, json);
if (error) { if (error) {
SerialPrint("EE", F("jsonWrite"), error.f_str()); if (e) {
jsonErrorDetected(); SerialPrint("EE", F("jsonWrite"), error.f_str());
jsonErrorDetected();
}
} }
doc[name] = value; doc[name] = value;
json = ""; json = "";

View File

@@ -5,7 +5,9 @@ void SerialPrint(String errorLevel, String module, String msg) {
String tosend = prettyMillis(millis()) + " [" + errorLevel + "] [" + module + "] " + msg; String tosend = prettyMillis(millis()) + " [" + errorLevel + "] [" + module + "] " + msg;
Serial.println(tosend); Serial.println(tosend);
if (jsonReadInt(settingsFlashJson, F("log")) != 0) { if (isNetworkActive()) {
standWebSocket.broadcastTXT("/log|" + tosend); if (jsonReadInt(settingsFlashJson, F("log")) != 0) {
standWebSocket.broadcastTXT("/log|" + tosend);
}
} }
} }