Settings.h 2.9 KB

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