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>
This commit is contained in:
CaCO3
2022-12-09 17:49:26 +01:00
committed by GitHub
parent 88bdcc9365
commit 2768667eb1
6 changed files with 41 additions and 50 deletions

View File

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

View File

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

View File

@@ -239,7 +239,7 @@ bool ClassFlowMQTT::doFlow(string zwtime)
{ {
std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers(); 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) for (int i = 0; i < (*NUMBERS).size(); ++i)
{ {
@@ -288,26 +288,7 @@ bool ClassFlowMQTT::doFlow(string zwtime)
if (resulttimestamp.length() > 0) if (resulttimestamp.length() > 0)
MQTTPublish(namenumber + "timestamp", resulttimestamp, SetRetainFlag); MQTTPublish(namenumber + "timestamp", resulttimestamp, SetRetainFlag);
std::string json = ""; std::string json = flowpostprocessing->getJsonFromNumber(i, "\n");
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+"\"}";
MQTTPublish(namenumber + "json", json, SetRetainFlag); MQTTPublish(namenumber + "json", json, SetRetainFlag);
} }
} }

View File

@@ -37,42 +37,51 @@ std::string ClassFlowPostProcessing::getNumbersName()
return ret; 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; std::string json="{" + _lineend;
for (int i = 0; i < NUMBERS.size(); ++i) for (int i = 0; i < NUMBERS.size(); ++i)
{ {
json += "\"" + NUMBERS[i]->name + "\":" + _lineend; json += "\"" + NUMBERS[i]->name + "\":" + _lineend;
json += " {" + _lineend;
if (_id.length() > 0) json += getJsonFromNumber(i, _lineend) + _lineend;
json += " \"ID\": \"" + _id + "\"," + _lineend;
if (_mac.length() > 0)
json += " \"MAC\": \"" + _mac + "\"," + _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()) if ((i+1) < NUMBERS.size())
json += " }," + _lineend; json += "," + _lineend;
else
json += " }" + _lineend;
} }
json += "}"; json += "}";
return 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) string ClassFlowPostProcessing::GetPreValue(std::string _number)
{ {
std::string result; std::string result;

View File

@@ -61,10 +61,11 @@ public:
string getReadoutRate(int _number = 0); string getReadoutRate(int _number = 0);
string getReadoutTimeStamp(int _number = 0); string getReadoutTimeStamp(int _number = 0);
void SavePreValue(); void SavePreValue();
string getJsonFromNumber(int i, std::string _lineend);
string GetPreValue(std::string _number = ""); string GetPreValue(std::string _number = "");
void SetPreValue(double zw, string _numbers, bool _extern = false); 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(); std::string getNumbersName();
void UpdateNachkommaDecimalShift(); void UpdateNachkommaDecimalShift();

View File

@@ -195,7 +195,7 @@ extern "C" void app_main(void)
ESP_LOGD(TAG, "No SSID and PASSWORD set!!!"); ESP_LOGD(TAG, "No SSID and PASSWORD set!!!");
if (hostname != NULL) if (hostname != NULL)
ESP_LOGD(TAG, "Hostename: %s", hostname); ESP_LOGD(TAG, "Hostname: %s", hostname);
else else
ESP_LOGD(TAG, "Hostname not set"); ESP_LOGD(TAG, "Hostname not set");
@@ -230,10 +230,10 @@ extern "C" void app_main(void)
ESP_LOGD(TAG, "time %s", zw.c_str()); ESP_LOGD(TAG, "time %s", zw.c_str());
size_t _hsize = getESPHeapSize(); 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); 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 (2MByte only) or not present at all. Firmware cannot start!!"; _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); LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _zws);
} else { // Bad Camera Status, retry init } else { // Bad Camera Status, retry init
if (camStatus != ESP_OK) { if (camStatus != ESP_OK) {