Browse Source

settings: fallback to defaults when old cfg is detected

mcspr-patch-1
Maxim Prokhorov 3 years ago
parent
commit
21b89efb4b
3 changed files with 95 additions and 39 deletions
  1. +59
    -34
      code/espurna/migrate.cpp
  2. +29
    -5
      code/espurna/settings.cpp
  3. +7
    -0
      code/espurna/settings.h

+ 59
- 34
code/espurna/migrate.cpp View File

@ -8,18 +8,37 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
#include "settings.h" #include "settings.h"
void _cmpMoveIndexDown(const char * key, int offset = 0) {
if (hasSetting({key, 0})) return;
for (unsigned char index = 1; index < SETTINGS_MAX_LIST_COUNT; index++) {
const unsigned char prev = index - 1;
if (hasSetting({key, index})) {
setSetting({key, prev}, getSetting({key, index}).toInt() + offset);
} else {
delSetting({key, prev});
#include <vector>
#include <utility>
namespace {
void delPrefixes(const char** prefixes, size_t size) {
std::vector<String> to_purge;
using namespace settings;
kv_store.foreach([&](kvs_type::KeyValueResult&& kv) {
auto key = kv.key.read();
for (size_t index = 0; index < size; ++index) {
if (key.startsWith(prefixes[index])) {
to_purge.push_back(std::move(key));
return;
}
} }
});
for (auto& key : to_purge) {
delSetting(key);
} }
} }
template <size_t Size>
void delPrefixes(const char* (&&prefixes)[Size]) {
delPrefixes(prefixes, Size);
}
} // namespace
// Configuration versions // Configuration versions
// //
// 1: based on Embedis, no board definitions // 1: based on Embedis, no board definitions
@ -44,34 +63,40 @@ void migrate() {
const auto version = migrateVersion(); const auto version = migrateVersion();
setSetting("cfg", CFG_VERSION); setSetting("cfg", CFG_VERSION);
if (!version) return;
if (!version) {
return;
}
// get rid of old keys that were never used until now
// and some very old keys that were forced via migrate.ino
switch (version) { switch (version) {
// migrate old version with 1-based indices
case 2:
_cmpMoveIndexDown("ledGPIO");
_cmpMoveIndexDown("ledLogic");
_cmpMoveIndexDown("btnGPIO");
_cmpMoveIndexDown("btnRelay", -1);
_cmpMoveIndexDown("relayGPIO");
_cmpMoveIndexDown("relayType");
// fall through
// get rid / move some existing keys from old migrate.ino
case 3:
moveSettings("chGPIO", "ltDimmerGPIO");
moveSettings("myDIGPIO", "ltMy92DIGPIO");
moveSettings("myDCKGPIO", "ltMy92DCKGPIO");
moveSettings("myChips", "ltMy92Chips");
moveSettings("myModel", "ltMy92Model");
moveSettings("chLogic", "ltDimmerInv");
moveSettings("ledLogic", "ledInv");
delSetting("lightProvider");
delSetting("relayProvider");
delSetting("relays");
delSetting("board");
// fall through
default:
break;
case 2:
delPrefixes({
"btnGPIO",
"ledGPIO",
"ledLogic",
"relayGPIO",
"relayType"
});
// fall through
case 3:
case 4:
delPrefixes({
"board",
"chGPIO",
"chLogic",
"ledGPIO",
"ledLogic",
"lightProvider",
"myChips",
"myDCKGPIO",
"myDIGPIO",
"relayGPIO",
"relayProvider",
"relayType",
"relays"
});
break;
} }
saveSettings(); saveSettings();


+ 29
- 5
code/espurna/settings.cpp View File

@ -335,18 +335,42 @@ String getSetting(const SettingsKey& key, String&& defaultValue) {
return result.value; return result.value;
} }
bool delSetting(const String& key) {
return settings::kv_store.del(key);
}
bool delSetting(const SettingsKey& key) { bool delSetting(const SettingsKey& key) {
return settings::kv_store.del(key.toString());
return delSetting(key.toString());
}
bool delSetting(const char* key) {
return delSetting(String(key));
}
bool delSetting(const __FlashStringHelper* key) {
return delSetting(String(key));
}
bool hasSetting(const String& key) {
return settings::kv_store.has(key);
} }
bool hasSetting(const SettingsKey& key) { bool hasSetting(const SettingsKey& key) {
return settings::kv_store.has(key.toString());
return hasSetting(key.toString());
}
bool hasSetting(const char* key) {
return hasSetting(String(key));
}
bool hasSetting(const __FlashStringHelper* key) {
return hasSetting(String(key));
} }
void saveSettings() { void saveSettings() {
#if not SETTINGS_AUTOSAVE
eepromCommit();
#endif
#if not SETTINGS_AUTOSAVE
eepromCommit();
#endif
} }
void resetSettings() { void resetSettings() {


+ 7
- 0
code/espurna/settings.h View File

@ -233,7 +233,14 @@ bool setSetting(const SettingsKey& key, T value) {
return setSetting(key, std::move(String(value))); return setSetting(key, std::move(String(value)));
} }
bool delSetting(const char* key);
bool delSetting(const String& key);
bool delSetting(const __FlashStringHelper* key);
bool delSetting(const SettingsKey& key); bool delSetting(const SettingsKey& key);
bool hasSetting(const char* key);
bool hasSetting(const String& key);
bool hasSetting(const __FlashStringHelper* key);
bool hasSetting(const SettingsKey& key); bool hasSetting(const SettingsKey& key);
void saveSettings(); void saveSettings();


Loading…
Cancel
Save