noam76

jsonconfig.h

Jul 23rd, 2021
842
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. bool loadConfiguration() {
  2.   File configFile = LittleFS.open("/config.json", "r");
  3.   if (!configFile) {
  4.     Serial.println("Failed to open config file");
  5.     return false;
  6.   }
  7.  
  8.   size_t size = configFile.size();
  9.   if (size > 1024) {
  10.     Serial.println("Config file size is too large");
  11.     return false;
  12.   }
  13.  
  14.   // Allocate a buffer to store contents of the file.
  15.   std::unique_ptr<char[]> buf(new char[size]);
  16.  
  17.   // We don't use String here because ArduinoJson library requires the input
  18.   // buffer to be mutable. If you don't use ArduinoJson, you may as well
  19.   // use configFile.readString instead.
  20.   configFile.readBytes(buf.get(), size);
  21.  
  22.   StaticJsonDocument<512> doc;
  23.   auto error = deserializeJson(doc, buf.get());
  24.   if (error) {
  25.     Serial.println("Failed to parse config file");
  26.   Serial.println(error.f_str());
  27.     return false;
  28.   }
  29.  
  30.   Usermax0 = doc["Usermax0"];
  31.   Usermin0 = doc["Usermin0"];
  32.   Usermax1 = doc["Usermax1"];
  33.   Usermin1 = doc["Usermin1"];
  34.   Usermax2 = doc["Usermax2"];
  35.   Usermin2 = doc["Usermin2"];
  36.   Usermax3 = doc["Usermax3"];
  37.   Usermin3 = doc["Usermin3"];
  38.   Usermax4 = doc["Usermax4"];
  39.   Usermin4 = doc["Usermin4"];
  40.   Usermax5 = doc["Usermax5"];
  41.   Usermin5 = doc["Usermin5"];
  42.   Usermax6 = doc["Usermax6"];
  43.   Usermin6 = doc["Usermin6"];
  44.   Usermax7 = doc["Usermax7"];
  45.   Usermin7 = doc["Usermin7"];
  46.   Usermax8 = doc["Usermax8"];
  47.   Usermin8 = doc["Usermin8"];
  48.   Usermax9 = doc["Usermax9"];
  49.   Usermin9 = doc["Usermin9"];
  50.   Usermax10 = doc["Usermax10"];
  51.   Usermin10 = doc["Usermin10"];
  52.   Usermax11 = doc["Usermax11"];
  53.   Usermin11 = doc["Usermin11"];
  54.   Usermax12 = doc["Usermax12"];
  55.   Usermin12 = doc["Usermin12"];
  56.   Usermax13 = doc["Usermax13"];
  57.   Usermin13 = doc["Usermin13"];
  58.   Usermax14 = doc["Usermax14"];
  59.   Usermin14 = doc["Usermin14"];
  60.   Usermax15 = doc["Usermax15"];
  61.   Usermin15 = doc["Usermin15"];
  62.  
  63.   Serial.print("Usermax0: "); Serial.println(Usermax0);
  64.   Serial.print("Usermin0: "); Serial.println(Usermin0);
  65.   Serial.print("Usermax1: "); Serial.println(Usermax1);
  66.   Serial.print("Usermin1: "); Serial.println(Usermin1);
  67.   Serial.print("Usermax2: "); Serial.println(Usermax2);
  68.   Serial.print("Usermin2: "); Serial.println(Usermin2);
  69.   Serial.print("Usermax3: "); Serial.println(Usermax3);
  70.   Serial.print("Usermin3: "); Serial.println(Usermin3);
  71.   Serial.print("Usermax4: "); Serial.println(Usermax4);
  72.   Serial.print("Usermin4: "); Serial.println(Usermin4);
  73.   Serial.print("Usermax5: "); Serial.println(Usermax5);
  74.   Serial.print("Usermin5: "); Serial.println(Usermin5);
  75.   Serial.print("Usermax6: "); Serial.println(Usermax6);
  76.   Serial.print("Usermin6: "); Serial.println(Usermin6);
  77.   Serial.print("Usermax7: "); Serial.println(Usermax7);
  78.   Serial.print("Usermin7: "); Serial.println(Usermin7);
  79.   Serial.print("Usermax8: "); Serial.println(Usermax8);
  80.   Serial.print("Usermin8: "); Serial.println(Usermin8);
  81.   Serial.print("Usermax9: "); Serial.println(Usermax9);
  82.   Serial.print("Usermin9: "); Serial.println(Usermin9);
  83.   Serial.print("Usermax10: "); Serial.println(Usermax10);
  84.   Serial.print("Usermin10: "); Serial.println(Usermin10);
  85.   Serial.print("Usermax11: "); Serial.println(Usermax11);
  86.   Serial.print("Usermin11: "); Serial.println(Usermin11);
  87.   Serial.print("Usermax12: "); Serial.println(Usermax12);
  88.   Serial.print("Usermin12: "); Serial.println(Usermin12);
  89.   Serial.print("Usermax13: "); Serial.println(Usermax13);
  90.   Serial.print("Usermin13: "); Serial.println(Usermin13);
  91.   Serial.print("Usermax14: "); Serial.println(Usermax14);
  92.   Serial.print("Usermin14: "); Serial.println(Usermin14);
  93.   Serial.print("Usermax15: "); Serial.println(Usermax15);
  94.   Serial.print("Usermin15: "); Serial.println(Usermin15);
  95.   //configFile.close();
  96.   return true;
  97. }
  98.  
  99. bool saveConfiguration() {
  100.   StaticJsonDocument<512> doc;
  101.  
  102.   doc["Usermax0"] = Usermax0;
  103.   doc["Usermin0"] = Usermin0;
  104.   doc["Usermax1"] = Usermax1;
  105.   doc["Usermin1"] = Usermin1;
  106.   doc["Usermax2"] = Usermax2;
  107.   doc["Usermin2"] = Usermin2;
  108.   doc["Usermax3"] = Usermax3;
  109.   doc["Usermin3"] = Usermin3;
  110.   doc["Usermax4"] = Usermax4;
  111.   doc["Usermin4"] = Usermin4;
  112.   doc["Usermax5"] = Usermax5;
  113.   doc["Usermin5"] = Usermin5;
  114.   doc["Usermax6"] = Usermax6;
  115.   doc["Usermin6"] = Usermin6;
  116.   doc["Usermax7"] = Usermax7;
  117.   doc["Usermin7"] = Usermin7;
  118.   doc["Usermax8"] = Usermax8;
  119.   doc["Usermin8"] = Usermin8;
  120.   doc["Usermax9"] = Usermax9;
  121.   doc["Usermin9"] = Usermin9;
  122.   doc["Usermax10"] = Usermax10;
  123.   doc["Usermin10"] = Usermin10;
  124.   doc["Usermax11"] = Usermax11;
  125.   doc["Usermin11"] = Usermin11;
  126.   doc["Usermax12"] = Usermax12;
  127.   doc["Usermin12"] = Usermin12;
  128.   doc["Usermax13"] = Usermax13;
  129.   doc["Usermin13"] = Usermin13;
  130.   doc["Usermax14"] = Usermax14;
  131.   doc["Usermin14"] = Usermin14;
  132.   doc["Usermax15"] = Usermax15;
  133.   doc["Usermin15"] = Usermin15;
  134.  
  135.   File configFile = LittleFS.open("/config.json", "w");
  136.   if (!configFile) {
  137.     Serial.println("Failed to open config file for writing");
  138.     return false;
  139.   }
  140.  
  141.   serializeJson(doc, configFile);
  142.   //configFile.close();
  143.   return true;
  144. }
RAW Paste Data