fixed RAM problems with JSON

This commit is contained in:
nightflyer88 2020-11-28 20:28:25 +01:00
parent 7335c5f906
commit 19978a7269

View File

@ -4,11 +4,12 @@
(c) 2019-2020 by M. Lehmann (c) 2019-2020 by M. Lehmann
------------------------------------------------------------------ ------------------------------------------------------------------
*/ */
#define CGSCALE_VERSION "2.21" #define CGSCALE_VERSION "2.22"
/* /*
****************************************************************** ******************************************************************
history: history:
V2.22 28.11.20 fixed RAM problems with JSON
V2.21 27.11.20 bug fixed: recompiled, binary file incorrect V2.21 27.11.20 bug fixed: recompiled, binary file incorrect
V2.2 01.11.20 Virtual weights built in V2.2 01.11.20 Virtual weights built in
V2.12 07.10.20 bug fixed: LR value was displayed in the wrong display position V2.12 07.10.20 bug fixed: LR value was displayed in the wrong display position
@ -583,7 +584,7 @@ bool saveModelJson(String modelName) {
return false; return false;
} }
StaticJsonDocument<JSONDOC_SIZE> jsonDoc; DynamicJsonDocument jsonDoc(JSONDOC_SIZE);
if (SPIFFS.exists(MODEL_FILE)) { if (SPIFFS.exists(MODEL_FILE)) {
// read json file // read json file
@ -591,6 +592,7 @@ bool saveModelJson(String modelName) {
auto error = deserializeJson(jsonDoc, f); auto error = deserializeJson(jsonDoc, f);
f.close(); f.close();
if (error) { if (error) {
printConsole(T_ERROR, "save JSON: " + String(error.c_str()));
return false; return false;
} }
// check if model exists // check if model exists
@ -606,6 +608,7 @@ bool saveModelJson(String modelName) {
serializeJson(jsonDoc, f); serializeJson(jsonDoc, f);
f.close(); f.close();
} else { } else {
printConsole(T_ERROR, "save JSON: " + String(error.c_str()));
return false; return false;
} }
} else { } else {
@ -617,6 +620,7 @@ bool saveModelJson(String modelName) {
serializeJson(jsonDoc, f); serializeJson(jsonDoc, f);
f.close(); f.close();
} else { } else {
printConsole(T_ERROR, "JSON is null ");
return false; return false;
} }
} }
@ -628,7 +632,7 @@ bool saveModelJson(String modelName) {
// read model data from json file // read model data from json file
bool openModelJson(String modelName) { bool openModelJson(String modelName) {
StaticJsonDocument<JSONDOC_SIZE> jsonDoc; DynamicJsonDocument jsonDoc(JSONDOC_SIZE);
if (SPIFFS.exists(MODEL_FILE)) { if (SPIFFS.exists(MODEL_FILE)) {
// read json file // read json file
@ -636,6 +640,7 @@ bool openModelJson(String modelName) {
auto error = deserializeJson(jsonDoc, f); auto error = deserializeJson(jsonDoc, f);
f.close(); f.close();
if (error) { if (error) {
printConsole(T_ERROR, "open JSON: " + String(error.c_str()));
return false; return false;
} }
// check if model exists // check if model exists
@ -658,6 +663,7 @@ bool openModelJson(String modelName) {
} }
} }
} else { } else {
printConsole(T_ERROR, "Model name not found");
return false; return false;
} }
@ -669,6 +675,7 @@ bool openModelJson(String modelName) {
return true; return true;
} }
printConsole(T_ERROR, "Modelfile not exists");
return false; return false;
} }
@ -676,7 +683,7 @@ bool openModelJson(String modelName) {
// delete model from json file // delete model from json file
bool deleteModelJson(String modelName) { bool deleteModelJson(String modelName) {
StaticJsonDocument<JSONDOC_SIZE> jsonDoc; DynamicJsonDocument jsonDoc(JSONDOC_SIZE);
if (SPIFFS.exists(MODEL_FILE)) { if (SPIFFS.exists(MODEL_FILE)) {
// read json file // read json file
@ -684,12 +691,14 @@ bool deleteModelJson(String modelName) {
auto error = deserializeJson(jsonDoc, f); auto error = deserializeJson(jsonDoc, f);
f.close(); f.close();
if (error) { if (error) {
printConsole(T_ERROR, "delete JSON: " + String(error.c_str()));
return false; return false;
} }
// check if model exists // check if model exists
if (jsonDoc.containsKey(modelName)) { if (jsonDoc.containsKey(modelName)) {
jsonDoc.remove(modelName); jsonDoc.remove(modelName);
} else { } else {
printConsole(T_ERROR, "Model name not found");
return false; return false;
} }
// if no models in json, kill it // if no models in json, kill it
@ -702,14 +711,15 @@ bool deleteModelJson(String modelName) {
serializeJson(jsonDoc, f); serializeJson(jsonDoc, f);
f.close(); f.close();
} else { } else {
printConsole(T_ERROR, "JSON is null ");
return false; return false;
} }
} }
return true; return true;
} }
printConsole(T_ERROR, "Modelfile not exists");
return false; return false;
} }
@ -790,7 +800,8 @@ void getParameter() {
model.targetCGmin = 0; model.targetCGmin = 0;
model.targetCGmax = 0; model.targetCGmax = 0;
StaticJsonDocument<JSONDOC_SIZE> jsonDoc; //StaticJsonDocument<JSONDOC_SIZE> jsonDoc;
DynamicJsonDocument jsonDoc(JSONDOC_SIZE);
if (SPIFFS.exists(MODEL_FILE)) { if (SPIFFS.exists(MODEL_FILE)) {
// read json file // read json file
@ -866,7 +877,8 @@ void getParameter() {
// send virtual weights to client // send virtual weights to client
void getVirtualWeight() { void getVirtualWeight() {
String response = ""; String response = "";
StaticJsonDocument<JSONDOC_SIZE> jsonDoc; //StaticJsonDocument<JSONDOC_SIZE> jsonDoc;
DynamicJsonDocument jsonDoc(JSONDOC_SIZE);
JsonArray virtw = jsonDoc.createNestedArray("virtual"); JsonArray virtw = jsonDoc.createNestedArray("virtual");
for (int i=0; i < MAX_VIRTUAL_WEIGHT; i++){ for (int i=0; i < MAX_VIRTUAL_WEIGHT; i++){
@ -982,7 +994,8 @@ void saveModel() {
if (server.hasArg("distanceX3")) model.distance[X3] = server.arg("distanceX3").toFloat(); if (server.hasArg("distanceX3")) model.distance[X3] = server.arg("distanceX3").toFloat();
if (server.hasArg("mechanicsType")) model.mechanicsType = server.arg("mechanicsType").toInt(); if (server.hasArg("mechanicsType")) model.mechanicsType = server.arg("mechanicsType").toInt();
if(server.hasArg("virtualWeight")){ if(server.hasArg("virtualWeight")){
StaticJsonDocument<JSONDOC_SIZE> jsonDoc; //StaticJsonDocument<JSONDOC_SIZE> jsonDoc;
DynamicJsonDocument jsonDoc(JSONDOC_SIZE);
String json = server.arg("virtualWeight"); String json = server.arg("virtualWeight");
json.replace("%22", "\""); json.replace("%22", "\"");
deserializeJson(jsonDoc, json); deserializeJson(jsonDoc, json);
@ -1295,7 +1308,6 @@ void setup() {
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
delay(500); delay(500);
Serial.print(".");
if (WiFi.status() == WL_NO_SSID_AVAIL) { if (WiFi.status() == WL_NO_SSID_AVAIL) {
printConsole(T_ERROR, "\nWifi: No SSID available"); printConsole(T_ERROR, "\nWifi: No SSID available");
break; break;
@ -1333,7 +1345,7 @@ void setup() {
printConsole(T_ERROR, "Wifi: " + hostname); printConsole(T_ERROR, "Wifi: " + hostname);
} else { } else {
hostname += ".local"; hostname += ".local";
printConsole(T_RUN, "Wifi: " + hostname); printConsole(T_RUN, "Wifi hostname: " + hostname);
} }
#endif #endif
@ -1662,7 +1674,7 @@ void loop() {
switch (menuPage) switch (menuPage)
{ {
case MENU_HOME: { case MENU_HOME: {
Serial.print(F("\n\n********************************************\nCG scale by M.Lehmann et al. - V")); Serial.print(F("\n\n********************************************\nCG scale by M.Lehmann - V"));
Serial.print(CGSCALE_VERSION); Serial.print(CGSCALE_VERSION);
Serial.print(F("\n\n")); Serial.print(F("\n\n"));