diff --git a/code/espurna/migrate.cpp b/code/espurna/migrate.cpp index 9e0892c7..984e625f 100644 --- a/code/espurna/migrate.cpp +++ b/code/espurna/migrate.cpp @@ -8,18 +8,37 @@ Copyright (C) 2016-2019 by Xose PĂ©rez #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 +#include + +namespace { + +void delPrefixes(const char** prefixes, size_t size) { + std::vector 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 +void delPrefixes(const char* (&&prefixes)[Size]) { + delPrefixes(prefixes, Size); +} + +} // namespace + // Configuration versions // // 1: based on Embedis, no board definitions @@ -44,34 +63,40 @@ void migrate() { const auto version = migrateVersion(); 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) { - // 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(); diff --git a/code/espurna/settings.cpp b/code/espurna/settings.cpp index f75b68cb..d4cdc69c 100644 --- a/code/espurna/settings.cpp +++ b/code/espurna/settings.cpp @@ -335,18 +335,42 @@ String getSetting(const SettingsKey& key, String&& defaultValue) { return result.value; } +bool delSetting(const String& key) { + return settings::kv_store.del(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) { - 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() { - #if not SETTINGS_AUTOSAVE - eepromCommit(); - #endif +#if not SETTINGS_AUTOSAVE + eepromCommit(); +#endif } void resetSettings() { diff --git a/code/espurna/settings.h b/code/espurna/settings.h index 51a6d961..b466f33a 100644 --- a/code/espurna/settings.h +++ b/code/espurna/settings.h @@ -233,7 +233,14 @@ bool setSetting(const SettingsKey& key, T 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 hasSetting(const char* key); +bool hasSetting(const String& key); +bool hasSetting(const __FlashStringHelper* key); bool hasSetting(const SettingsKey& key); void saveSettings();