Settings.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. { }
  60. ~Settings() {
  61. if (deviceIds) {
  62. delete deviceIds;
  63. }
  64. }
  65. bool hasAuthSettings();
  66. bool isAutoRestartEnabled();
  67. size_t getAutoRestartPeriod();
  68. static void deserialize(Settings& settings, String json);
  69. static void load(Settings& settings);
  70. static RadioInterfaceType typeFromString(const String& s);
  71. static String typeToString(RadioInterfaceType type);
  72. void save();
  73. String toJson(const bool prettyPrint = true);
  74. void serialize(Stream& stream, const bool prettyPrint = false);
  75. void updateDeviceIds(JsonArray& arr);
  76. void updateGatewayConfigs(JsonArray& arr);
  77. void patch(JsonObject& obj);
  78. String mqttServer();
  79. uint16_t mqttPort();
  80. String adminUsername;
  81. String adminPassword;
  82. uint8_t cePin;
  83. uint8_t csnPin;
  84. uint8_t resetPin;
  85. RadioInterfaceType radioInterfaceType;
  86. uint16_t *deviceIds;
  87. GatewayConfig **gatewayConfigs;
  88. size_t numGatewayConfigs;
  89. size_t numDeviceIds;
  90. size_t packetRepeats;
  91. size_t httpRepeatFactor;
  92. String _mqttServer;
  93. String mqttUsername;
  94. String mqttPassword;
  95. String mqttTopicPattern;
  96. String mqttUpdateTopicPattern;
  97. String mqttStateTopicPattern;
  98. uint16_t discoveryPort;
  99. uint8_t listenRepeats;
  100. protected:
  101. size_t _autoRestartPeriod;
  102. template <typename T>
  103. void setIfPresent(JsonObject& obj, const char* key, T& var) {
  104. if (obj.containsKey(key)) {
  105. var = obj.get<T>(key);
  106. }
  107. }
  108. };
  109. #endif