[SYS] Avoid nvs errors (#1588)
Avoid nvs errors when trying to load or erase non-existent nvs preferences.
This commit is contained in:
1 parent
9b6133ff03
commit
e560bd7904
3 files changed
+46
-37
No files matched your search
+20
-16
@@ -46,10 +46,12 @@ void ONOFFConfig_fromJson(JsonObject& ONOFFdata) {
|
|||||||
if (ONOFFdata.containsKey("erase") && ONOFFdata["erase"].as<bool>()) {
|
if (ONOFFdata.containsKey("erase") && ONOFFdata["erase"].as<bool>()) {
|
||||||
// Erase config from NVS (non-volatile storage)
|
// Erase config from NVS (non-volatile storage)
|
||||||
preferences.begin(Gateway_Short_Name, false);
|
preferences.begin(Gateway_Short_Name, false);
|
||||||
preferences.remove("ONOFFConfig");
|
if (preferences.isKey("ONOFFConfig")) {
|
||||||
preferences.end();
|
preferences.remove("ONOFFConfig");
|
||||||
Log.notice(F("ONOFF config erased" CR));
|
preferences.end();
|
||||||
return; // Erase prevails on save, so skipping save
|
Log.notice(F("ONOFF config erased" CR));
|
||||||
|
return; // Erase prevails on save, so skipping save
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (ONOFFdata.containsKey("save") && ONOFFdata["save"].as<bool>()) {
|
if (ONOFFdata.containsKey("save") && ONOFFdata["save"].as<bool>()) {
|
||||||
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
|
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
|
||||||
@@ -69,19 +71,21 @@ void ONOFFConfig_fromJson(JsonObject& ONOFFdata) {
|
|||||||
void ONOFFConfig_load() {
|
void ONOFFConfig_load() {
|
||||||
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
|
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
|
||||||
preferences.begin(Gateway_Short_Name, true);
|
preferences.begin(Gateway_Short_Name, true);
|
||||||
auto error = deserializeJson(jsonBuffer, preferences.getString("ONOFFConfig", "{}"));
|
if (preferences.isKey("ONOFFConfig")) {
|
||||||
preferences.end();
|
auto error = deserializeJson(jsonBuffer, preferences.getString("ONOFFConfig", "{}"));
|
||||||
if (error) {
|
preferences.end();
|
||||||
Log.error(F("ONOFF config deserialization failed: %s, buffer capacity: %u" CR), error.c_str(), jsonBuffer.capacity());
|
if (error) {
|
||||||
return;
|
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<JsonObject>();
|
||||||
|
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<JsonObject>();
|
|
||||||
ONOFFConfig_fromJson(jo);
|
|
||||||
Log.notice(F("ONOFF config loaded" CR));
|
|
||||||
}
|
}
|
||||||
# else
|
# else
|
||||||
void ONOFFConfig_init(){};
|
void ONOFFConfig_init(){};
|
||||||
|
|||||||
@@ -176,8 +176,10 @@ void MQTTtoSSD1306(char* topicOri, JsonObject& SSD1306data) { // json object dec
|
|||||||
} else if (SSD1306data.containsKey("erase") && SSD1306data["erase"]) {
|
} else if (SSD1306data.containsKey("erase") && SSD1306data["erase"]) {
|
||||||
// Erase config from NVS (non-volatile storage)
|
// Erase config from NVS (non-volatile storage)
|
||||||
preferences.begin(Gateway_Short_Name, false);
|
preferences.begin(Gateway_Short_Name, false);
|
||||||
success = preferences.remove("SSD1306Config");
|
if (preferences.isKey("SSD1306Config")) {
|
||||||
preferences.end();
|
success = preferences.remove("SSD1306Config");
|
||||||
|
preferences.end();
|
||||||
|
}
|
||||||
if (success) {
|
if (success) {
|
||||||
Log.notice(F("SSD1306 config erased" CR));
|
Log.notice(F("SSD1306 config erased" CR));
|
||||||
}
|
}
|
||||||
@@ -222,8 +224,7 @@ void SSD1306Config_init() {
|
|||||||
bool SSD1306Config_load() {
|
bool SSD1306Config_load() {
|
||||||
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
|
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
|
||||||
preferences.begin(Gateway_Short_Name, true);
|
preferences.begin(Gateway_Short_Name, true);
|
||||||
bool exists = preferences.isKey("SSD1306Config");
|
if (preferences.isKey("SSD1306Config")) {
|
||||||
if (exists) {
|
|
||||||
auto error = deserializeJson(jsonBuffer, preferences.getString("SSD1306Config", "{}"));
|
auto error = deserializeJson(jsonBuffer, preferences.getString("SSD1306Config", "{}"));
|
||||||
preferences.end();
|
preferences.end();
|
||||||
if (error) {
|
if (error) {
|
||||||
|
|||||||
+21
-17
@@ -221,10 +221,12 @@ void BTConfig_fromJson(JsonObject& BTdata, bool startup = false) {
|
|||||||
if (BTdata.containsKey("erase") && BTdata["erase"].as<bool>()) {
|
if (BTdata.containsKey("erase") && BTdata["erase"].as<bool>()) {
|
||||||
// Erase config from NVS (non-volatile storage)
|
// Erase config from NVS (non-volatile storage)
|
||||||
preferences.begin(Gateway_Short_Name, false);
|
preferences.begin(Gateway_Short_Name, false);
|
||||||
preferences.remove("BTConfig");
|
if (preferences.isKey("BTConfig")) {
|
||||||
preferences.end();
|
preferences.remove("BTConfig");
|
||||||
Log.notice(F("BT config erased" CR));
|
preferences.end();
|
||||||
return; // Erase prevails on save, so skipping save
|
Log.notice(F("BT config erased" CR));
|
||||||
|
return; // Erase prevails on save, so skipping save
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (BTdata.containsKey("save") && BTdata["save"].as<bool>()) {
|
if (BTdata.containsKey("save") && BTdata["save"].as<bool>()) {
|
||||||
@@ -262,20 +264,22 @@ void BTConfig_fromJson(JsonObject& BTdata, bool startup = false) {
|
|||||||
void BTConfig_load() {
|
void BTConfig_load() {
|
||||||
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
|
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
|
||||||
preferences.begin(Gateway_Short_Name, true);
|
preferences.begin(Gateway_Short_Name, true);
|
||||||
auto error = deserializeJson(jsonBuffer, preferences.getString("BTConfig", "{}"));
|
if (preferences.isKey("BTConfig")) {
|
||||||
preferences.end();
|
auto error = deserializeJson(jsonBuffer, preferences.getString("BTConfig", "{}"));
|
||||||
Log.notice(F("BT config loaded" CR));
|
preferences.end();
|
||||||
if (error) {
|
Log.notice(F("BT config loaded" CR));
|
||||||
Log.error(F("BT config deserialization failed: %s, buffer capacity: %u" CR), error.c_str(), jsonBuffer.capacity());
|
if (error) {
|
||||||
return;
|
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<JsonObject>();
|
||||||
|
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<JsonObject>();
|
|
||||||
BTConfig_fromJson(jo, true); // Never send MQTT message with config
|
|
||||||
Log.notice(F("BT config loaded" CR));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void pubBTMainCore(JsonObject& data, bool haPresenceEnabled = true) {
|
void pubBTMainCore(JsonObject& data, bool haPresenceEnabled = true) {
|
||||||
|
|||||||
Reference in new issue
Block a user