Settings.h 1.5 KB

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