From e1326698b98d2e07775d3e70b9fed4af2f6a9547 Mon Sep 17 00:00:00 2001 From: lumapu Date: Fri, 17 Mar 2023 23:14:38 +0100 Subject: [PATCH] 0.5.102 fix JSON import #775 fix save settings, at least already stored settings are not lost #771 --- src/.vscode/settings.json | 1 + src/CHANGES.md | 13 +++++++++++++ src/config/settings.h | 31 ++++++++++++++++++++++++------- src/defines.h | 2 +- src/hm/miPayload.h | 2 +- src/web/html/setup.html | 2 +- src/web/web.h | 24 +++++++++++------------- 7 files changed, 52 insertions(+), 23 deletions(-) diff --git a/src/.vscode/settings.json b/src/.vscode/settings.json index 85de73f4..c0becfd1 100644 --- a/src/.vscode/settings.json +++ b/src/.vscode/settings.json @@ -85,4 +85,5 @@ "stop_token": "cpp", "thread": "cpp" }, + "cmake.configureOnOpen": false, } \ No newline at end of file diff --git a/src/CHANGES.md b/src/CHANGES.md index 9ec5f85b..131e891d 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -2,6 +2,19 @@ (starting from release version `0.5.66`) +## 0.5.102 +* fix JSON import #775 +* fix save settings, at least already stored settings are not lost #771 + +## 0.5.101 +* fix SSD1306 +* update documentation +* Update miPayload.h +* Update README.md +* MI - remarks to user manual +* MI - fix AC calc +* MI - fix status msg. analysis + ## 0.5.100 * fix add inverter `setup.html` #766 * fix MQTT retained flag for total values #726 diff --git a/src/config/settings.h b/src/config/settings.h index 77bc7a26..29974862 100644 --- a/src/config/settings.h +++ b/src/config/settings.h @@ -14,6 +14,12 @@ #include "../utils/dbg.h" #include "../utils/helper.h" +#if defined(ESP32) + #define MAX_ALLOWED_BUF_SIZE ESP.getMaxAllocHeap() - 2048 +#else + #define MAX_ALLOWED_BUF_SIZE ESP.getMaxFreeBlockSize() - 2048 +#endif + /** * More info: * https://arduino-esp8266.readthedocs.io/en/latest/filesystem.html#flash-layout @@ -224,8 +230,9 @@ class settings { else { //DPRINTLN(DBG_INFO, fp.readString()); //fp.seek(0, SeekSet); - DynamicJsonDocument root(5500); + DynamicJsonDocument root(MAX_ALLOWED_BUF_SIZE); DeserializationError err = deserializeJson(root, fp); + root.shrinkToFit(); if(!err && (root.size() > 0)) { mCfg.valid = true; jsonWifi(root[F("wifi")]); @@ -249,13 +256,8 @@ class settings { bool saveSettings(bool stopFs = false) { DPRINTLN(DBG_DEBUG, F("save settings")); - File fp = LittleFS.open("/settings.json", "w"); - if(!fp) { - DPRINTLN(DBG_ERROR, F("can't open settings file!")); - return false; - } - DynamicJsonDocument json(6500); + DynamicJsonDocument json(MAX_ALLOWED_BUF_SIZE); JsonObject root = json.to(); jsonWifi(root.createNestedObject(F("wifi")), true); jsonNrf(root.createNestedObject(F("nrf")), true); @@ -267,12 +269,27 @@ class settings { jsonPlugin(root.createNestedObject(F("plugin")), true); jsonInst(root.createNestedObject(F("inst")), true); + DPRINT(DBG_INFO, "memory usage: "); + DBGPRINTLN(String(json.memoryUsage())); + + if(json.overflowed()) { + DPRINTLN(DBG_ERROR, F("buffer too small!")); + return false; + } + + File fp = LittleFS.open("/settings.json", "w"); + if(!fp) { + DPRINTLN(DBG_ERROR, F("can't open settings file!")); + return false; + } + if(0 == serializeJson(root, fp)) { DPRINTLN(DBG_ERROR, F("can't write settings file!")); return false; } fp.close(); + DPRINTLN(DBG_INFO, F("settings saved")); if(stopFs) stop(); diff --git a/src/defines.h b/src/defines.h index 997981a3..e6206a96 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 5 -#define VERSION_PATCH 101 +#define VERSION_PATCH 102 //------------------------------------- typedef struct { diff --git a/src/hm/miPayload.h b/src/hm/miPayload.h index 62e7450e..554e57c8 100644 --- a/src/hm/miPayload.h +++ b/src/hm/miPayload.h @@ -118,7 +118,7 @@ class MiPayload { //DPRINTLN(DBG_INFO, F("MI got data [0]=") + String(p->packet[0], HEX)); if (p->packet[0] == (0x08 + ALL_FRAMES)) { // 0x88; MI status response to 0x09 - miStsDecode(iv, p), CH1; + miStsDecode(iv, p, CH1); } else if (p->packet[0] == (0x11 + SINGLE_FRAME)) { // 0x92; MI status response to 0x11 diff --git a/src/web/html/setup.html b/src/web/html/setup.html index 967cccd6..58144219 100644 --- a/src/web/html/setup.html +++ b/src/web/html/setup.html @@ -270,7 +270,7 @@
Luminance
-
+

Pinout

diff --git a/src/web/web.h b/src/web/web.h index 098ed7ed..814259b8 100644 --- a/src/web/web.h +++ b/src/web/web.h @@ -161,26 +161,24 @@ class Web { DPRINTLN(DBG_ERROR, F("can't open file!")); mUploadFail = true; mUploadFp.close(); + return; } } mUploadFp.write(data, len); if (final) { mUploadFp.close(); - File fp = LittleFS.open("/tmp.json", "r"); - if (!fp) + char pwd[PWD_LEN]; + strncpy(pwd, mConfig->sys.stationPwd, PWD_LEN); // backup WiFi PWD + if (!mApp->readSettings("/tmp.json")) { mUploadFail = true; - else { - char pwd[PWD_LEN]; - strncpy(pwd, mConfig->sys.stationPwd, PWD_LEN); // backup WiFi PWD - if (!mApp->readSettings("tmp.json")) { - mUploadFail = true; - DPRINTLN(DBG_ERROR, F("upload JSON error!")); - } else { - strncpy(mConfig->sys.stationPwd, pwd, PWD_LEN); // restore WiFi PWD - mApp->saveSettings(true); - } + DPRINTLN(DBG_ERROR, F("upload JSON error!")); + } else { + LittleFS.remove("/tmp.json"); + strncpy(mConfig->sys.stationPwd, pwd, PWD_LEN); // restore WiFi PWD + mApp->saveSettings(true); } - DPRINTLN(DBG_INFO, F("upload finished!")); + if (!mUploadFail) + DPRINTLN(DBG_INFO, F("upload finished!")); } }