Settings.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. class GatewayConfig {
  16. public:
  17. GatewayConfig(uint16_t deviceId, uint16_t port, uint8_t protocolVersion)
  18. : deviceId(deviceId),
  19. port(port),
  20. protocolVersion(protocolVersion)
  21. { }
  22. const uint16_t deviceId;
  23. const uint16_t port;
  24. const uint8_t protocolVersion;
  25. };
  26. class Settings {
  27. public:
  28. Settings() :
  29. adminUsername(""),
  30. adminPassword(""),
  31. // CE and CSN pins from nrf24l01
  32. cePin(D0),
  33. csnPin(D8),
  34. deviceIds(NULL),
  35. gatewayConfigs(NULL),
  36. numDeviceIds(0),
  37. numGatewayConfigs(0),
  38. packetRepeats(10),
  39. httpRepeatFactor(5)
  40. { }
  41. ~Settings() {
  42. if (deviceIds) {
  43. delete deviceIds;
  44. }
  45. }
  46. bool hasAuthSettings() {
  47. return adminUsername.length() > 0 && adminPassword.length() > 0;
  48. }
  49. static void deserialize(Settings& settings, String json);
  50. static void deserialize(Settings& settings, JsonObject& json);
  51. static void load(Settings& settings);
  52. void save();
  53. String toJson(const bool prettyPrint = true);
  54. void serialize(Stream& stream, const bool prettyPrint = false);
  55. void updateDeviceIds(JsonArray& arr);
  56. void updateGatewayConfigs(JsonArray& arr);
  57. void patch(JsonObject& obj);
  58. String adminUsername;
  59. String adminPassword;
  60. uint8_t cePin;
  61. uint8_t csnPin;
  62. uint16_t *deviceIds;
  63. GatewayConfig **gatewayConfigs;
  64. size_t numGatewayConfigs;
  65. size_t numDeviceIds;
  66. size_t packetRepeats;
  67. size_t httpRepeatFactor;
  68. };
  69. #endif