Settings.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <Arduino.h>
  2. #include <StringStream.h>
  3. #include <ArduinoJson.h>
  4. #ifndef _SETTINGS_H_INCLUDED
  5. #define _SETTINGS_H_INCLUDED
  6. #ifndef FIRMWARE_VARIANT
  7. #define FIRMWARE_VARIANT "unknown"
  8. #endif
  9. #ifndef MILIGHT_HUB_VERSION
  10. #define MILIGHT_HUB_VERSION "unknown"
  11. #endif
  12. #define SETTINGS_FILE "/config.json"
  13. #define SETTINGS_TERMINATOR '\0'
  14. #define WEB_INDEX_FILENAME "/web/index.html"
  15. #define MILIGHT_GITHUB_USER "sidoh"
  16. #define MILIGHT_GITHUB_REPO "esp8266_milight_hub"
  17. #define MILIGHT_REPO_WEB_PATH "/data/web/index.html"
  18. class GatewayConfig {
  19. public:
  20. GatewayConfig(uint16_t deviceId, uint16_t port, uint8_t protocolVersion)
  21. : deviceId(deviceId),
  22. port(port),
  23. protocolVersion(protocolVersion)
  24. { }
  25. const uint16_t deviceId;
  26. const uint16_t port;
  27. const uint8_t protocolVersion;
  28. };
  29. class Settings {
  30. public:
  31. Settings() :
  32. adminUsername(""),
  33. adminPassword(""),
  34. // CE and CSN pins from nrf24l01
  35. cePin(D0),
  36. csnPin(D8),
  37. deviceIds(NULL),
  38. gatewayConfigs(NULL),
  39. numDeviceIds(0),
  40. numGatewayConfigs(0),
  41. packetRepeats(10),
  42. httpRepeatFactor(5)
  43. { }
  44. ~Settings() {
  45. if (deviceIds) {
  46. delete deviceIds;
  47. }
  48. }
  49. bool hasAuthSettings() {
  50. return adminUsername.length() > 0 && adminPassword.length() > 0;
  51. }
  52. static void deserialize(Settings& settings, String json);
  53. static void deserialize(Settings& settings, JsonObject& json);
  54. static void load(Settings& settings);
  55. void save();
  56. String toJson(const bool prettyPrint = true);
  57. void serialize(Stream& stream, const bool prettyPrint = false);
  58. void updateDeviceIds(JsonArray& arr);
  59. void updateGatewayConfigs(JsonArray& arr);
  60. void patch(JsonObject& obj);
  61. String adminUsername;
  62. String adminPassword;
  63. uint8_t cePin;
  64. uint8_t csnPin;
  65. uint16_t *deviceIds;
  66. GatewayConfig **gatewayConfigs;
  67. size_t numGatewayConfigs;
  68. size_t numDeviceIds;
  69. size_t packetRepeats;
  70. size_t httpRepeatFactor;
  71. };
  72. #endif