mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 14:12:16 +03:00
глобальное изменение системы обновления прошивки
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Global.h"
|
#include "Global.h"
|
||||||
//#include "Upgrade.h"
|
// #include "Upgrade.h"
|
||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
//#include "ESP8266.h"
|
// #include "ESP8266.h"
|
||||||
#else
|
#else
|
||||||
#include <HTTPUpdate.h>
|
#include <HTTPUpdate.h>
|
||||||
#endif
|
#endif
|
||||||
@@ -17,9 +17,9 @@ struct updateFirm {
|
|||||||
|
|
||||||
extern void upgradeInit();
|
extern void upgradeInit();
|
||||||
extern void getLastVersion();
|
extern void getLastVersion();
|
||||||
extern void upgrade_firmware(int type);
|
extern void upgrade_firmware(int type, String path);
|
||||||
extern bool upgradeFS();
|
extern bool upgradeFS(String path);
|
||||||
extern bool upgradeBuild();
|
extern bool upgradeBuild(String path);
|
||||||
extern void restartEsp();
|
extern void restartEsp();
|
||||||
|
|
||||||
extern const String getBinPath(String file);
|
extern const String getBinPath(String file);
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"esp32_4mb": {
|
"esp8266_4mb": {
|
||||||
"0": "400"
|
"0": "400"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,12 +2,12 @@
|
|||||||
|
|
||||||
updateFirm update;
|
updateFirm update;
|
||||||
|
|
||||||
void upgrade_firmware(int type) {
|
void upgrade_firmware(int type, String path) {
|
||||||
putUserDataToRam();
|
putUserDataToRam();
|
||||||
|
|
||||||
// only build
|
// only build
|
||||||
if (type == 1) {
|
if (type == 1) {
|
||||||
if (upgradeBuild()) {
|
if (upgradeBuild(path)) {
|
||||||
saveUserDataToFlash();
|
saveUserDataToFlash();
|
||||||
restartEsp();
|
restartEsp();
|
||||||
}
|
}
|
||||||
@@ -15,7 +15,7 @@ void upgrade_firmware(int type) {
|
|||||||
|
|
||||||
// only littlefs
|
// only littlefs
|
||||||
else if (type == 2) {
|
else if (type == 2) {
|
||||||
if (upgradeFS()) {
|
if (upgradeFS(path)) {
|
||||||
saveUserDataToFlash();
|
saveUserDataToFlash();
|
||||||
restartEsp();
|
restartEsp();
|
||||||
}
|
}
|
||||||
@@ -23,33 +23,33 @@ void upgrade_firmware(int type) {
|
|||||||
|
|
||||||
// littlefs and build
|
// littlefs and build
|
||||||
else if (type == 3) {
|
else if (type == 3) {
|
||||||
if (upgradeFS()) {
|
if (upgradeFS(path)) {
|
||||||
saveUserDataToFlash();
|
saveUserDataToFlash();
|
||||||
if (upgradeBuild()) {
|
if (upgradeBuild(path)) {
|
||||||
restartEsp();
|
restartEsp();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool upgradeFS() {
|
bool upgradeFS(String path) {
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
WiFiClient wifiClient;
|
WiFiClient wifiClient;
|
||||||
SerialPrint("!!!", F("Update"), F("Start upgrade FS..."));
|
SerialPrint("!!!", F("Update"), "Start upgrade FS... " + path);
|
||||||
handleUpdateStatus(true, UPDATE_FS_IN_PROGRESS);
|
handleUpdateStatus(true, UPDATE_FS_IN_PROGRESS);
|
||||||
if (getBinPath("") == "error") {
|
if (path == "") {
|
||||||
SerialPrint("E", F("Update"), F("FS Path error"));
|
SerialPrint("E", F("Update"), F("FS Path error"));
|
||||||
handleUpdateStatus(true, PATH_ERROR);
|
handleUpdateStatus(true, PATH_ERROR);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
ESPhttpUpdate.rebootOnUpdate(false);
|
ESPhttpUpdate.rebootOnUpdate(false);
|
||||||
t_httpUpdate_return retFS = ESPhttpUpdate.updateFS(wifiClient, getBinPath("littlefs.bin"));
|
t_httpUpdate_return retFS = ESPhttpUpdate.updateFS(wifiClient, path + "/littlefs.bin");
|
||||||
#endif
|
#endif
|
||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
httpUpdate.rebootOnUpdate(false);
|
httpUpdate.rebootOnUpdate(false);
|
||||||
// обновляем little fs с помощью метода обновления spiffs
|
// обновляем little fs с помощью метода обновления spiffs!!!!
|
||||||
HTTPUpdateResult retFS = httpUpdate.updateSpiffs(wifiClient, getBinPath("littlefs.bin"));
|
HTTPUpdateResult retFS = httpUpdate.updateSpiffs(wifiClient, path + "/littlefs.bin");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// если FS обновилась успешно
|
// если FS обновилась успешно
|
||||||
@@ -59,28 +59,32 @@ bool upgradeFS() {
|
|||||||
ret = true;
|
ret = true;
|
||||||
} else {
|
} else {
|
||||||
handleUpdateStatus(true, UPDATE_FS_FAILED);
|
handleUpdateStatus(true, UPDATE_FS_FAILED);
|
||||||
|
if (retFS == HTTP_UPDATE_FAILED) {
|
||||||
|
SerialPrint("E", F("Update"), "HTTP_UPDATE_FAILED");
|
||||||
|
} else if (retFS == HTTP_UPDATE_NO_UPDATES) {
|
||||||
|
SerialPrint("E", F("Update"), "HTTP_UPDATE_NO_UPDATES");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool upgradeBuild() {
|
bool upgradeBuild(String path) {
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
WiFiClient wifiClient;
|
WiFiClient wifiClient;
|
||||||
SerialPrint("!!!", F("Update"), F("Start upgrade BUILD..."));
|
SerialPrint("!!!", F("Update"), "Start upgrade BUILD... " + path);
|
||||||
handleUpdateStatus(true, UPDATE_BUILD_IN_PROGRESS);
|
handleUpdateStatus(true, UPDATE_BUILD_IN_PROGRESS);
|
||||||
if (getBinPath("") == "error") {
|
if (path == "") {
|
||||||
SerialPrint("E", F("Update"), F("Build Path error"));
|
SerialPrint("E", F("Update"), F("Build Path error"));
|
||||||
handleUpdateStatus(true, PATH_ERROR);
|
handleUpdateStatus(true, PATH_ERROR);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
#if defined(esp8266_4mb) || defined(esp8266_16mb) || defined(esp8266_1mb) || defined(esp8266_1mb_ota) || defined(esp8266_2mb) || defined(esp8266_2mb_ota)
|
#if defined(esp8266_4mb) || defined(esp8266_16mb) || defined(esp8266_1mb) || defined(esp8266_1mb_ota) || defined(esp8266_2mb) || defined(esp8266_2mb_ota)
|
||||||
ESPhttpUpdate.rebootOnUpdate(false);
|
ESPhttpUpdate.rebootOnUpdate(false);
|
||||||
t_httpUpdate_return retBuild = ESPhttpUpdate.update(wifiClient, getBinPath("firmware.bin"));
|
t_httpUpdate_return retBuild = ESPhttpUpdate.update(wifiClient, path + "/firmware.bin");
|
||||||
#endif
|
#endif
|
||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
httpUpdate.rebootOnUpdate(false);
|
httpUpdate.rebootOnUpdate(false);
|
||||||
HTTPUpdateResult retBuild = httpUpdate.update(wifiClient, getBinPath("firmware.bin"));
|
HTTPUpdateResult retBuild = httpUpdate.update(wifiClient, path + "/firmware.bin");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// если BUILD обновился успешно
|
// если BUILD обновился успешно
|
||||||
@@ -90,6 +94,11 @@ bool upgradeBuild() {
|
|||||||
ret = true;
|
ret = true;
|
||||||
} else {
|
} else {
|
||||||
handleUpdateStatus(true, UPDATE_BUILD_FAILED);
|
handleUpdateStatus(true, UPDATE_BUILD_FAILED);
|
||||||
|
if (retBuild == HTTP_UPDATE_FAILED) {
|
||||||
|
SerialPrint("E", F("Update"), "HTTP_UPDATE_FAILED");
|
||||||
|
} else if (retBuild == HTTP_UPDATE_NO_UPDATES) {
|
||||||
|
SerialPrint("E", F("Update"), "HTTP_UPDATE_NO_UPDATES");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -100,22 +109,23 @@ void restartEsp() {
|
|||||||
ESP.restart();
|
ESP.restart();
|
||||||
}
|
}
|
||||||
|
|
||||||
const String getBinPath(String file) {
|
// теперь путь к обнавленю прошивки мы получаем из веб интерфейса
|
||||||
String path = "error";
|
// const String getBinPath(String file) {
|
||||||
int targetVersion = 0;
|
// String path = "error";
|
||||||
String serverip;
|
// int targetVersion = 0;
|
||||||
if (jsonRead(errorsHeapJson, F("chver"), targetVersion)) {
|
// String serverip;
|
||||||
if (targetVersion >= 400) {
|
// if (jsonRead(errorsHeapJson, F("chver"), targetVersion)) {
|
||||||
if (jsonRead(settingsFlashJson, F("serverip"), serverip)) {
|
// if (targetVersion >= 400) {
|
||||||
if (serverip != "") {
|
// if (jsonRead(settingsFlashJson, F("serverip"), serverip)) {
|
||||||
path = jsonReadStr(settingsFlashJson, F("serverip")) + "/iotm/" + String(FIRMWARE_NAME) + "/" + String(targetVersion) + "/" + file;
|
// if (serverip != "") {
|
||||||
}
|
// path = jsonReadStr(settingsFlashJson, F("serverip")) + "/iotm/" + String(FIRMWARE_NAME) + "/" + String(targetVersion) + "/" + file;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
SerialPrint("i", F("Update"), "path: " + path);
|
// }
|
||||||
return path;
|
// SerialPrint("i", F("Update"), "path: " + path);
|
||||||
}
|
// return path;
|
||||||
|
// }
|
||||||
|
|
||||||
void putUserDataToRam() {
|
void putUserDataToRam() {
|
||||||
update.configJson = readFile("config.json", 4096);
|
update.configJson = readFile("config.json", 4096);
|
||||||
|
|||||||
@@ -36,8 +36,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload,
|
|||||||
|
|
||||||
case WStype_TEXT: {
|
case WStype_TEXT: {
|
||||||
bool endOfHeaderFound = false;
|
bool endOfHeaderFound = false;
|
||||||
size_t maxAllowedHeaderSize =
|
size_t maxAllowedHeaderSize = 15; // максимальное количество символов заголовка
|
||||||
15; // максимальное количество символов заголовка
|
|
||||||
size_t headerLenth = 0;
|
size_t headerLenth = 0;
|
||||||
String headerStr;
|
String headerStr;
|
||||||
for (size_t i = 0; i <= maxAllowedHeaderSize; i++) {
|
for (size_t i = 0; i <= maxAllowedHeaderSize; i++) {
|
||||||
@@ -59,8 +58,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload,
|
|||||||
|
|
||||||
// публикация всех виджетов
|
// публикация всех виджетов
|
||||||
if (headerStr == "/|") {
|
if (headerStr == "/|") {
|
||||||
sendFileToWsByFrames("/layout.json", "layout", "", num,
|
sendFileToWsByFrames("/layout.json", "layout", "", num, WEB_SOCKETS_FRAME_SIZE);
|
||||||
WEB_SOCKETS_FRAME_SIZE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (headerStr == "/params|") {
|
if (headerStr == "/params|") {
|
||||||
@@ -107,14 +105,10 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload,
|
|||||||
|
|
||||||
// отвечаем данными на запрос страницы
|
// отвечаем данными на запрос страницы
|
||||||
if (headerStr == "/config|") {
|
if (headerStr == "/config|") {
|
||||||
sendFileToWsByFrames("/items.json", "itemsj", "", num,
|
sendFileToWsByFrames("/items.json", "itemsj", "", num, WEB_SOCKETS_FRAME_SIZE);
|
||||||
WEB_SOCKETS_FRAME_SIZE);
|
sendFileToWsByFrames("/widgets.json", "widget", "", num, WEB_SOCKETS_FRAME_SIZE);
|
||||||
sendFileToWsByFrames("/widgets.json", "widget", "", num,
|
sendFileToWsByFrames("/config.json", "config", "", num, WEB_SOCKETS_FRAME_SIZE);
|
||||||
WEB_SOCKETS_FRAME_SIZE);
|
sendFileToWsByFrames("/scenario.txt", "scenar", "", num, WEB_SOCKETS_FRAME_SIZE);
|
||||||
sendFileToWsByFrames("/config.json", "config", "", num,
|
|
||||||
WEB_SOCKETS_FRAME_SIZE);
|
|
||||||
sendFileToWsByFrames("/scenario.txt", "scenar", "", num,
|
|
||||||
WEB_SOCKETS_FRAME_SIZE);
|
|
||||||
sendStringToWs("settin", settingsFlashJson, num);
|
sendStringToWs("settin", settingsFlashJson, num);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,10 +216,8 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload,
|
|||||||
if (headerStr == "/dev|") {
|
if (headerStr == "/dev|") {
|
||||||
sendStringToWs("errors", errorsHeapJson, num);
|
sendStringToWs("errors", errorsHeapJson, num);
|
||||||
sendStringToWs("settin", settingsFlashJson, num);
|
sendStringToWs("settin", settingsFlashJson, num);
|
||||||
sendFileToWsByFrames("/config.json", "config", "", num,
|
sendFileToWsByFrames("/config.json", "config", "", num, WEB_SOCKETS_FRAME_SIZE);
|
||||||
WEB_SOCKETS_FRAME_SIZE);
|
sendFileToWsByFrames("/items.json", "itemsj", "", num, WEB_SOCKETS_FRAME_SIZE);
|
||||||
sendFileToWsByFrames("/items.json", "itemsj", "", num,
|
|
||||||
WEB_SOCKETS_FRAME_SIZE);
|
|
||||||
// sendFileToWsByFrames("/layout.json", "layout", "", num,
|
// sendFileToWsByFrames("/layout.json", "layout", "", num,
|
||||||
// WEB_SOCKETS_FRAME_SIZE);
|
// WEB_SOCKETS_FRAME_SIZE);
|
||||||
}
|
}
|
||||||
@@ -244,8 +236,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload,
|
|||||||
|
|
||||||
// переписать любое поле в errors json
|
// переписать любое поле в errors json
|
||||||
if (headerStr == "/rorre|") {
|
if (headerStr == "/rorre|") {
|
||||||
writeUint8tValueToJsonString(payload, length, headerLenth,
|
writeUint8tValueToJsonString(payload, length, headerLenth, errorsHeapJson);
|
||||||
errorsHeapJson);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// команда перезагрузки esp
|
// команда перезагрузки esp
|
||||||
@@ -260,7 +251,9 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload,
|
|||||||
|
|
||||||
// команда обновления прошивки esp
|
// команда обновления прошивки esp
|
||||||
if (headerStr == "/update|") {
|
if (headerStr == "/update|") {
|
||||||
upgrade_firmware(3);
|
String path;
|
||||||
|
writeUint8tToString(payload, length, headerLenth, path);
|
||||||
|
upgrade_firmware(3, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Прием команд control c dashboard
|
// Прием команд control c dashboard
|
||||||
@@ -270,9 +263,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload,
|
|||||||
String key = selectFromMarkerToMarker(msg, "/", 0);
|
String key = selectFromMarkerToMarker(msg, "/", 0);
|
||||||
String value = selectFromMarkerToMarker(msg, "/", 1);
|
String value = selectFromMarkerToMarker(msg, "/", 1);
|
||||||
generateOrder(key, value);
|
generateOrder(key, value);
|
||||||
SerialPrint(
|
SerialPrint("i", F("=>WS"), "Msg from svelte web, WS No: " + String(num) + ", msg: " + msg);
|
||||||
"i", F("=>WS"),
|
|
||||||
"Msg from svelte web, WS No: " + String(num) + ", msg: " + msg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (headerStr == "/tst|") {
|
if (headerStr == "/tst|") {
|
||||||
|
|||||||
@@ -2,34 +2,26 @@
|
|||||||
#include "utils/FileUtils.h"
|
#include "utils/FileUtils.h"
|
||||||
|
|
||||||
// new================================================================================
|
// new================================================================================
|
||||||
String jsonReadStrDoc(DynamicJsonDocument &doc, String name)
|
String jsonReadStrDoc(DynamicJsonDocument &doc, String name) {
|
||||||
{
|
|
||||||
return doc[name].as<String>();
|
return doc[name].as<String>();
|
||||||
}
|
}
|
||||||
|
|
||||||
void jsonWriteStrDoc(DynamicJsonDocument &doc, String name, String value)
|
void jsonWriteStrDoc(DynamicJsonDocument &doc, String name, String value) {
|
||||||
{
|
|
||||||
doc[name] = value;
|
doc[name] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// new==============================================================================
|
// new==============================================================================
|
||||||
bool jsonRead(const String &json, String key, long &value, bool e)
|
bool jsonRead(const String &json, String key, long &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) {
|
||||||
{
|
if (e) {
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonRead"), error.f_str());
|
SerialPrint("E", F("jsonRead"), error.f_str());
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
} else if (!doc.containsKey(key)) {
|
||||||
else if (!doc.containsKey(key))
|
if (e) {
|
||||||
{
|
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonRead"), key + " missing in " + json);
|
SerialPrint("E", F("jsonRead"), key + " missing in " + json);
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
@@ -39,23 +31,17 @@ bool jsonRead(const String &json, String key, long &value, bool e)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool jsonRead(const String &json, String key, float &value, bool e)
|
bool jsonRead(const String &json, String key, 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) {
|
||||||
{
|
if (e) {
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonRead"), error.f_str());
|
SerialPrint("E", F("jsonRead"), error.f_str());
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
} else if (!doc.containsKey(key)) {
|
||||||
else if (!doc.containsKey(key))
|
if (e) {
|
||||||
{
|
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonRead"), key + " missing in " + json);
|
SerialPrint("E", F("jsonRead"), key + " missing in " + json);
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
@@ -65,23 +51,17 @@ bool jsonRead(const String &json, String key, float &value, bool e)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool jsonRead(const String &json, String key, String &value, bool e)
|
bool jsonRead(const String &json, String key, 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) {
|
||||||
{
|
if (e) {
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonRead"), error.f_str());
|
SerialPrint("E", F("jsonRead"), error.f_str());
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
} else if (!doc.containsKey(key)) {
|
||||||
else if (!doc.containsKey(key))
|
if (e) {
|
||||||
{
|
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonRead"), key + " missing in " + json);
|
SerialPrint("E", F("jsonRead"), key + " missing in " + json);
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
@@ -91,31 +71,24 @@ bool jsonRead(const String &json, String key, String &value, bool e)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool jsonRead(const String &json, String key, bool &value, bool e)
|
bool jsonRead(const String &json, String key, bool &value, bool e) {
|
||||||
{
|
|
||||||
int lvalue = value;
|
int lvalue = value;
|
||||||
bool ret = jsonRead(json, key, lvalue, e);
|
bool ret = jsonRead(json, key, lvalue, e);
|
||||||
value = lvalue;
|
value = lvalue;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool jsonRead(const String &json, String key, int &value, bool e)
|
bool jsonRead(const String &json, String key, 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) {
|
||||||
{
|
if (e) {
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonRead"), error.f_str());
|
SerialPrint("E", F("jsonRead"), error.f_str());
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
} else if (!doc.containsKey(key)) {
|
||||||
else if (!doc.containsKey(key))
|
if (e) {
|
||||||
{
|
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonRead"), key + " missing in " + json);
|
SerialPrint("E", F("jsonRead"), key + " missing in " + json);
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
@@ -125,37 +98,28 @@ bool jsonRead(const String &json, String key, int &value, bool e)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool jsonReadArray(const String &json, String key, std::vector<String> &jArray, bool e)
|
bool jsonReadArray(const String &json, String key, std::vector<String> &jArray, 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) {
|
||||||
{
|
if (e) {
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonReadArray"), error.f_str());
|
SerialPrint("E", F("jsonReadArray"), error.f_str());
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
} else if (!doc.containsKey(key)) {
|
||||||
else if (!doc.containsKey(key))
|
if (e) {
|
||||||
{
|
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonReadArray"), key + " missing in " + json);
|
SerialPrint("E", F("jsonReadArray"), key + " missing in " + json);
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// SerialPrint("E", F("jsonReadArray"), key + " doc " + doc[key].as<String>());
|
// SerialPrint("E", F("jsonReadArray"), key + " doc " + doc[key].as<String>());
|
||||||
if (doc[key].is<JsonArray>())
|
if (doc[key].is<JsonArray>()) {
|
||||||
{
|
|
||||||
for (int8_t i = 0; i < doc[key].size(); i++)
|
for (int8_t i = 0; i < doc[key].size(); i++)
|
||||||
jArray.push_back(doc[key][i].as<String>());
|
jArray.push_back(doc[key][i].as<String>());
|
||||||
// SerialPrint("E", F("jsonReadArray"), "isArray"+key + " doc " + doc[key].as<String>());
|
// SerialPrint("E", F("jsonReadArray"), "isArray"+key + " doc " + doc[key].as<String>());
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
jArray.push_back(doc[key].as<String>());
|
jArray.push_back(doc[key].as<String>());
|
||||||
// DynamicJsonDocument docArr(JSON_BUFFER_SIZE/5);
|
// DynamicJsonDocument docArr(JSON_BUFFER_SIZE/5);
|
||||||
// jArray = doc[key].as<JsonArray>();
|
// jArray = doc[key].as<JsonArray>();
|
||||||
@@ -169,15 +133,12 @@ bool jsonReadArray(const String &json, String key, std::vector<String> &jArray,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// new==============================================================================
|
// new==============================================================================
|
||||||
bool jsonWriteStr_(String &json, const String &key, const String &value, bool e)
|
bool jsonWriteStr_(String &json, const String &key, const 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) {
|
||||||
{
|
if (e) {
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonWrite"), error.f_str());
|
SerialPrint("E", F("jsonWrite"), error.f_str());
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
@@ -189,15 +150,12 @@ bool jsonWriteStr_(String &json, const String &key, const String &value, bool e)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool jsonWriteBool_(String &json, const String &key, bool value, bool e)
|
bool jsonWriteBool_(String &json, const 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) {
|
||||||
{
|
if (e) {
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonWrite"), error.f_str());
|
SerialPrint("E", F("jsonWrite"), error.f_str());
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
@@ -209,15 +167,12 @@ bool jsonWriteBool_(String &json, const String &key, bool value, bool e)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool jsonWriteInt_(String &json, const String &key, int value, bool e)
|
bool jsonWriteInt_(String &json, const 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) {
|
||||||
{
|
if (e) {
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonWrite"), error.f_str());
|
SerialPrint("E", F("jsonWrite"), error.f_str());
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
@@ -229,15 +184,12 @@ bool jsonWriteInt_(String &json, const String &key, int value, bool e)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool jsonWriteFloat_(String &json, const String &key, float value, bool e)
|
bool jsonWriteFloat_(String &json, const 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) {
|
||||||
{
|
if (e) {
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonWrite"), error.f_str());
|
SerialPrint("E", F("jsonWrite"), error.f_str());
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
@@ -249,29 +201,24 @@ bool jsonWriteFloat_(String &json, const String &key, float value, bool e)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
{
|
|
||||||
String payloadStr;
|
String payloadStr;
|
||||||
payloadStr.reserve(length + 1);
|
payloadStr.reserve(length + 1);
|
||||||
for (size_t i = headerLenth; i < length; i++)
|
for (size_t i = headerLenth; i < length; i++) {
|
||||||
{
|
|
||||||
payloadStr += (char)payload[i];
|
payloadStr += (char)payload[i];
|
||||||
}
|
}
|
||||||
jsonMergeObjects(json, payloadStr);
|
jsonMergeObjects(json, payloadStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool jsonMergeObjects(String &json1, String &json2, bool e)
|
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);
|
||||||
DynamicJsonDocument doc2(JSON_BUFFER_SIZE);
|
DynamicJsonDocument doc2(JSON_BUFFER_SIZE);
|
||||||
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) {
|
||||||
{
|
if (e) {
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("json"), "jsonMergeObjects error");
|
SerialPrint("E", F("json"), "jsonMergeObjects error");
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
@@ -282,23 +229,18 @@ bool jsonMergeObjects(String &json1, String &json2, bool e)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void jsonMergeDocs(JsonObject dest, JsonObjectConst src)
|
void jsonMergeDocs(JsonObject dest, JsonObjectConst src) {
|
||||||
{
|
for (auto kvp : src) {
|
||||||
for (auto kvp : src)
|
|
||||||
{
|
|
||||||
dest[kvp.key()] = kvp.value();
|
dest[kvp.key()] = kvp.value();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// depricated======================================================================
|
// depricated======================================================================
|
||||||
String jsonReadStr(const String &json, String name, bool e)
|
String jsonReadStr(const 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) {
|
||||||
{
|
if (e) {
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonRead"), error.f_str());
|
SerialPrint("E", F("jsonRead"), error.f_str());
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
@@ -306,14 +248,11 @@ String jsonReadStr(const String &json, String name, bool e)
|
|||||||
return doc[name].as<String>();
|
return doc[name].as<String>();
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean jsonReadBool(const String &json, String name, bool e)
|
boolean jsonReadBool(const 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) {
|
||||||
{
|
if (e) {
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonRead"), error.f_str());
|
SerialPrint("E", F("jsonRead"), error.f_str());
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
@@ -321,14 +260,11 @@ boolean jsonReadBool(const String &json, String name, bool e)
|
|||||||
return doc[name].as<bool>();
|
return doc[name].as<bool>();
|
||||||
}
|
}
|
||||||
|
|
||||||
int jsonReadInt(const String &json, String name, bool e)
|
int jsonReadInt(const 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) {
|
||||||
{
|
if (e) {
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonRead"), error.f_str());
|
SerialPrint("E", F("jsonRead"), error.f_str());
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
@@ -336,14 +272,11 @@ int jsonReadInt(const String &json, String name, bool e)
|
|||||||
return doc[name].as<int>();
|
return doc[name].as<int>();
|
||||||
}
|
}
|
||||||
|
|
||||||
long int jsonReadLInt(const String &json, String name, bool e)
|
long int jsonReadLInt(const 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) {
|
||||||
{
|
if (e) {
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonRead"), error.f_str());
|
SerialPrint("E", F("jsonRead"), error.f_str());
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
@@ -352,14 +285,11 @@ long int jsonReadLInt(const String &json, String name, bool e)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// depricated========================================================================
|
// depricated========================================================================
|
||||||
String jsonWriteStr(String &json, String name, String value, bool e)
|
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) {
|
||||||
{
|
if (e) {
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonWrite"), error.f_str());
|
SerialPrint("E", F("jsonWrite"), error.f_str());
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
@@ -370,14 +300,11 @@ String jsonWriteStr(String &json, String name, String value, bool e)
|
|||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
String jsonWriteBool(String &json, String name, boolean value, bool e)
|
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) {
|
||||||
{
|
if (e) {
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonWrite"), error.f_str());
|
SerialPrint("E", F("jsonWrite"), error.f_str());
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
@@ -388,14 +315,11 @@ String jsonWriteBool(String &json, String name, boolean value, bool e)
|
|||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
String jsonWriteInt(String &json, String name, int value, bool e)
|
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) {
|
||||||
{
|
if (e) {
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonWrite"), error.f_str());
|
SerialPrint("E", F("jsonWrite"), error.f_str());
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
@@ -406,14 +330,11 @@ String jsonWriteInt(String &json, String name, int value, bool e)
|
|||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
String jsonWriteFloat(String &json, String name, float value, bool e)
|
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) {
|
||||||
{
|
if (e) {
|
||||||
if (e)
|
|
||||||
{
|
|
||||||
SerialPrint("E", F("jsonWrite"), error.f_str());
|
SerialPrint("E", F("jsonWrite"), error.f_str());
|
||||||
jsonErrorDetected();
|
jsonErrorDetected();
|
||||||
}
|
}
|
||||||
@@ -424,8 +345,7 @@ String jsonWriteFloat(String &json, String name, float value, bool e)
|
|||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
void jsonErrorDetected()
|
void jsonErrorDetected() {
|
||||||
{
|
|
||||||
// пример как отправить ошибку с количеством
|
// пример как отправить ошибку с количеством
|
||||||
// jsonWriteInt(errorsHeapJson, F("jse2"), 1);
|
// jsonWriteInt(errorsHeapJson, F("jse2"), 1);
|
||||||
// int number = jsonReadInt(errorsHeapJson, F("jse2n"));
|
// int number = jsonReadInt(errorsHeapJson, F("jse2n"));
|
||||||
|
|||||||
Reference in New Issue
Block a user