diff --git a/main/ZactuatorONOFF.ino b/main/ZactuatorONOFF.ino index baf834a..b60ffb2 100644 --- a/main/ZactuatorONOFF.ino +++ b/main/ZactuatorONOFF.ino @@ -46,10 +46,12 @@ void ONOFFConfig_fromJson(JsonObject& ONOFFdata) { if (ONOFFdata.containsKey("erase") && ONOFFdata["erase"].as()) { // Erase config from NVS (non-volatile storage) preferences.begin(Gateway_Short_Name, false); - preferences.remove("ONOFFConfig"); - preferences.end(); - Log.notice(F("ONOFF config erased" CR)); - return; // Erase prevails on save, so skipping save + if (preferences.isKey("ONOFFConfig")) { + preferences.remove("ONOFFConfig"); + preferences.end(); + Log.notice(F("ONOFF config erased" CR)); + return; // Erase prevails on save, so skipping save + } } if (ONOFFdata.containsKey("save") && ONOFFdata["save"].as()) { StaticJsonDocument jsonBuffer; @@ -69,19 +71,21 @@ void ONOFFConfig_fromJson(JsonObject& ONOFFdata) { void ONOFFConfig_load() { StaticJsonDocument jsonBuffer; preferences.begin(Gateway_Short_Name, true); - auto error = deserializeJson(jsonBuffer, preferences.getString("ONOFFConfig", "{}")); - preferences.end(); - if (error) { - Log.error(F("ONOFF config deserialization failed: %s, buffer capacity: %u" CR), error.c_str(), jsonBuffer.capacity()); - return; + if (preferences.isKey("ONOFFConfig")) { + auto error = deserializeJson(jsonBuffer, preferences.getString("ONOFFConfig", "{}")); + preferences.end(); + if (error) { + Log.error(F("ONOFF config deserialization failed: %s, buffer capacity: %u" CR), error.c_str(), jsonBuffer.capacity()); + return; + } + if (jsonBuffer.isNull()) { + Log.warning(F("ONOFF config is null" CR)); + return; + } + JsonObject jo = jsonBuffer.as(); + ONOFFConfig_fromJson(jo); + Log.notice(F("ONOFF config loaded" CR)); } - if (jsonBuffer.isNull()) { - Log.warning(F("ONOFF config is null" CR)); - return; - } - JsonObject jo = jsonBuffer.as(); - ONOFFConfig_fromJson(jo); - Log.notice(F("ONOFF config loaded" CR)); } # else void ONOFFConfig_init(){}; diff --git a/main/ZdisplaySSD1306.ino b/main/ZdisplaySSD1306.ino index e0198ba..2054096 100644 --- a/main/ZdisplaySSD1306.ino +++ b/main/ZdisplaySSD1306.ino @@ -176,8 +176,10 @@ void MQTTtoSSD1306(char* topicOri, JsonObject& SSD1306data) { // json object dec } else if (SSD1306data.containsKey("erase") && SSD1306data["erase"]) { // Erase config from NVS (non-volatile storage) preferences.begin(Gateway_Short_Name, false); - success = preferences.remove("SSD1306Config"); - preferences.end(); + if (preferences.isKey("SSD1306Config")) { + success = preferences.remove("SSD1306Config"); + preferences.end(); + } if (success) { Log.notice(F("SSD1306 config erased" CR)); } @@ -222,8 +224,7 @@ void SSD1306Config_init() { bool SSD1306Config_load() { StaticJsonDocument jsonBuffer; preferences.begin(Gateway_Short_Name, true); - bool exists = preferences.isKey("SSD1306Config"); - if (exists) { + if (preferences.isKey("SSD1306Config")) { auto error = deserializeJson(jsonBuffer, preferences.getString("SSD1306Config", "{}")); preferences.end(); if (error) { diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 590e992..7526798 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -221,10 +221,12 @@ void BTConfig_fromJson(JsonObject& BTdata, bool startup = false) { if (BTdata.containsKey("erase") && BTdata["erase"].as()) { // Erase config from NVS (non-volatile storage) preferences.begin(Gateway_Short_Name, false); - preferences.remove("BTConfig"); - preferences.end(); - Log.notice(F("BT config erased" CR)); - return; // Erase prevails on save, so skipping save + if (preferences.isKey("BTConfig")) { + preferences.remove("BTConfig"); + preferences.end(); + Log.notice(F("BT config erased" CR)); + return; // Erase prevails on save, so skipping save + } } if (BTdata.containsKey("save") && BTdata["save"].as()) { @@ -262,20 +264,22 @@ void BTConfig_fromJson(JsonObject& BTdata, bool startup = false) { void BTConfig_load() { StaticJsonDocument jsonBuffer; preferences.begin(Gateway_Short_Name, true); - auto error = deserializeJson(jsonBuffer, preferences.getString("BTConfig", "{}")); - preferences.end(); - Log.notice(F("BT config loaded" CR)); - if (error) { - Log.error(F("BT config deserialization failed: %s, buffer capacity: %u" CR), error.c_str(), jsonBuffer.capacity()); - return; + if (preferences.isKey("BTConfig")) { + auto error = deserializeJson(jsonBuffer, preferences.getString("BTConfig", "{}")); + preferences.end(); + Log.notice(F("BT config loaded" CR)); + if (error) { + Log.error(F("BT config deserialization failed: %s, buffer capacity: %u" CR), error.c_str(), jsonBuffer.capacity()); + return; + } + if (jsonBuffer.isNull()) { + Log.warning(F("BT config is null" CR)); + return; + } + JsonObject jo = jsonBuffer.as(); + BTConfig_fromJson(jo, true); // Never send MQTT message with config + Log.notice(F("BT config loaded" CR)); } - if (jsonBuffer.isNull()) { - Log.warning(F("BT config is null" CR)); - return; - } - JsonObject jo = jsonBuffer.as(); - BTConfig_fromJson(jo, true); // Never send MQTT message with config - Log.notice(F("BT config loaded" CR)); } void pubBTMainCore(JsonObject& data, bool haPresenceEnabled = true) {