From a7856989bf6e982f6a88aceb5614d754b53a5667 Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Fri, 9 Dec 2022 17:49:26 +0100 Subject: [PATCH] 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 --- .../jomjol_flowcontroll/ClassFlowControll.cpp | 4 +- .../jomjol_flowcontroll/ClassFlowControll.h | 2 +- .../jomjol_flowcontroll/ClassFlowMQTT.cpp | 23 +-------- .../ClassFlowPostProcessing.cpp | 51 +++++++++++-------- .../ClassFlowPostProcessing.h | 3 +- code/main/main.cpp | 8 +-- 6 files changed, 41 insertions(+), 50 deletions(-) diff --git a/code/components/jomjol_flowcontroll/ClassFlowControll.cpp b/code/components/jomjol_flowcontroll/ClassFlowControll.cpp index 714a72a05..59eea88b8 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowControll.cpp +++ b/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(); } diff --git a/code/components/jomjol_flowcontroll/ClassFlowControll.h b/code/components/jomjol_flowcontroll/ClassFlowControll.h index 026e30b10..48eaf1f33 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowControll.h +++ b/code/components/jomjol_flowcontroll/ClassFlowControll.h @@ -50,7 +50,7 @@ class ClassFlowControll : 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); diff --git a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp index 60f82d323..e874c664a 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp @@ -239,7 +239,7 @@ bool ClassFlowMQTT::doFlow(string zwtime) { std::vector* 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); } } diff --git a/code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp b/code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp index a4da2cea4..dd4f861e9 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp +++ b/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; diff --git a/code/components/jomjol_flowcontroll/ClassFlowPostProcessing.h b/code/components/jomjol_flowcontroll/ClassFlowPostProcessing.h index 27a26d536..5ae5e08cf 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowPostProcessing.h +++ b/code/components/jomjol_flowcontroll/ClassFlowPostProcessing.h @@ -61,10 +61,11 @@ class ClassFlowPostProcessing : 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(); diff --git a/code/main/main.cpp b/code/main/main.cpp index c26baabde..acd7fd3ab 100644 --- a/code/main/main.cpp +++ b/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) {