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.

241 lines
6.4 KiB

  1. /*
  2. SETTINGS MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #pragma once
  6. #include <Arduino.h>
  7. #include <functional>
  8. #include <utility>
  9. #include <vector>
  10. #include <ArduinoJson.h>
  11. #include "espurna.h"
  12. #include "broker.h"
  13. #include "libs/EmbedisWrap.h"
  14. BrokerDeclare(ConfigBroker, void(const String& key, const String& value));
  15. // --------------------------------------------------------------------------
  16. class settings_key_t {
  17. public:
  18. settings_key_t(const char* value, unsigned char index) :
  19. value(value), index(index)
  20. {}
  21. settings_key_t(const String& value, unsigned char index) :
  22. value(value), index(index)
  23. {}
  24. settings_key_t(const char* value) :
  25. value(value), index(-1)
  26. {}
  27. settings_key_t(const String& value) :
  28. value(value), index(-1)
  29. {}
  30. settings_key_t(const __FlashStringHelper* value) :
  31. value(value), index(-1)
  32. {}
  33. settings_key_t() :
  34. value(), index(-1)
  35. {}
  36. bool match(const char* _value) const {
  37. return (value == _value) || (toString() == _value);
  38. }
  39. bool match(const String& _value) const {
  40. return (value == _value) || (toString() == _value);
  41. }
  42. String toString() const;
  43. explicit operator String () const {
  44. return toString();
  45. }
  46. private:
  47. const String value;
  48. int index;
  49. };
  50. using settings_move_key_t = std::pair<settings_key_t, settings_key_t>;
  51. using settings_filter_t = std::function<String(String& value)>;
  52. // --------------------------------------------------------------------------
  53. struct settings_cfg_t {
  54. String& setting;
  55. const char* key;
  56. const char* default_value;
  57. };
  58. using settings_cfg_list_t = std::initializer_list<settings_cfg_t>;
  59. // --------------------------------------------------------------------------
  60. namespace settings {
  61. namespace internal {
  62. uint32_t u32fromString(const String& string, int base);
  63. template <typename T>
  64. using convert_t = T(*)(const String& value);
  65. template <typename T>
  66. T convert(const String& value);
  67. // --------------------------------------------------------------------------
  68. template <>
  69. float convert(const String& value);
  70. template <>
  71. double convert(const String& value);
  72. template <>
  73. int convert(const String& value);
  74. template <>
  75. long convert(const String& value);
  76. template <>
  77. bool convert(const String& value);
  78. template <>
  79. unsigned long convert(const String& value);
  80. template <>
  81. unsigned int convert(const String& value);
  82. template <>
  83. unsigned short convert(const String& value);
  84. template <>
  85. unsigned char convert(const String& value);
  86. template<typename T>
  87. String serialize(const T& value);
  88. template<typename T>
  89. String serialize(const T& value) {
  90. return String(value);
  91. }
  92. } // namespace settings::internal
  93. } // namespace settings
  94. // --------------------------------------------------------------------------
  95. struct settings_key_match_t {
  96. using match_f = bool(*)(const char* key);
  97. using key_f = const String(*)(const String& key);
  98. match_f match;
  99. key_f key;
  100. };
  101. void settingsRegisterDefaults(const settings_key_match_t& matcher);
  102. String settingsQueryDefaults(const String& key);
  103. // --------------------------------------------------------------------------
  104. void moveSetting(const String& from, const String& to);
  105. void moveSetting(const String& from, const String& to, unsigned int index);
  106. void moveSettings(const String& from, const String& to);
  107. template<typename R, settings::internal::convert_t<R> Rfunc = settings::internal::convert>
  108. R getSetting(const settings_key_t& key, R defaultValue) __attribute__((noinline));
  109. template<typename R, settings::internal::convert_t<R> Rfunc = settings::internal::convert>
  110. R getSetting(const settings_key_t& key, R defaultValue) {
  111. String value;
  112. if (!Embedis::get(key.toString(), value)) {
  113. return defaultValue;
  114. }
  115. return Rfunc(value);
  116. }
  117. template<>
  118. String getSetting(const settings_key_t& key, String defaultValue);
  119. String getSetting(const settings_key_t& key);
  120. String getSetting(const settings_key_t& key, const char* defaultValue);
  121. String getSetting(const settings_key_t& key, const __FlashStringHelper* defaultValue);
  122. template<typename T>
  123. bool setSetting(const settings_key_t& key, const T& value) {
  124. return Embedis::set(key.toString(), String(value));
  125. }
  126. template<>
  127. bool setSetting(const settings_key_t& key, const String& value);
  128. bool delSetting(const settings_key_t& key);
  129. bool hasSetting(const settings_key_t& key);
  130. void saveSettings();
  131. void resetSettings();
  132. void settingsGetJson(JsonObject& data);
  133. bool settingsRestoreJson(char* json_string, size_t json_buffer_size = 1024);
  134. bool settingsRestoreJson(JsonObject& data);
  135. size_t settingsKeyCount();
  136. String settingsKeyName(unsigned int index);
  137. std::vector<String> settingsKeys();
  138. void settingsProcessConfig(const settings_cfg_list_t& config, settings_filter_t filter = nullptr);
  139. unsigned long settingsSize();
  140. void migrate();
  141. void settingsSetup();
  142. // -----------------------------------------------------------------------------
  143. // Deprecated implementation
  144. // -----------------------------------------------------------------------------
  145. template <typename T>
  146. String getSetting(const String& key, unsigned char index, T defaultValue)
  147. __attribute__((deprecated("getSetting({key, index}, default) should be used instead")));
  148. template<typename T>
  149. bool setSetting(const String& key, unsigned char index, T value)
  150. __attribute__((deprecated("setSetting({key, index}, value) should be used instead")));
  151. template<typename T>
  152. bool hasSetting(const String& key, unsigned char index)
  153. __attribute__((deprecated("hasSetting({key, index}) should be used instead")));
  154. template<typename T>
  155. bool delSetting(const String& key, unsigned char index)
  156. __attribute__((deprecated("delSetting({key, index}) should be used instead")));
  157. // --------------------------------------------------------------------------
  158. template<typename T>
  159. String getSetting(const String& key, unsigned char index, T defaultValue) {
  160. return getSetting({key, index}, defaultValue);
  161. }
  162. template<typename T>
  163. bool setSetting(const String& key, unsigned char index, T value) {
  164. return setSetting({key, index}, value);
  165. }
  166. template<typename T>
  167. bool hasSetting(const String& key, unsigned char index) {
  168. return hasSetting({key, index});
  169. }
  170. template<typename T>
  171. bool delSetting(const String& key, unsigned char index) {
  172. return delSetting({key, index});
  173. }