From 5dc111e7a5792d0b4bcf6d3abf111eb75ca5a7df Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Mon, 23 Jan 2023 19:08:24 +0100 Subject: [PATCH] Remove excessive whitespaces and newlines in json (#1904) * . * . * . * . * . * remove newlines in json as it is not needed * . Co-authored-by: CaCO3 --- code/components/jomjol_mqtt/server_mqtt.cpp | 58 ++++++++++----------- code/main/server_main.cpp | 26 +++++---- 2 files changed, 40 insertions(+), 44 deletions(-) diff --git a/code/components/jomjol_mqtt/server_mqtt.cpp b/code/components/jomjol_mqtt/server_mqtt.cpp index a027353d..bef04653 100644 --- a/code/components/jomjol_mqtt/server_mqtt.cpp +++ b/code/components/jomjol_mqtt/server_mqtt.cpp @@ -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; @@ -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; + "\"availability_topic\": \"~/" + std::string(LWT_TOPIC) + "\"," + + "\"payload_available\": \"" + LWT_CONNECTED + "\"," + + "\"payload_not_available\": \"" + LWT_DISCONNECTED + "\","; - 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; + 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); } diff --git a/code/main/server_main.cpp b/code/main/server_main.cpp index d45f7ce1..3e2cd3ae 100644 --- a/code/main/server_main.cpp +++ b/code/main/server_main.cpp @@ -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();