Skip to content

Commit

Permalink
Remove excessive whitespaces and newlines in json (#1904)
Browse files Browse the repository at this point in the history
* .

* .

* .

* .

* .

* remove newlines in json as it is not needed

* .

Co-authored-by: CaCO3 <caco@ruinelli.ch>
  • Loading branch information
caco3 and CaCO3 authored Jan 23, 2023
1 parent 90d7c2e commit 5dc111e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 45 deletions.
60 changes: 29 additions & 31 deletions code/components/jomjol_mqtt/server_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ void sendHomeAssistantDiscoveryTopic(std::string group, std::string field,
std::string topicFull;
std::string configTopic;
std::string payload;
std::string nl = "\n";

configTopic = field;

Expand All @@ -74,63 +73,62 @@ void sendHomeAssistantDiscoveryTopic(std::string group, std::string field,
}

/* See https://www.home-assistant.io/docs/mqtt/discovery/ */
payload = "{" + nl +
"\"~\": \"" + maintopic + "\"," + nl +
"\"unique_id\": \"" + maintopic + "-" + configTopic + "\"," + nl +
"\"object_id\": \"" + maintopic + "_" + configTopic + "\"," + nl + // This used to generate the Entity ID
"\"name\": \"" + name + "\"," + nl +
"\"icon\": \"mdi:" + icon + "\"," + nl;
payload = string("{") +
"\"~\": \"" + maintopic + "\"," +
"\"unique_id\": \"" + maintopic + "-" + configTopic + "\"," +
"\"object_id\": \"" + maintopic + "_" + configTopic + "\"," + // This used to generate the Entity ID
"\"name\": \"" + name + "\"," +
"\"icon\": \"mdi:" + icon + "\",";

if (group != "") {
if (field == "problem") { // Special binary sensor which is based on error topic
payload += "\"state_topic\": \"~/" + group + "/error\"," + nl;
payload += "\"value_template\": \"{{ 'OFF' if 'no error' in value else 'ON'}}\"," + nl;
payload += "\"state_topic\": \"~/" + group + "/error\",";
payload += "\"value_template\": \"{{ 'OFF' if 'no error' in value else 'ON'}}\",";
}
else {
payload += "\"state_topic\": \"~/" + group + "/" + field + "\"," + nl;
payload += "\"state_topic\": \"~/" + group + "/" + field + "\",";
}
}
else {
if (field == "problem") { // Special binary sensor which is based on error topic
payload += "\"state_topic\": \"~/error\"," + nl;
payload += "\"value_template\": \"{{ 'OFF' if 'no error' in value else 'ON'}}\"," + nl;
payload += "\"state_topic\": \"~/error\",";
payload += "\"value_template\": \"{{ 'OFF' if 'no error' in value else 'ON'}}\",";
}
else {
payload += "\"state_topic\": \"~/" + field + "\"," + nl;
payload += "\"state_topic\": \"~/" + field + "\",";
}
}

if (unit != "") {
payload += "\"unit_of_meas\": \"" + unit + "\"," + nl;
payload += "\"unit_of_meas\": \"" + unit + "\",";
}

if (deviceClass != "") {
payload += "\"device_class\": \"" + deviceClass + "\"," + nl;
payload += "\"device_class\": \"" + deviceClass + "\",";
}

if (stateClass != "") {
payload += "\"state_class\": \"" + stateClass + "\"," + nl;
payload += "\"state_class\": \"" + stateClass + "\",";
}

if (entityCategory != "") {
payload += "\"entity_category\": \"" + entityCategory + "\"," + nl;
payload += "\"entity_category\": \"" + entityCategory + "\",";
}

payload +=
"\"availability_topic\": \"~/" + std::string(LWT_TOPIC) + "\"," + nl +
"\"payload_available\": \"" + LWT_CONNECTED + "\"," + nl +
"\"payload_not_available\": \"" + LWT_DISCONNECTED + "\"," + nl;

payload +=
"\"device\": {" + nl +
"\"identifiers\": [\"" + maintopic + "\"]," + nl +
"\"name\": \"" + maintopic + "\"," + nl +
"\"model\": \"Meter Digitizer\"," + nl +
"\"manufacturer\": \"AI on the Edge Device\"," + nl +
"\"sw_version\": \"" + version + "\"," + nl +
"\"configuration_url\": \"http://" + *getIPAddress() + "\"" + nl +
"}" + nl +
"}" + nl;
"\"availability_topic\": \"~/" + std::string(LWT_TOPIC) + "\"," +
"\"payload_available\": \"" + LWT_CONNECTED + "\"," +
"\"payload_not_available\": \"" + LWT_DISCONNECTED + "\",";

payload += string("\"device\": {") +
"\"identifiers\": [\"" + maintopic + "\"]," +
"\"name\": \"" + maintopic + "\"," +
"\"model\": \"Meter Digitizer\"," +
"\"manufacturer\": \"AI on the Edge Device\"," +
"\"sw_version\": \"" + version + "\"," +
"\"configuration_url\": \"http://" + *getIPAddress() + "\"" +
"}" +
"}";

MQTTPublish(topicFull, payload, true);
}
Expand Down
26 changes: 12 additions & 14 deletions code/main/server_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,20 +356,18 @@ esp_err_t sysinfo_handler(httpd_req_t *req)
const char *hostname;
ESP_ERROR_CHECK(tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &hostname));

zw = "[\
{\
\"firmware\" : \"" + gitversion + "\",\
\"buildtime\" : \"" + buildtime + "\",\
\"gitbranch\" : \"" + gitbranch + "\",\
\"gittag\" : \"" + gittag + "\",\
\"gitrevision\" : \"" + gitrevision + "\",\
\"html\" : \"" + htmlversion + "\",\
\"cputemp\" : \"" + cputemp + "\",\
\"hostname\" : \"" + hostname + "\",\
\"IPv4\" : \"" + ip4addr_ntoa(&ip_info.ip) + "\",\
\"freeHeapMem\" : \"" + freeheapmem + "\"\
}\
]";
zw = string("[{") +
"\"firmware\": \"" + gitversion + "\"," +
"\"buildtime\": \"" + buildtime + "\"," +
"\"gitbranch\": \"" + gitbranch + "\"," +
"\"gittag\": \"" + gittag + "\"," +
"\"gitrevision\": \"" + gitrevision + "\"," +
"\"html\": \"" + htmlversion + "\"," +
"\"cputemp\": \"" + cputemp + "\"," +
"\"hostname\": \"" + hostname + "\"," +
"\"IPv4\": \"" + ip4addr_ntoa(&ip_info.ip) + "\"," +
"\"freeHeapMem\": \"" + freeheapmem + "\"" +
"}]";

resp_str = zw.c_str();

Expand Down

0 comments on commit 5dc111e

Please sign in to comment.