Settings.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include <Arduino.h>
  2. #include <StringStream.h>
  3. #include <ArduinoJson.h>
  4. #include <GroupStateField.h>
  5. #include <RF24PowerLevel.h>
  6. #include <RF24Channel.h>
  7. #include <Size.h>
  8. #include <LEDStatus.h>
  9. #include <AuthProviders.h>
  10. #include <MiLightRemoteType.h>
  11. #include <BulbId.h>
  12. #include <vector>
  13. #include <memory>
  14. #include <map>
  15. #ifndef _SETTINGS_H_INCLUDED
  16. #define _SETTINGS_H_INCLUDED
  17. #ifndef MILIGHT_HUB_SETTINGS_BUFFER_SIZE
  18. #define MILIGHT_HUB_SETTINGS_BUFFER_SIZE 4096
  19. #endif
  20. #define XQUOTE(x) #x
  21. #define QUOTE(x) XQUOTE(x)
  22. #ifndef FIRMWARE_NAME
  23. #define FIRMWARE_NAME unknown
  24. #endif
  25. #ifndef FIRMWARE_VARIANT
  26. #define FIRMWARE_VARIANT unknown
  27. #endif
  28. #ifndef MILIGHT_HUB_VERSION
  29. #define MILIGHT_HUB_VERSION unknown
  30. #endif
  31. #ifndef MILIGHT_MAX_STATE_ITEMS
  32. #define MILIGHT_MAX_STATE_ITEMS 100
  33. #endif
  34. #ifndef MILIGHT_MAX_STALE_MQTT_GROUPS
  35. #define MILIGHT_MAX_STALE_MQTT_GROUPS 10
  36. #endif
  37. #define SETTINGS_FILE "/config.json"
  38. #define SETTINGS_TERMINATOR '\0'
  39. #define WEB_INDEX_FILENAME "/web/index.html"
  40. #define MILIGHT_GITHUB_USER "sidoh"
  41. #define MILIGHT_GITHUB_REPO "esp8266_milight_hub"
  42. #define MILIGHT_REPO_WEB_PATH "/data/web/index.html"
  43. #define MINIMUM_RESTART_PERIOD 1
  44. #define DEFAULT_MQTT_PORT 1883
  45. #define MAX_IP_ADDR_LEN 15
  46. enum RadioInterfaceType {
  47. nRF24 = 0,
  48. LT8900 = 1,
  49. };
  50. enum class WifiMode {
  51. B, G, N
  52. };
  53. static const std::vector<GroupStateField> DEFAULT_GROUP_STATE_FIELDS({
  54. GroupStateField::STATE,
  55. GroupStateField::BRIGHTNESS,
  56. GroupStateField::COMPUTED_COLOR,
  57. GroupStateField::MODE,
  58. GroupStateField::COLOR_TEMP,
  59. GroupStateField::BULB_MODE
  60. });
  61. struct GatewayConfig {
  62. GatewayConfig(uint16_t deviceId, uint16_t port, uint8_t protocolVersion);
  63. const uint16_t deviceId;
  64. const uint16_t port;
  65. const uint8_t protocolVersion;
  66. };
  67. class Settings {
  68. public:
  69. Settings() :
  70. adminUsername(""),
  71. adminPassword(""),
  72. // CE and CSN pins from nrf24l01
  73. cePin(4),
  74. csnPin(15),
  75. resetPin(0),
  76. ledPin(-2),
  77. radioInterfaceType(nRF24),
  78. packetRepeats(50),
  79. httpRepeatFactor(1),
  80. listenRepeats(3),
  81. discoveryPort(48899),
  82. simpleMqttClientStatus(false),
  83. stateFlushInterval(10000),
  84. mqttStateRateLimit(500),
  85. packetRepeatThrottleThreshold(200),
  86. packetRepeatThrottleSensitivity(0),
  87. packetRepeatMinimum(3),
  88. enableAutomaticModeSwitching(false),
  89. ledModeWifiConfig(LEDStatus::LEDMode::FastToggle),
  90. ledModeWifiFailed(LEDStatus::LEDMode::On),
  91. ledModeOperating(LEDStatus::LEDMode::SlowBlip),
  92. ledModePacket(LEDStatus::LEDMode::Flicker),
  93. ledModePacketCount(3),
  94. hostname("milight-hub"),
  95. rf24PowerLevel(RF24PowerLevelHelpers::defaultValue()),
  96. rf24Channels(RF24ChannelHelpers::allValues()),
  97. groupStateFields(DEFAULT_GROUP_STATE_FIELDS),
  98. rf24ListenChannel(RF24Channel::RF24_LOW),
  99. packetRepeatsPerLoop(10),
  100. wifiMode(WifiMode::N),
  101. _autoRestartPeriod(0)
  102. { }
  103. ~Settings() { }
  104. bool isAuthenticationEnabled() const;
  105. const String& getUsername() const;
  106. const String& getPassword() const;
  107. bool isAutoRestartEnabled();
  108. size_t getAutoRestartPeriod();
  109. static void load(Settings& settings);
  110. static RadioInterfaceType typeFromString(const String& s);
  111. static String typeToString(RadioInterfaceType type);
  112. static std::vector<RF24Channel> defaultListenChannels();
  113. void save();
  114. String toJson(const bool prettyPrint = true);
  115. void serialize(Print& stream, const bool prettyPrint = false);
  116. void updateDeviceIds(JsonArray arr);
  117. void updateGatewayConfigs(JsonArray arr);
  118. void patch(JsonObject obj);
  119. String mqttServer();
  120. uint16_t mqttPort();
  121. std::map<String, BulbId>::const_iterator findAlias(MiLightRemoteType deviceType, uint16_t deviceId, uint8_t groupId);
  122. String adminUsername;
  123. String adminPassword;
  124. uint8_t cePin;
  125. uint8_t csnPin;
  126. uint8_t resetPin;
  127. int8_t ledPin;
  128. RadioInterfaceType radioInterfaceType;
  129. size_t packetRepeats;
  130. size_t httpRepeatFactor;
  131. uint8_t listenRepeats;
  132. uint16_t discoveryPort;
  133. String _mqttServer;
  134. String mqttUsername;
  135. String mqttPassword;
  136. String mqttTopicPattern;
  137. String mqttUpdateTopicPattern;
  138. String mqttStateTopicPattern;
  139. String mqttClientStatusTopic;
  140. bool simpleMqttClientStatus;
  141. size_t stateFlushInterval;
  142. size_t mqttStateRateLimit;
  143. size_t packetRepeatThrottleThreshold;
  144. size_t packetRepeatThrottleSensitivity;
  145. size_t packetRepeatMinimum;
  146. bool enableAutomaticModeSwitching;
  147. LEDStatus::LEDMode ledModeWifiConfig;
  148. LEDStatus::LEDMode ledModeWifiFailed;
  149. LEDStatus::LEDMode ledModeOperating;
  150. LEDStatus::LEDMode ledModePacket;
  151. size_t ledModePacketCount;
  152. String hostname;
  153. RF24PowerLevel rf24PowerLevel;
  154. std::vector<uint16_t> deviceIds;
  155. std::vector<RF24Channel> rf24Channels;
  156. std::vector<GroupStateField> groupStateFields;
  157. std::vector<std::shared_ptr<GatewayConfig>> gatewayConfigs;
  158. RF24Channel rf24ListenChannel;
  159. String wifiStaticIP;
  160. String wifiStaticIPNetmask;
  161. String wifiStaticIPGateway;
  162. size_t packetRepeatsPerLoop;
  163. std::map<String, BulbId> groupIdAliases;
  164. std::map<uint32_t, BulbId> deletedGroupIdAliases;
  165. String homeAssistantDiscoveryPrefix;
  166. WifiMode wifiMode;
  167. protected:
  168. size_t _autoRestartPeriod;
  169. void parseGroupIdAliases(JsonObject json);
  170. void dumpGroupIdAliases(JsonObject json);
  171. static WifiMode wifiModeFromString(const String& mode);
  172. static String wifiModeToString(WifiMode mode);
  173. template <typename T>
  174. void setIfPresent(JsonObject obj, const char* key, T& var) {
  175. if (obj.containsKey(key)) {
  176. JsonVariant val = obj[key];
  177. var = val.as<T>();
  178. }
  179. }
  180. };
  181. #endif