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.

100 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 "config/version.h"
  7. #include <vector>
  8. #include <utility>
  9. namespace {
  10. void delPrefixes(const std::initializer_list<const char*>& prefixes) {
  11. std::vector<String> to_purge;
  12. using namespace settings;
  13. kv_store.foreach([&](kvs_type::KeyValueResult&& kv) {
  14. auto key = kv.key.read();
  15. for (const auto* prefix : prefixes) {
  16. if (key.startsWith(prefix)) {
  17. to_purge.push_back(std::move(key));
  18. return;
  19. }
  20. }
  21. });
  22. for (auto& key : to_purge) {
  23. delSetting(key);
  24. }
  25. }
  26. } // namespace
  27. // Configuration versions
  28. //
  29. // 1: based on Embedis, no board definitions
  30. // 2: based on Embedis, with board definitions 1-based
  31. // 3: based on Embedis, with board definitions 0-based
  32. // 4: based on Embedis, no board definitions
  33. // 5: based on Embedis, updated rfb codes format
  34. int migrateVersion() {
  35. const static auto version = getSetting("cfg", CFG_VERSION);
  36. if (version == CFG_VERSION) {
  37. return 0;
  38. }
  39. return version;
  40. }
  41. void migrate() {
  42. // We either get 0, when version did not change
  43. // Or, the version we migrate from
  44. const auto version = migrateVersion();
  45. setSetting("cfg", CFG_VERSION);
  46. if (!version) {
  47. return;
  48. }
  49. // get rid of old keys that were never used until now
  50. // and some very old keys that were forced via migrate.ino
  51. switch (version) {
  52. case 2:
  53. delPrefixes({
  54. "btnGPIO",
  55. "ledGPIO",
  56. "ledLogic",
  57. "relayGPIO",
  58. "relayType"
  59. });
  60. // fall through
  61. case 3:
  62. case 4:
  63. delPrefixes({
  64. "board",
  65. "chGPIO",
  66. "chLogic",
  67. "ledGPIO",
  68. "ledLogic",
  69. "lightProvider",
  70. "myChips",
  71. "myDCKGPIO",
  72. "myDIGPIO",
  73. "relayGPIO",
  74. "relayProvider",
  75. "relayType",
  76. "relays"
  77. });
  78. break;
  79. }
  80. saveSettings();
  81. }