d1_mini_pro-sensor-mqtt/lib/HomeAssistantDevices/HADevice.cpp

40 lines
1.1 KiB
C++

#include <HADevice.hpp>
HADevice::HADevice(PubSubClient &client, const char *device_type,
const char *name, const char *unique_id)
: client(client), device_type(device_type), name(name),
unique_id(unique_id) {
buffer = (char *)malloc(MQTT_MAX_PACKET_SIZE);
setupDeviceTopic();
setupConfigTopic();
setupStateTopic();
}
void HADevice::setupDeviceTopic() {
device_topic_size =
strlen(base_topic) + 1 + strlen(device_type) + 1 + strlen(unique_id) + 1;
device_topic = (char *)malloc(device_topic_size);
strcpy(device_topic, base_topic);
strcat(device_topic, "/");
strcat(device_topic, device_type);
strcat(device_topic, "/");
strcat(device_topic, unique_id);
}
void HADevice::setupConfigTopic() {
config_topic_size = strlen(device_topic) + 7 + 1;
config_topic = (char *)malloc(config_topic_size);
strcpy(config_topic, device_topic);
strcat(config_topic, "/config");
}
void HADevice::setupStateTopic() {
state_topic_size = strlen(device_topic) + 6 + 1;
state_topic = (char *)malloc(state_topic_size);
strcpy(state_topic, device_topic);
strcat(state_topic, "/state");
}