Settings.h 2.6 KB

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