소스 검색

Refactor JSON (#1518)

* use correct log level

* corrected logging

* typo

* refactored JSON generagion: removed unused parameters, consolidated into singel function, added "pre"

* Wrapped 'rate' into double quotes, like all other JSON values

Co-authored-by: CaCO3 <caco@ruinelli.ch>
CaCO3 3 년 전
부모
커밋
2768667eb1

+ 2 - 2
code/components/jomjol_flowcontroll/ClassFlowControll.cpp

@@ -701,7 +701,7 @@ string ClassFlowControll::getNumbersName()
     return flowpostprocessing->getNumbersName();
 }
 
-string ClassFlowControll::getJSON(std::string _id, std::string _mac)
+string ClassFlowControll::getJSON()
 {
-    return flowpostprocessing->GetJSON(_id, _mac);
+    return flowpostprocessing->GetJSON();
 }

+ 1 - 1
code/components/jomjol_flowcontroll/ClassFlowControll.h

@@ -50,7 +50,7 @@ public:
 	string UpdatePrevalue(std::string _newvalue, std::string _numbers, bool _extern);
 	string GetPrevalue(std::string _number = "");	
 	bool ReadParameter(FILE* pfile, string& aktparamgraph);	
-	string getJSON(std::string _id = "", std::string _mac = "");
+	string getJSON();
 	string getNumbersName();
 
 	string TranslateAktstatus(std::string _input);

+ 2 - 21
code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp

@@ -239,7 +239,7 @@ bool ClassFlowMQTT::doFlow(string zwtime)
     {
         std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers();
 
-        LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Publishing MQTT topics...");
+        LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Publishing MQTT topics...");
 
         for (int i = 0; i < (*NUMBERS).size(); ++i)
         {
@@ -288,26 +288,7 @@ bool ClassFlowMQTT::doFlow(string zwtime)
             if (resulttimestamp.length() > 0)
                 MQTTPublish(namenumber + "timestamp", resulttimestamp, SetRetainFlag);
 
-            std::string json = "";
-            
-            if (result.length() > 0)
-                json += "{\"value\": "+result;
-            else
-                json += "{\"value\": \"\"";
-
-            json += ", \"raw\": \""+resultraw;
-
-            json += ", \"pre\": \"" + resultpre;
-
-            json += "\", \"error\": \""+resulterror;
-
-            if (resultrate.length() > 0)
-                json += "\", \"rate\": "+resultrate;
-            else
-                json += "\", \"rate\": \"\"";
-
-            json += ", \"timestamp\": \""+resulttimestamp+"\"}";
-
+            std::string json = flowpostprocessing->getJsonFromNumber(i, "\n");
             MQTTPublish(namenumber + "json", json, SetRetainFlag);
         }
     }

+ 30 - 21
code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp

@@ -37,42 +37,51 @@ std::string ClassFlowPostProcessing::getNumbersName()
     return ret;
 }
 
-std::string ClassFlowPostProcessing::GetJSON(std::string _id, std::string _mac, std::string _lineend)
+std::string ClassFlowPostProcessing::GetJSON(std::string _lineend)
 {
     std::string json="{" + _lineend;
 
     for (int i = 0; i < NUMBERS.size(); ++i)
     {
         json += "\"" + NUMBERS[i]->name + "\":"  + _lineend;
-        json += "  {"  + _lineend;
 
-        if (_id.length() > 0)
-            json += "    \"ID\": \"" + _id + "\","  + _lineend;
-        if (_mac.length() > 0)
-            json += "    \"MAC\": \"" + _mac + "\","  + _lineend;
+        json += getJsonFromNumber(i, _lineend) + _lineend;
 
-        if (NUMBERS[i]->ReturnValue.length() > 0)
-            json += "    \"value\": \""      + NUMBERS[i]->ReturnValue          + "\"," + _lineend;
-        else
-            json += "    \"value\": \"\","  + _lineend;
-        json += "    \"raw\": \""        + NUMBERS[i]->ReturnRawValue              + "\","  + _lineend;
-        json += "    \"error\": \""     + NUMBERS[i]->ErrorMessageText             + "\","  + _lineend;
-        if (NUMBERS[i]->ReturnRateValue.length() > 0)
-            json += "    \"rate\": "      + NUMBERS[i]->ReturnRateValue                + ","  + _lineend;
-        else
-            json += "    \"rate\": \"\","  + _lineend;
-
-        json += "    \"timestamp\": \"" + NUMBERS[i]->timeStamp                    + "\""  + _lineend;
         if ((i+1) < NUMBERS.size())
-            json += "  }," + _lineend;
-        else
-            json += "  }" + _lineend;
+            json += "," + _lineend;
     }
     json += "}";
 
     return json;
 }
 
+
+string ClassFlowPostProcessing::getJsonFromNumber(int i, std::string _lineend) {
+	std::string json = "";
+
+	json += "  {" + _lineend;
+
+	if (NUMBERS[i]->ReturnValue.length() > 0)
+		json += "    \"value\": \"" + NUMBERS[i]->ReturnValue + "\"," + _lineend;
+	else
+		json += "    \"value\": \"\"," + _lineend;
+
+	json += "    \"raw\": \"" + NUMBERS[i]->ReturnRawValue + "\"," + _lineend;
+	json += "    \"pre\": \"" + NUMBERS[i]->ReturnPreValue + "\"," + _lineend;
+	json += "    \"error\": \"" + NUMBERS[i]->ErrorMessageText + "\"," + _lineend;
+
+	if (NUMBERS[i]->ReturnRateValue.length() > 0)
+		json += "    \"rate\": \"" + NUMBERS[i]->ReturnRateValue + "\"," + _lineend;
+	else
+		json += "    \"rate\": \"\"," + _lineend;
+
+	json += "    \"timestamp\": \"" + NUMBERS[i]->timeStamp + "\"" + _lineend;
+	json += "  }" + _lineend;
+
+	return json;
+}
+
+
 string ClassFlowPostProcessing::GetPreValue(std::string _number)
 {
     std::string result;

+ 2 - 1
code/components/jomjol_flowcontroll/ClassFlowPostProcessing.h

@@ -61,10 +61,11 @@ public:
     string getReadoutRate(int _number = 0);
     string getReadoutTimeStamp(int _number = 0);
     void SavePreValue();
+    string getJsonFromNumber(int i, std::string _lineend);
     string GetPreValue(std::string _number = "");
     void SetPreValue(double zw, string _numbers, bool _extern = false);
 
-    std::string GetJSON(std::string _id = "", std::string _mac = "", std::string _lineend = "\n");
+    std::string GetJSON(std::string _lineend = "\n");
     std::string getNumbersName();
 
     void UpdateNachkommaDecimalShift();

+ 4 - 4
code/main/main.cpp

@@ -195,7 +195,7 @@ extern "C" void app_main(void)
         ESP_LOGD(TAG, "No SSID and PASSWORD set!!!");
 
     if (hostname != NULL)
-        ESP_LOGD(TAG, "Hostename: %s", hostname);
+        ESP_LOGD(TAG, "Hostname: %s", hostname);
     else
         ESP_LOGD(TAG, "Hostname not set");
 
@@ -230,10 +230,10 @@ extern "C" void app_main(void)
     ESP_LOGD(TAG, "time %s", zw.c_str());
 
     size_t _hsize = getESPHeapSize();
-    if (_hsize < 4000000)
+    if (_hsize < 4000000) // Check for a bit less than 4 MB (but clearly over 2 MB)
     {
-        std::string _zws = "Not enough PSRAM available. Expected 4.194.304 MByte - available: " + std::to_string(_hsize);
-        _zws = _zws + "\nEither not initialized, too small (2MByte only) or not present at all. Firmware cannot start!!";
+        std::string _zws = "Not enough PSRAM available. Expected around 4 MBytes - available: " + std::to_string((float)_hsize/1024/1024) + " MBytes!";
+        _zws = _zws + "\nEither not initialized, too small (2 MByte only) or not present at all. Firmware cannot start!!";
         LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _zws);
     } else { // Bad Camera Status, retry init   
         if (camStatus != ESP_OK) {