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.

104 lines
2.2 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 <vector>
  7. #include <utility>
  8. namespace {
  9. void delPrefixes(const char** prefixes, size_t size) {
  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 (size_t index = 0; index < size; ++index) {
  15. if (key.startsWith(prefixes[index])) {
  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. template <size_t Size>
  26. void delPrefixes(const char* (&&prefixes)[Size]) {
  27. delPrefixes(prefixes, Size);
  28. }
  29. } // namespace
  30. // Configuration versions
  31. //
  32. // 1: based on Embedis, no board definitions
  33. // 2: based on Embedis, with board definitions 1-based
  34. // 3: based on Embedis, with board definitions 0-based
  35. // 4: based on Embedis, no board definitions
  36. // 5: based on Embedis, updated rfb codes format
  37. int migrateVersion() {
  38. const static auto version = getSetting("cfg", CFG_VERSION);
  39. if (version == CFG_VERSION) {
  40. return 0;
  41. }
  42. return version;
  43. }
  44. void migrate() {
  45. // We either get 0, when version did not change
  46. // Or, the version we migrate from
  47. const auto version = migrateVersion();
  48. setSetting("cfg", CFG_VERSION);
  49. if (!version) {
  50. return;
  51. }
  52. // get rid of old keys that were never used until now
  53. // and some very old keys that were forced via migrate.ino
  54. switch (version) {
  55. case 2:
  56. delPrefixes({
  57. "btnGPIO",
  58. "ledGPIO",
  59. "ledLogic",
  60. "relayGPIO",
  61. "relayType"
  62. });
  63. // fall through
  64. case 3:
  65. case 4:
  66. delPrefixes({
  67. "board",
  68. "chGPIO",
  69. "chLogic",
  70. "ledGPIO",
  71. "ledLogic",
  72. "lightProvider",
  73. "myChips",
  74. "myDCKGPIO",
  75. "myDIGPIO",
  76. "relayGPIO",
  77. "relayProvider",
  78. "relayType",
  79. "relays"
  80. });
  81. break;
  82. }
  83. saveSettings();
  84. }