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.

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