configuration.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "configuration.h"
  2. #include <SmingCore.h>
  3. ClockConfig ActiveConfig;
  4. ClockConfig loadConfig(void)
  5. {
  6. DynamicJsonDocument doc(1024);
  7. ClockConfig cfg;
  8. if(Json::loadFromFile(doc, CLOCK_CONFIG_FILE)) {
  9. JsonObject network = doc["network"];
  10. cfg.NetworkSSID = network["ssid"].as<const char*>();
  11. cfg.NetworkPassword = network["password"].as<const char*>();
  12. JsonObject correction = doc["correction"];
  13. cfg.AddTZ = correction["TZ"];
  14. JsonObject light = doc["light"];
  15. cfg.LightTrhLow = light["low"];
  16. cfg.LightTrhHigh = light["high"];
  17. JsonObject bright = doc["bright"];
  18. cfg.BrightnessLow = bright["low"];
  19. cfg.BrightnessMiddle = bright["mid"];
  20. cfg.BrightnessHigh = bright["high"];
  21. } else {
  22. cfg.NetworkSSID = WIFI_SSID;
  23. cfg.NetworkPassword = WIFI_PWD;
  24. }
  25. return cfg;
  26. }
  27. void saveConfig(ClockConfig& cfg)
  28. {
  29. ActiveConfig = cfg;
  30. DynamicJsonDocument doc(1024);
  31. auto network = doc.createNestedObject("network");
  32. network["ssid"] = cfg.NetworkSSID;
  33. network["password"] = cfg.NetworkPassword;
  34. auto correction = doc.createNestedObject("correction");
  35. correction["TZ"] = cfg.AddTZ;
  36. auto light = doc.createNestedObject("light");
  37. light["low"] = cfg.LightTrhLow;
  38. light["high"] = cfg.LightTrhHigh;
  39. auto bright = doc.createNestedObject("bright");
  40. bright["low"] = cfg.BrightnessLow;
  41. bright["mid"] = cfg.BrightnessMiddle;
  42. bright["high"] = cfg.BrightnessHigh;
  43. Json::saveToFile(doc, CLOCK_CONFIG_FILE, Json::Pretty);
  44. }