Make topic prefix configurable
This commit is contained in:
23
src/main.cpp
23
src/main.cpp
@@ -35,6 +35,9 @@ std::string generate_client_id_suffix(int length) {
|
||||
|
||||
std::string clientID = "hotdog_" + generate_client_id_suffix(6);
|
||||
|
||||
std::string topic_prefix = "hotdog/";
|
||||
std::string last_will_topic = std::string(topic_prefix) + "availability";
|
||||
|
||||
EspMQTTClient client(
|
||||
SECRET_WIFI_SSID, // Wifi SSID
|
||||
SECRET_WIFI_PASSWORD, // Wifi Password
|
||||
@@ -47,7 +50,7 @@ EspMQTTClient client(
|
||||
|
||||
ShadeRepository shadeRepository = ShadeRepository();
|
||||
|
||||
HADiscovery haDiscovery("hotdog/", [] (const char* topic, const char* message) {
|
||||
HADiscovery haDiscovery(topic_prefix.c_str(), [] (const char* topic, const char* message) {
|
||||
client.publish(topic, message);
|
||||
});
|
||||
|
||||
@@ -85,7 +88,7 @@ void setup() {
|
||||
}
|
||||
|
||||
client.setKeepAlive(10);
|
||||
client.enableLastWillMessage("hotdog/availability", "offline", true);
|
||||
client.enableLastWillMessage(last_will_topic.c_str(), "offline", true);
|
||||
client.enableDebuggingMessages();
|
||||
if (!client.setMaxPacketSize(1024)) {
|
||||
Serial.println("Failed to set max packet size");
|
||||
@@ -94,8 +97,8 @@ void setup() {
|
||||
shadeRepository.addShadeAddedCallback([] (Shade& shade) {
|
||||
// Only add shade if client is already connected
|
||||
if (client.isConnected()) {
|
||||
client.subscribe(("hotdog/" + shade.key + "/command").c_str(), processCommandMessage);
|
||||
client.subscribe(("hotdog/" + shade.key + "/set_position").c_str(), processSetPositionMessage);
|
||||
client.subscribe((topic_prefix + shade.key + "/command").c_str(), processCommandMessage);
|
||||
client.subscribe((topic_prefix + shade.key + "/set_position").c_str(), processSetPositionMessage);
|
||||
}
|
||||
publishDiscoveryTopics();
|
||||
});
|
||||
@@ -193,11 +196,11 @@ void onConnectionEstablished() {
|
||||
Serial.println("Connection established");
|
||||
|
||||
for (auto shade = shadeRepository.begin(); shade != shadeRepository.end(); shade++) {
|
||||
client.subscribe(("hotdog/" + shade->key + "/command").c_str(), processCommandMessage);
|
||||
client.subscribe(("hotdog/" + shade->key + "/set_position").c_str(), processSetPositionMessage);
|
||||
client.subscribe((topic_prefix + shade->key + "/command").c_str(), processCommandMessage);
|
||||
client.subscribe((topic_prefix + shade->key + "/set_position").c_str(), processSetPositionMessage);
|
||||
}
|
||||
|
||||
client.publish("hotdog/availability", "online", true);
|
||||
client.publish((topic_prefix + "availability").c_str(), "online", true);
|
||||
|
||||
publishDiscoveryTopics();
|
||||
|
||||
@@ -388,15 +391,15 @@ void startFetchingPosition(uint16_t shadeID, int8_t targetPosition) {
|
||||
}
|
||||
|
||||
void publishState(const String& shadeName, const String& state) {
|
||||
client.publish("hotdog/" + shadeName + "/state", state, true);
|
||||
client.publish(topic_prefix.c_str() + shadeName + "/state", state, true);
|
||||
}
|
||||
|
||||
void publishPosition(const String& shadeName, const uint8_t position) {
|
||||
client.publish("hotdog/" + shadeName + "/position", String(position), true);
|
||||
client.publish(topic_prefix.c_str() + shadeName + "/position", String(position), true);
|
||||
}
|
||||
|
||||
void publishBattery(const String& shadeName, const uint8_t battery) {
|
||||
client.publish("hotdog/" + shadeName + "/battery", String(battery), true);
|
||||
client.publish(topic_prefix.c_str() + shadeName + "/battery", String(battery), true);
|
||||
}
|
||||
|
||||
void publishDiscoveryTopics() {
|
||||
|
||||
Reference in New Issue
Block a user