Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.7 KiB

  1. /*
  2. MIGRATE MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include "settings.h"
  6. #include "config/version.h"
  7. #include <vector>
  8. #include <utility>
  9. void delSettingPrefix(const std::initializer_list<const char*>& prefixes) {
  10. std::vector<String> to_purge;
  11. using namespace settings;
  12. kv_store.foreach([&](kvs_type::KeyValueResult&& kv) {
  13. auto key = kv.key.read();
  14. for (const auto* prefix : prefixes) {
  15. if (key.startsWith(prefix)) {
  16. to_purge.push_back(std::move(key));
  17. return;
  18. }
  19. }
  20. });
  21. for (auto& key : to_purge) {
  22. delSetting(key);
  23. }
  24. }
  25. void delSettingPrefix(const char* prefix) {
  26. delSettingPrefix({prefix});
  27. }
  28. void delSettingPrefix(const String& prefix) {
  29. delSettingPrefix(prefix.c_str());
  30. }
  31. // Configuration versions
  32. //
  33. // 1: based on Embedis, no board definitions
  34. // 2: based on Embedis, with board definitions 1-based
  35. // 3: based on Embedis, with board definitions 0-based
  36. // 4: based on Embedis, no board definitions
  37. // 5: based on Embedis, updated rfb codes format
  38. int migrateVersion() {
  39. const static auto version = getSetting("cfg", CFG_VERSION);
  40. if (version == CFG_VERSION) {
  41. return 0;
  42. }
  43. return version;
  44. }
  45. void migrate() {
  46. // We either get 0, when version did not change
  47. // Or, the version we migrate from
  48. const auto version = migrateVersion();
  49. setSetting("cfg", CFG_VERSION);
  50. if (!version) {
  51. return;
  52. }
  53. // get rid of old keys that were never used until now
  54. // and some very old keys that were forced via migrate.ino
  55. switch (version) {
  56. case 2:
  57. case 3:
  58. case 4:
  59. delSetting("board");
  60. break;
  61. }
  62. saveSettings();
  63. }