Skip to content

Commit

Permalink
Refactor JSON (#1518)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
caco3 and CaCO3 committed Dec 9, 2022
1 parent 38ff8fd commit a785698
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 50 deletions.
4 changes: 2 additions & 2 deletions code/components/jomjol_flowcontroll/ClassFlowControll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion code/components/jomjol_flowcontroll/ClassFlowControll.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 2 additions & 21 deletions code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}
}
Expand Down
51 changes: 30 additions & 21 deletions code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions code/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a785698

Please sign in to comment.