Settings.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <Arduino.h>
  2. #include <StringStream.h>
  3. #include <ArduinoJson.h>
  4. #ifndef _SETTINGS_H_INCLUDED
  5. #define _SETTINGS_H_INCLUDED
  6. #define XQUOTE(x) #x
  7. #define QUOTE(x) XQUOTE(x)
  8. #ifndef FIRMWARE_VARIANT
  9. #define FIRMWARE_VARIANT unknown
  10. #endif
  11. #ifndef MILIGHT_HUB_VERSION
  12. #define MILIGHT_HUB_VERSION unknown
  13. #endif
  14. #ifndef MILIGHT_MAX_STATE_ITEMS
  15. #define MILIGHT_MAX_STATE_ITEMS 100
  16. #endif
  17. #ifndef MILIGHT_MAX_STALE_MQTT_GROUPS
  18. #define MILIGHT_MAX_STALE_MQTT_GROUPS 10
  19. #endif
  20. #define SETTINGS_FILE "/config.json"
  21. #define SETTINGS_TERMINATOR '\0'
  22. #define WEB_INDEX_FILENAME "/web/index.html"
  23. #define MILIGHT_GITHUB_USER "sidoh"
  24. #define MILIGHT_GITHUB_REPO "esp8266_milight_hub"
  25. #define MILIGHT_REPO_WEB_PATH "/data/web/index.html"
  26. #define MINIMUM_RESTART_PERIOD 1
  27. #define DEFAULT_MQTT_PORT 1883
  28. enum RadioInterfaceType {
  29. nRF24 = 0,
  30. LT8900 = 1,
  31. };
  32. class GatewayConfig {
  33. public:
  34. GatewayConfig(uint16_t deviceId, uint16_t port, uint8_t protocolVersion)
  35. : deviceId(deviceId),
  36. port(port),
  37. protocolVersion(protocolVersion)
  38. { }
  39. const uint16_t deviceId;
  40. const uint16_t port;
  41. const uint8_t protocolVersion;
  42. };
  43. class Settings {
  44. public:
  45. Settings() :
  46. adminUsername(""),
  47. adminPassword(""),
  48. // CE and CSN pins from nrf24l01
  49. cePin(D0),
  50. csnPin(D8),
  51. resetPin(0),
  52. radioInterfaceType(nRF24),
  53. deviceIds(NULL),
  54. gatewayConfigs(NULL),
  55. numDeviceIds(0),
  56. numGatewayConfigs(0),
  57. packetRepeats(50),
  58. httpRepeatFactor(1),
  59. listenRepeats(3),
  60. _autoRestartPeriod(0),
  61. discoveryPort(48899),
  62. stateFlushInterval(1000),
  63. mqttStateRateLimit(500),
  64. packetRepeatThrottleThreshold(200),
  65. packetRepeatThrottleSensitivity(1),
  66. packetRepeatMinimum(3)
  67. { }
  68. ~Settings() {
  69. if (deviceIds) {
  70. delete deviceIds;
  71. }
  72. }
  73. bool hasAuthSettings();
  74. bool isAutoRestartEnabled();
  75. size_t getAutoRestartPeriod();
  76. static void deserialize(Settings& settings, String json);
  77. static void load(Settings& settings);
  78. static RadioInterfaceType typeFromString(const String& s);
  79. static String typeToString(RadioInterfaceType type);
  80. void save();
  81. String toJson(const bool prettyPrint = true);
  82. void serialize(Stream& stream, const bool prettyPrint = false);
  83. void updateDeviceIds(JsonArray& arr);
  84. void updateGatewayConfigs(JsonArray& arr);
  85. void patch(JsonObject& obj);
  86. String mqttServer();
  87. uint16_t mqttPort();
  88. String adminUsername;
  89. String adminPassword;
  90. uint8_t cePin;
  91. uint8_t csnPin;
  92. uint8_t resetPin;
  93. RadioInterfaceType radioInterfaceType;
  94. uint16_t *deviceIds;
  95. GatewayConfig **gatewayConfigs;
  96. size_t numGatewayConfigs;
  97. size_t numDeviceIds;
  98. size_t packetRepeats;
  99. size_t httpRepeatFactor;
  100. String _mqttServer;
  101. String mqttUsername;
  102. String mqttPassword;
  103. String mqttTopicPattern;
  104. String mqttUpdateTopicPattern;
  105. String mqttStateTopicPattern;
  106. uint16_t discoveryPort;
  107. uint8_t listenRepeats;
  108. size_t stateFlushInterval;
  109. size_t mqttStateRateLimit;
  110. size_t packetRepeatThrottleSensitivity;
  111. size_t packetRepeatThrottleThreshold;
  112. size_t packetRepeatMinimum;
  113. protected:
  114. size_t _autoRestartPeriod;
  115. template <typename T>
  116. void setIfPresent(JsonObject& obj, const char* key, T& var) {
  117. if (obj.containsKey(key)) {
  118. var = obj.get<T>(key);
  119. }
  120. }
  121. };
  122. #endif