From 2213ad7bce72bed710a84015dfa7d0e2219a7cfc Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Tue, 24 Sep 2024 21:47:56 +0200 Subject: [PATCH] MQTT Hass: Move serialization and allocation check into own method --- include/MqttHandleHass.h | 1 + src/MqttHandleHass.cpp | 50 ++++++++++++---------------------------- 2 files changed, 16 insertions(+), 35 deletions(-) diff --git a/include/MqttHandleHass.h b/include/MqttHandleHass.h index 6a104941..c5acca4c 100644 --- a/include/MqttHandleHass.h +++ b/include/MqttHandleHass.h @@ -59,6 +59,7 @@ public: private: void loop(); static void publish(const String& subtopic, const String& payload); + static void publish(const String& subtopic, const JsonDocument& doc); // Binary Sensor static void publishBinarySensor(JsonDocument& doc, const String& root_device, const String& unique_id_prefix, const String& name, const String& state_topic, const String& payload_on, const String& payload_off, const String& device_class, const String& category); diff --git a/src/MqttHandleHass.cpp b/src/MqttHandleHass.cpp index 5407defa..3f0b045a 100644 --- a/src/MqttHandleHass.cpp +++ b/src/MqttHandleHass.cpp @@ -171,13 +171,7 @@ void MqttHandleHassClass::publishInverterField(std::shared_ptr root["stat_cla"] = stateCls; } - if (!Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) { - return; - } - - String buffer; - serializeJson(root, buffer); - publish(configTopic, buffer); + publish(configTopic, root); } else { publish(configTopic, ""); } @@ -213,13 +207,7 @@ void MqttHandleHassClass::publishInverterButton(std::shared_ptr inv) @@ -330,6 +312,16 @@ void MqttHandleHassClass::publish(const String& subtopic, const String& payload) yield(); } +void MqttHandleHassClass::publish(const String& subtopic, const JsonDocument& doc) +{ + if (!Utils::checkJsonAlloc(doc, __FUNCTION__, __LINE__)) { + return; + } + String buffer; + serializeJson(doc, buffer); + publish(subtopic, buffer); +} + void MqttHandleHassClass::publishBinarySensor(JsonDocument& doc, const String& root_device, const String& unique_id_prefix, const String& name, const String& state_topic, const String& payload_on, const String& payload_off, const String& device_class, const String& category) { String sensor_id = name; @@ -349,14 +341,8 @@ void MqttHandleHassClass::publishBinarySensor(JsonDocument& doc, const String& r doc["ent_cat"] = category; } - if (!Utils::checkJsonAlloc(doc, __FUNCTION__, __LINE__)) { - return; - } - - String buffer; const String configTopic = "binary_sensor/" + root_device + "/" + sensor_id + "/config"; - serializeJson(doc, buffer); - publish(configTopic, buffer); + publish(configTopic, doc); } void MqttHandleHassClass::publishDtuBinarySensor(const String& name, const String& state_topic, const String& payload_on, const String& payload_off, const String& device_class, const String& category) @@ -405,14 +391,8 @@ void MqttHandleHassClass::publishSensor(JsonDocument& doc, const String& root_de doc["pl_avail"] = config.Mqtt.Lwt.Value_Online; doc["pl_not_avail"] = config.Mqtt.Lwt.Value_Offline; - if (!Utils::checkJsonAlloc(doc, __FUNCTION__, __LINE__)) { - return; - } - - String buffer; const String configTopic = "sensor/" + root_device + "/" + sensor_id + "/config"; - serializeJson(doc, buffer); - publish(configTopic, buffer); + publish(configTopic, doc); } void MqttHandleHassClass::publishDtuSensor(const String& name, const String& state_topic, const String& unit_of_measure, const String& icon, const String& device_class, const String& category)