From c44eedb8c77415ab877f736950c38b330913b127 Mon Sep 17 00:00:00 2001 From: lumapu Date: Tue, 15 Nov 2022 15:32:01 +0100 Subject: [PATCH] fix littefs --- src/app.cpp | 2 ++ src/app.h | 1 - src/config/config.h | 10 +++--- src/config/settings.h | 82 ++++++++++++++++++++++++------------------- src/hm/hmSystem.h | 2 +- src/web/web.cpp | 5 +-- 6 files changed, 57 insertions(+), 45 deletions(-) diff --git a/src/app.cpp b/src/app.cpp index 8c56e4df..443e7fcb 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -249,6 +249,8 @@ void app::getAvailNetworks(JsonObject obj) { //----------------------------------------------------------------------------- void app::resetSystem(void) { + snprintf(mVersion, 12, "%d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH); + mUptimeSecs = 0; mPrevMillis = 0; mUpdateNtp = false; diff --git a/src/app.h b/src/app.h index 0f75f5ca..640c94d5 100644 --- a/src/app.h +++ b/src/app.h @@ -53,7 +53,6 @@ class app { void getAvailNetworks(JsonObject obj); void saveSettings(void) { - DPRINTLN(DBG_INFO, "a"); mSettings.saveSettings(); } diff --git a/src/config/config.h b/src/config/config.h index 281820e6..70a36f58 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -44,13 +44,13 @@ // default pinout (GPIO Number) #if defined(ESP32) - #define DEF_CS_PIN 15 - #define DEF_CE_PIN 2 - #define DEF_IRQ_PIN 0 -#else #define DEF_CS_PIN 5 #define DEF_CE_PIN 4 #define DEF_IRQ_PIN 16 +#else + #define DEF_CS_PIN 15 + #define DEF_CE_PIN 2 + #define DEF_IRQ_PIN 0 #endif #define DEF_LED0_PIN 255 // off #define DEF_LED1_PIN 255 // off @@ -62,7 +62,7 @@ #define PACKET_BUFFER_SIZE 30 // number of configurable inverters -#define MAX_NUM_INVERTERS 4 +#define MAX_NUM_INVERTERS 10 // default serial interval #define SERIAL_INTERVAL 5 diff --git a/src/config/settings.h b/src/config/settings.h index c8541b2e..3b0791d6 100644 --- a/src/config/settings.h +++ b/src/config/settings.h @@ -157,11 +157,12 @@ class settings { if(!fp) DPRINTLN(DBG_WARN, F("failed to load json, using default config")); else { - DynamicJsonDocument json(8192); - DeserializationError err = deserializeJson(json, fp); + DPRINTLN(DBG_INFO, fp.readString()); + fp.seek(0, SeekSet); + DynamicJsonDocument root(4096); + DeserializationError err = deserializeJson(root, fp); if(!err) { mValid = true; - JsonObject root = json.to(); jsonWifi(root["wifi"]); jsonNrf(root["nrf"]); jsonNtp(root["ntp"]); @@ -187,21 +188,22 @@ class settings { return false; } - DynamicJsonDocument json(8192); + DynamicJsonDocument json(4096); JsonObject root = json.to(); - jsonWifi(root["wifi"], true); - jsonNrf(root["nrf"], true); - jsonNtp(root["ntp"], true); - jsonSun(root["sun"], true); - jsonSerial(root["serial"], true); - jsonMqtt(root["mqtt"], true); - jsonLed(root["led"], true); - jsonInst(root["inst"], true); + jsonWifi(root.createNestedObject(F("wifi")), true); + jsonNrf(root.createNestedObject(F("nrf")), true); + jsonNtp(root.createNestedObject(F("ntp")), true); + jsonSun(root.createNestedObject(F("sun")), true); + jsonSerial(root.createNestedObject(F("serial")), true); + jsonMqtt(root.createNestedObject(F("mqtt")), true); + jsonLed(root.createNestedObject(F("led")), true); + jsonInst(root.createNestedObject(F("inst")), true); if(0 == serializeJson(root, fp)) { DPRINTLN(DBG_ERROR, F("can't write settings file!")); return false; } + fp.close(); return true; } @@ -235,9 +237,11 @@ class settings { DPRINTLN(DBG_INFO, F("loadDefaults")); memset(&mCfg, 0, sizeof(settings_t)); - snprintf(mCfg.sys.stationSsid, SSID_LEN, FB_WIFI_SSID); - snprintf(mCfg.sys.stationPwd, PWD_LEN, FB_WIFI_PWD); - snprintf(mCfg.sys.deviceName, DEVNAME_LEN, DEF_DEVICE_NAME); + if(wifi) { + snprintf(mCfg.sys.stationSsid, SSID_LEN, FB_WIFI_SSID); + snprintf(mCfg.sys.stationPwd, PWD_LEN, FB_WIFI_PWD); + snprintf(mCfg.sys.deviceName, DEVNAME_LEN, DEF_DEVICE_NAME); + } mCfg.nrf.sendInterval = SEND_INTERVAL; mCfg.nrf.maxRetransPerPyld = DEF_MAX_RETRANS_PER_PYLD; @@ -361,41 +365,47 @@ class settings { void jsonLed(JsonObject obj, bool set = false) { if(set) { - obj["0"] = mCfg.led.led0; - obj["1"] = mCfg.led.led1; + obj[F("0")] = mCfg.led.led0; + obj[F("1")] = mCfg.led.led1; } else { - mCfg.led.led0 = obj["0"]; - mCfg.led.led1 = obj["1"]; + mCfg.led.led0 = obj[F("0")]; + mCfg.led.led1 = obj[F("1")]; } } void jsonInst(JsonObject obj, bool set = false) { - if(set) { - obj["en"] = mCfg.inst.enabled; - } else { - mCfg.inst.enabled = obj["en"]; - } + if(set) + obj[F("en")] = mCfg.inst.enabled; + else + mCfg.inst.enabled = obj[F("en")]; + + JsonArray ivArr; + if(set) + ivArr = obj.createNestedArray(F("iv")); for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i++) { - jsonIv(obj["iv"][i], mCfg.inst.iv[i], set); + if(set) + jsonIv(ivArr.createNestedObject(), &mCfg.inst.iv[i], true); + else + jsonIv(obj[F("iv")][i], &mCfg.inst.iv[i]); } } - void jsonIv(JsonObject obj, cfgIv_t cfg, bool set = false) { + void jsonIv(JsonObject obj, cfgIv_t *cfg, bool set = false) { if(set) { - obj["en"] = cfg.enabled; - obj["name"] = cfg.name; - obj["serial"] = cfg.serial.u64; + obj[F("en")] = cfg->enabled; + obj[F("name")] = cfg->name; + obj[F("sn")] = cfg->serial.u64; for(uint8_t i = 0; i < 4; i++) { - obj["chPwr"][i] = cfg.chMaxPwr[i]; - obj["chName"][i] = cfg.chName[i]; + obj[F("pwr")][i] = cfg->chMaxPwr[i]; + obj[F("chName")][i] = cfg->chName[i]; } } else { - cfg.enabled = obj["en"]; - snprintf(cfg.name, MAX_NAME_LENGTH, "%s", obj[F("name")].as()); - cfg.serial.u64 = obj["serial"]; + cfg->enabled = obj[F("en")]; + snprintf(cfg->name, MAX_NAME_LENGTH, "%s", obj[F("name")].as()); + cfg->serial.u64 = obj[F("sn")]; for(uint8_t i = 0; i < 4; i++) { - cfg.chMaxPwr[i] = obj["chPwr"][i]; - snprintf(cfg.chName[i], MAX_NAME_LENGTH, "%s", obj["chName"][i].as()); + cfg->chMaxPwr[i] = obj[F("pwr")][i]; + snprintf(cfg->chName[i], MAX_NAME_LENGTH, "%s", obj[F("chName")][i].as()); } } } diff --git a/src/hm/hmSystem.h b/src/hm/hmSystem.h index be005b5f..53b6e85d 100644 --- a/src/hm/hmSystem.h +++ b/src/hm/hmSystem.h @@ -40,8 +40,8 @@ class HmSystem { void addInverters(cfgInst_t *config) { Inverter<> *iv; for (uint8_t i = 0; i < MAX_NUM_INVERTERS; i++) { + iv = addInverter(&config->iv[i]); if (0ULL != config->iv[i].serial.u64) { - iv = addInverter(&config->iv[i]); if (NULL != iv) DPRINTLN(DBG_INFO, "added inverter " + String(iv->config->serial.u64, HEX)); } diff --git a/src/web/web.cpp b/src/web/web.cpp index 5309c819..d762751b 100644 --- a/src/web/web.cpp +++ b/src/web/web.cpp @@ -366,7 +366,7 @@ void web::showSave(AsyncWebServerRequest *request) { // inverter - /*Inverter<> *iv; + Inverter<> *iv; for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) { iv = mMain->mSys->getInverterByPos(i, false); // address @@ -390,7 +390,8 @@ void web::showSave(AsyncWebServerRequest *request) { request->arg("inv" + String(i) + "ModName" + String(j)).toCharArray(iv->config->chName[j], MAX_NAME_LENGTH); } iv->initialized = true; - }*/ + } + if(request->arg("invInterval") != "") mConfig->nrf.sendInterval = request->arg("invInterval").toInt(); if(request->arg("invRetry") != "")