Settings.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. enum RadioInterfaceType {
  22. nRF24 = 0,
  23. LT8900 = 1,
  24. };
  25. class GatewayConfig {
  26. public:
  27. GatewayConfig(uint16_t deviceId, uint16_t port, uint8_t protocolVersion)
  28. : deviceId(deviceId),
  29. port(port),
  30. protocolVersion(protocolVersion)
  31. { }
  32. const uint16_t deviceId;
  33. const uint16_t port;
  34. const uint8_t protocolVersion;
  35. };
  36. class Settings {
  37. public:
  38. Settings() :
  39. adminUsername(""),
  40. adminPassword(""),
  41. // CE and CSN pins from nrf24l01
  42. cePin(D0),
  43. csnPin(D8),
  44. resetPin(0),
  45. radioInterfaceType(nRF24),
  46. deviceIds(NULL),
  47. gatewayConfigs(NULL),
  48. numDeviceIds(0),
  49. numGatewayConfigs(0),
  50. packetRepeats(10),
  51. httpRepeatFactor(5),
  52. _autoRestartPeriod(0)
  53. { }
  54. ~Settings() {
  55. if (deviceIds) {
  56. delete deviceIds;
  57. }
  58. }
  59. bool hasAuthSettings();
  60. bool isAutoRestartEnabled();
  61. size_t getAutoRestartPeriod();
  62. static void deserialize(Settings& settings, String json);
  63. static void deserialize(Settings& settings, JsonObject& 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 adminUsername;
  74. String adminPassword;
  75. uint8_t cePin;
  76. uint8_t csnPin;
  77. uint8_t resetPin;
  78. RadioInterfaceType radioInterfaceType;
  79. uint16_t *deviceIds;
  80. GatewayConfig **gatewayConfigs;
  81. size_t numGatewayConfigs;
  82. size_t numDeviceIds;
  83. size_t packetRepeats;
  84. size_t httpRepeatFactor;
  85. protected:
  86. size_t _autoRestartPeriod;
  87. };
  88. #endif