From d7d25083dc451a8af7e27f2ab5c43b96d5107411 Mon Sep 17 00:00:00 2001 From: Daniel Ekman Date: Tue, 21 Dec 2021 14:21:02 +0100 Subject: [PATCH] flat data messages --- src/mqtt/HomeAssistantMqttHandler.cpp | 60 +++++++-------------------- src/web/AmsWebServer.cpp | 2 +- web/json1ha.json | 3 ++ web/json2ha.json | 15 +++++++ web/json3ha.json | 20 +++++++++ web/json3hapf.json | 24 +++++++++++ web/mqtt.html | 2 +- 7 files changed, 80 insertions(+), 46 deletions(-) create mode 100644 web/json1ha.json create mode 100644 web/json2ha.json create mode 100644 web/json3ha.json create mode 100644 web/json3hapf.json diff --git a/src/mqtt/HomeAssistantMqttHandler.cpp b/src/mqtt/HomeAssistantMqttHandler.cpp index fc8a1c8f..24ee83a6 100644 --- a/src/mqtt/HomeAssistantMqttHandler.cpp +++ b/src/mqtt/HomeAssistantMqttHandler.cpp @@ -1,10 +1,10 @@ #include "HomeAssistantMqttHandler.h" #include "hexutils.h" #include "Uptime.h" -#include "web/root/json1_json.h" -#include "web/root/json2_json.h" -#include "web/root/json3_json.h" -#include "web/root/json3pf_json.h" +#include "web/root/json1ha_json.h" +#include "web/root/json2ha_json.h" +#include "web/root/json3ha_json.h" +#include "web/root/json3hapf_json.h" #include "web/root/jsonsys_json.h" #include "web/root/jsonprices_json.h" @@ -14,27 +14,13 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState) { if(data->getListType() == 1) { char json[192]; - snprintf_P(json, sizeof(json), JSON1_JSON, - WiFi.macAddress().c_str(), - clientId.c_str(), - (uint32_t) (millis64()/1000), - data->getPackageTimestamp(), - hw->getVcc(), - hw->getWifiRssi(), - hw->getTemperature(), + snprintf_P(json, sizeof(json), JSON1HA_JSON, data->getActiveImportPower() ); - return mqtt->publish(topic, json); + return mqtt->publish(topic + "/data1", json); } else if(data->getListType() == 2) { char json[384]; - snprintf_P(json, sizeof(json), JSON2_JSON, - WiFi.macAddress().c_str(), - clientId.c_str(), - (uint32_t) (millis64()/1000), - data->getPackageTimestamp(), - hw->getVcc(), - hw->getWifiRssi(), - hw->getTemperature(), + snprintf_P(json, sizeof(json), JSON2HA_JSON, data->getListId().c_str(), data->getMeterId().c_str(), data->getMeterModel().c_str(), @@ -49,18 +35,11 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState) { data->getL2Voltage(), data->getL3Voltage() ); - return mqtt->publish(topic, json); + return mqtt->publish(topic + "/data2", json); } else if(data->getListType() == 3) { if(data->getPowerFactor() == 0) { char json[512]; - snprintf_P(json, sizeof(json), JSON3_JSON, - WiFi.macAddress().c_str(), - clientId.c_str(), - (uint32_t) (millis64()/1000), - data->getPackageTimestamp(), - hw->getVcc(), - hw->getWifiRssi(), - hw->getTemperature(), + snprintf_P(json, sizeof(json), JSON3HA_JSON, data->getListId().c_str(), data->getMeterId().c_str(), data->getMeterModel().c_str(), @@ -80,17 +59,10 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState) { data->getReactiveExportCounter(), data->getMeterTimestamp() ); - return mqtt->publish(topic, json); + return mqtt->publish(topic + "/data3", json); } else { char json[768]; - snprintf_P(json, sizeof(json), JSON3PF_JSON, - WiFi.macAddress().c_str(), - clientId.c_str(), - (uint32_t) (millis64()/1000), - data->getPackageTimestamp(), - hw->getVcc(), - hw->getWifiRssi(), - hw->getTemperature(), + snprintf_P(json, sizeof(json), JSON3HAPF_JSON, data->getListId().c_str(), data->getMeterId().c_str(), data->getMeterModel().c_str(), @@ -114,7 +86,7 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState) { data->getReactiveExportCounter(), data->getMeterTimestamp() ); - return mqtt->publish(topic, json); + return mqtt->publish(topic + "/data3pf", json); } } return false; @@ -144,7 +116,7 @@ bool HomeAssistantMqttHandler::publishTemperatures(AmsConfiguration* config, HwT } char* pos = buf+strlen(buf); snprintf(count == 0 ? pos : pos-1, 8, "}}"); - return mqtt->publish(topic, buf); + return mqtt->publish(topic + "/temperatures", buf); } bool HomeAssistantMqttHandler::publishPrices(EntsoeApi* eapi) { @@ -250,13 +222,13 @@ bool HomeAssistantMqttHandler::publishPrices(EntsoeApi* eapi) { ts3hr, ts6hr ); - return mqtt->publish(topic, json); + return mqtt->publish(topic + "/prices", json); } bool HomeAssistantMqttHandler::publishSystem(HwTools* hw) { if(init || topic.isEmpty() || !mqtt->connected()) return false; - if(!topic.endsWith("/")) topic += "/"; + //if(!topic.endsWith("/")) topic += "/"; char json[192]; snprintf_P(json, sizeof(json), JSONSYS_JSON, @@ -267,6 +239,6 @@ bool HomeAssistantMqttHandler::publishSystem(HwTools* hw) { hw->getWifiRssi(), hw->getTemperature() ); - init = mqtt->publish(topic, json); + init = mqtt->publish(topic + "/state", json); return init; } diff --git a/src/web/AmsWebServer.cpp b/src/web/AmsWebServer.cpp index d14c9c9f..ff05f9a2 100644 --- a/src/web/AmsWebServer.cpp +++ b/src/web/AmsWebServer.cpp @@ -539,7 +539,7 @@ void AmsWebServer::configMqttHtml() { html.replace("{u}", mqtt.username); html.replace("{pw}", mqtt.password); html.replace("{f}", String(mqtt.payloadFormat)); - for(int i = 0; i<4; i++) { + for(int i = 0; i<5; i++) { html.replace("{f" + String(i) + "}", mqtt.payloadFormat == i ? "selected" : ""); } diff --git a/web/json1ha.json b/web/json1ha.json new file mode 100644 index 00000000..9fa1f374 --- /dev/null +++ b/web/json1ha.json @@ -0,0 +1,3 @@ +{ + "P" : %d +} diff --git a/web/json2ha.json b/web/json2ha.json new file mode 100644 index 00000000..8fa16c93 --- /dev/null +++ b/web/json2ha.json @@ -0,0 +1,15 @@ +{ + "lv" : "%s", + "id" : "%s", + "type" : "%s", + "P" : %d, + "Q" : %d, + "PO" : %d, + "QO" : %d, + "I1" : %.2f, + "I2" : %.2f, + "I3" : %.2f, + "U1" : %.2f, + "U2" : %.2f, + "U3" : %.2f +} diff --git a/web/json3ha.json b/web/json3ha.json new file mode 100644 index 00000000..55220a70 --- /dev/null +++ b/web/json3ha.json @@ -0,0 +1,20 @@ +{ + "lv" : "%s", + "id" : "%s", + "type" : "%s", + "P" : %d, + "Q" : %d, + "PO" : %d, + "QO" : %d, + "I1" : %.2f, + "I2" : %.2f, + "I3" : %.2f, + "U1" : %.2f, + "U2" : %.2f, + "U3" : %.2f, + "tPI" : %.2f, + "tPO" : %.2f, + "tQI" : %.2f, + "tQO" : %.2f, + "rtc" : %lu +} diff --git a/web/json3hapf.json b/web/json3hapf.json new file mode 100644 index 00000000..dd3e4a5b --- /dev/null +++ b/web/json3hapf.json @@ -0,0 +1,24 @@ +{ + "lv" : "%s", + "id" : "%s", + "type" : "%s", + "P" : %d, + "Q" : %d, + "PO" : %d, + "QO" : %d, + "I1" : %.2f, + "I2" : %.2f, + "I3" : %.2f, + "U1" : %.2f, + "U2" : %.2f, + "U3" : %.2f, + "PF" : %.2f, + "PF1" : %.2f, + "PF2" : %.2f, + "PF3" : %.2f, + "tPI" : %.2f, + "tPO" : %.2f, + "tQI" : %.2f, + "tQO" : %.2f, + "rtc" : %lu +} diff --git a/web/mqtt.html b/web/mqtt.html index ce128e01..34d445a9 100644 --- a/web/mqtt.html +++ b/web/mqtt.html @@ -62,7 +62,7 @@ - +