Settings.h 2.7 KB

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