configuration.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "../include/configuration.h"
  2. #include <SmingCore/SmingCore.h>
  3. MeteoConfig ActiveConfig;
  4. MeteoConfig loadConfig()
  5. {
  6. DynamicJsonBuffer jsonBuffer;
  7. MeteoConfig cfg;
  8. if (fileExist(METEO_CONFIG_FILE))
  9. {
  10. int size = fileGetSize(METEO_CONFIG_FILE);
  11. char* jsonString = new char[size + 1];
  12. fileGetContent(METEO_CONFIG_FILE, jsonString, size + 1);
  13. JsonObject& root = jsonBuffer.parseObject(jsonString);
  14. JsonObject& network = root["network"];
  15. cfg.NetworkSSID = String((const char*)network["ssid"]);
  16. cfg.NetworkPassword = String((const char*)network["password"]);
  17. JsonObject& correction = root["correction"];
  18. cfg.AddT = correction["T"];
  19. cfg.AddRH = correction["RH"];
  20. cfg.AddTZ = correction["TZ"];
  21. JsonObject& trigger = root["trigger"];
  22. cfg.Trigger = (TriggerType)(int)trigger["type"];
  23. cfg.RangeMin = trigger["min"];
  24. cfg.RangeMax = trigger["max"];
  25. delete[] jsonString;
  26. }
  27. else
  28. {
  29. cfg.NetworkSSID = WIFI_SSID;
  30. cfg.NetworkPassword = WIFI_PWD;
  31. }
  32. return cfg;
  33. }
  34. void saveConfig(MeteoConfig& cfg)
  35. {
  36. ActiveConfig = cfg;
  37. DynamicJsonBuffer jsonBuffer;
  38. JsonObject& root = jsonBuffer.createObject();
  39. JsonObject& network = jsonBuffer.createObject();
  40. root["network"] = network;
  41. network["ssid"] = cfg.NetworkSSID.c_str();
  42. network["password"] = cfg.NetworkPassword.c_str();
  43. JsonObject& correction = jsonBuffer.createObject();
  44. root["correction"] = correction;
  45. correction["T"] = cfg.AddT;
  46. correction["RH"] = cfg.AddRH;
  47. correction["TZ"] = cfg.AddTZ;
  48. JsonObject& trigger = jsonBuffer.createObject();
  49. root["trigger"] = trigger;
  50. trigger["type"] = (int)cfg.Trigger;
  51. trigger["min"] = cfg.RangeMin;
  52. trigger["max"] = cfg.RangeMax;
  53. char buf[3048];
  54. root.prettyPrintTo(buf, sizeof(buf));
  55. fileSetContent(METEO_CONFIG_FILE, buf);
  56. }