added buffer for writing to mqtt

This commit is contained in:
Yurii
2023-12-02 23:02:48 +03:00
parent e379df388c
commit 3284e84b67
2 changed files with 20 additions and 7 deletions

View File

@@ -1,11 +1,19 @@
#pragma once
#include <Arduino.h>
#include <StreamUtils.h>
class HomeAssistantHelper {
public:
HomeAssistantHelper(PubSubClient& client) :
client(&client)
{
HomeAssistantHelper(PubSubClient& client) {
this->client = &client;
}
void setBufferedClient() {
this->bClient = nullptr;
}
void setBufferedClient(BufferingPrint* bClient) {
this->bClient = bClient;
}
void setDevicePrefix(String value) {
@@ -52,11 +60,14 @@ public:
doc[FPSTR(HA_DEVICE)][FPSTR(HA_CONF_URL)] = deviceConfigUrl;
}
// Feeding the watchdog
yield();
client->beginPublish(topic, measureJson(doc), true);
serializeJson(doc, *client);
if (this->bClient != nullptr) {
serializeJson(doc, *this->bClient);
this->bClient->flush();
} else {
serializeJson(doc, *client);
}
return client->endPublish();
}
@@ -79,6 +90,7 @@ public:
protected:
PubSubClient* client;
BufferingPrint* bClient = nullptr;
String prefix = "homeassistant";
String devicePrefix = "";
String deviceVersion = "1.0";

View File

@@ -15,6 +15,7 @@ lib_deps =
bblanchon/ArduinoJson@^6.20.0
ihormelnyk/OpenTherm Library@^1.1.4
knolleary/PubSubClient@^2.8
bblanchon/StreamUtils@^1.7.3
lennarthennigs/ESP Telnet@^2.1.2
gyverlibs/EEManager@^2.0
gyverlibs/GyverPID@^3.3