mqtt refactoring, change version to 1.4.0-rc.1

* added MqttWriter
* added MqttWiFiClient (modified WiFiClient for esp8266)
* adaptation HomeAssistantHelper for MqttWriter
* adaptation HaHelper for new HomeAssistantHelper
This commit is contained in:
Yurii
2023-12-16 00:29:19 +03:00
parent 21ed8f2a14
commit 315a975aa8
7 changed files with 721 additions and 528 deletions

View File

@@ -5,133 +5,130 @@ class HomeAssistantHelper {
public:
HomeAssistantHelper() {}
HomeAssistantHelper(PubSubClient* client) {
this->setClient(client);
void setWriter() {
this->writer = nullptr;
}
void setClient() {
this->client = nullptr;
void setWriter(MqttWriter* writer) {
this->writer = writer;
}
void setClient(PubSubClient* client) {
this->client = client;
void setEventPublishCallback(std::function<void(const char*, bool)> callback) {
this->eventPublishCallback = callback;
}
void setYieldCallback(void(*yieldCallback)(void*)) {
this->yieldCallback = yieldCallback;
this->yieldArg = nullptr;
void setEventPublishCallback() {
this->eventPublishCallback = nullptr;
}
void setYieldCallback(void(*yieldCallback)(void*), void* arg) {
this->yieldCallback = yieldCallback;
this->yieldArg = arg;
void setDevicePrefix(const char* value) {
this->devicePrefix = value;
}
void setDevicePrefix(String value) {
devicePrefix = value;
void setDeviceVersion(const char* value) {
this->deviceVersion = value;
}
void setDeviceVersion(String value) {
deviceVersion = value;
void setDeviceManufacturer(const char* value) {
this->deviceManufacturer = value;
}
void setDeviceManufacturer(String value) {
deviceManufacturer = value;
void setDeviceModel(const char* value) {
this->deviceModel = value;
}
void setDeviceModel(String value) {
deviceModel = value;
void setDeviceName(const char* value) {
this->deviceName = value;
}
void setDeviceName(String value) {
deviceName = value;
}
void setDeviceConfigUrl(String value) {
deviceConfigUrl = value;
void setDeviceConfigUrl(const char* value) {
this->deviceConfigUrl = value;
}
bool publish(const char* topic, JsonDocument& doc) {
if (this->client == nullptr) {
if (this->writer == nullptr) {
this->eventPublishCallback(topic, false);
return false;
}
doc[FPSTR(HA_DEVICE)][FPSTR(HA_IDENTIFIERS)][0] = devicePrefix;
doc[FPSTR(HA_DEVICE)][FPSTR(HA_SW_VERSION)] = deviceVersion;
doc[FPSTR(HA_DEVICE)][FPSTR(HA_IDENTIFIERS)][0] = this->devicePrefix;
doc[FPSTR(HA_DEVICE)][FPSTR(HA_SW_VERSION)] = this->deviceVersion;
if (deviceManufacturer) {
doc[FPSTR(HA_DEVICE)][FPSTR(HA_MANUFACTURER)] = deviceManufacturer;
if (this->deviceManufacturer != nullptr) {
doc[FPSTR(HA_DEVICE)][FPSTR(HA_MANUFACTURER)] = this->deviceManufacturer;
}
if (deviceModel) {
doc[FPSTR(HA_DEVICE)][FPSTR(HA_MODEL)] = deviceModel;
if (this->deviceModel != nullptr) {
doc[FPSTR(HA_DEVICE)][FPSTR(HA_MODEL)] = this->deviceModel;
}
if (deviceName) {
doc[FPSTR(HA_DEVICE)][FPSTR(HA_NAME)] = deviceName;
if (this->deviceName != nullptr) {
doc[FPSTR(HA_DEVICE)][FPSTR(HA_NAME)] = this->deviceName;
}
if (deviceConfigUrl) {
doc[FPSTR(HA_DEVICE)][FPSTR(HA_CONF_URL)] = deviceConfigUrl;
if (this->deviceConfigUrl != nullptr) {
doc[FPSTR(HA_DEVICE)][FPSTR(HA_CONF_URL)] = this->deviceConfigUrl;
}
size_t docSize = measureJson(doc);
uint8_t* buffer = (uint8_t*) malloc(docSize * sizeof(*buffer));
size_t length = serializeJson(doc, buffer, docSize);
size_t written = 0;
if (length != 0) {
if (this->client->beginPublish(topic, docSize, true)) {
for (size_t offset = 0; offset < docSize; offset += 128) {
size_t packetSize = offset + 128 <= docSize ? 128 : docSize - offset;
written += this->client->write(buffer + offset, packetSize);
}
this->client->flush();
}
}
free(buffer);
Log.straceln("MQTT", "Publish %u of %u bytes to topic: %s", written, docSize, topic);
if (this->yieldCallback != nullptr) {
this->yieldCallback(yieldArg);
bool result = this->writer->publish(topic, doc, true);
if (this->eventPublishCallback) {
this->eventPublishCallback(topic, result);
}
return docSize == written;
return result;
}
bool publish(const char* topic) {
if (this->client == nullptr) {
if (this->writer == nullptr) {
this->eventPublishCallback(topic, false);
return false;
}
return client->publish(topic, NULL, true);
bool result = writer->publish(topic, nullptr, 0, true);
if (this->eventPublishCallback) {
this->eventPublishCallback(topic, result);
}
return result;
}
String getTopic(const char* category, const char* name, const char* nameSeparator = "/") {
String getTopic(const char* category, const char* name, char nameSeparator = '/') {
String topic = "";
topic.concat(prefix);
topic.concat("/");
topic.concat(this->prefix);
topic.concat('/');
topic.concat(category);
topic.concat("/");
topic.concat(devicePrefix);
topic.concat('/');
topic.concat(this->devicePrefix);
topic.concat(nameSeparator);
topic.concat(name);
topic.concat("/config");
return topic;
}
template <class T> String getDeviceTopic(T value, char separator = '/') {
String topic = "";
topic.concat(this->devicePrefix);
topic.concat(separator);
topic.concat(value);
return topic;
}
template <class T> String getObjectId(T value, char separator = '_') {
String topic = "";
topic.concat(this->devicePrefix);
topic.concat(separator);
topic.concat(value);
return topic;
}
protected:
void(*yieldCallback)(void*) = nullptr;
void* yieldArg = nullptr;
PubSubClient* client = nullptr;
String prefix = "homeassistant";
String devicePrefix = "";
String deviceVersion = "1.0";
String deviceManufacturer = "Community";
String deviceModel = "";
String deviceName = "";
String deviceConfigUrl = "";
std::function<void(const char*, bool)> eventPublishCallback = nullptr;
MqttWriter* writer = nullptr;
const char* prefix = "homeassistant";
const char* devicePrefix = "";
const char* deviceVersion = "1.0";
const char* deviceManufacturer = nullptr;
const char* deviceModel = nullptr;
const char* deviceName = nullptr;
const char* deviceConfigUrl = nullptr;
};