configuration.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "../include/configuration.h"
  2. #include <SmingCore/SmingCore.h>
  3. ClockConfig ActiveConfig;
  4. ClockConfig
  5. loadConfig ()
  6. {
  7. DynamicJsonBuffer jsonBuffer;
  8. ClockConfig cfg;
  9. if (fileExist (CLOCK_CONFIG_FILE))
  10. {
  11. int size = fileGetSize (CLOCK_CONFIG_FILE);
  12. char* jsonString = new char[size + 1];
  13. fileGetContent (CLOCK_CONFIG_FILE, jsonString, size + 1);
  14. JsonObject& root = jsonBuffer.parseObject (jsonString);
  15. JsonObject& network = root["network"];
  16. cfg.NetworkSSID = String ((const char*) network["ssid"]);
  17. cfg.NetworkPassword = String ((const char*) network["password"]);
  18. JsonObject& correction = root["correction"];
  19. cfg.AddTZ = correction["TZ"];
  20. JsonObject& light = root["light"];
  21. cfg.LightTrhLow = light["low"];
  22. cfg.LightTrhHigh = light["high"];
  23. JsonObject& bright = root["bright"];
  24. cfg.BrightnessLow = bright["low"];
  25. cfg.BrightnessMiddle = bright["mid"];
  26. cfg.BrightnessHigh = bright["high"];
  27. delete[] jsonString;
  28. }
  29. else
  30. {
  31. cfg.NetworkSSID = WIFI_SSID;
  32. cfg.NetworkPassword = WIFI_PWD;
  33. }
  34. return cfg;
  35. }
  36. void
  37. saveConfig (ClockConfig& cfg)
  38. {
  39. ActiveConfig = cfg;
  40. DynamicJsonBuffer jsonBuffer;
  41. JsonObject& root = jsonBuffer.createObject ();
  42. JsonObject& network = jsonBuffer.createObject ();
  43. root["network"] = network;
  44. network["ssid"] = cfg.NetworkSSID.c_str ();
  45. network["password"] = cfg.NetworkPassword.c_str ();
  46. JsonObject& correction = jsonBuffer.createObject ();
  47. root["correction"] = correction;
  48. correction["TZ"] = cfg.AddTZ;
  49. JsonObject& light = jsonBuffer.createObject ();
  50. root["light"] = light;
  51. light["low"] = cfg.LightTrhLow;
  52. light["high"] = cfg.LightTrhHigh;
  53. JsonObject& bright = jsonBuffer.createObject ();
  54. root["bright"] = bright;
  55. bright["low"] = cfg.BrightnessLow;
  56. bright["mid"] = cfg.BrightnessMiddle;
  57. bright["high"] = cfg.BrightnessHigh;
  58. char buf[3048];
  59. root.prettyPrintTo (buf, sizeof(buf));
  60. fileSetContent (CLOCK_CONFIG_FILE, buf);
  61. }