From 551c1300a623641336d58f7365a98e6c7c027c5f Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Thu, 2 May 2024 13:27:05 +0200 Subject: [PATCH] publish cleanup config on OTA updates --- gateway/include/ha.h | 4 ++++ gateway/include/mqtt.h | 6 ++++++ gateway/include/ota.h | 1 + gateway/include/utils.h | 20 ++++++++++++++++++++ 4 files changed, 31 insertions(+) create mode 100644 gateway/include/utils.h diff --git a/gateway/include/ha.h b/gateway/include/ha.h index f5b726e..500c12f 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -1,6 +1,7 @@ #pragma once #include +#include "utils.h" #define JSON_SIZE 512 #define TOPIC_SIZE 255 @@ -14,9 +15,11 @@ namespace Ha { char* id; const char* type; char configTopic[TOPIC_SIZE]; + static List configs; Component(const char* name, const char* id, const char* type) : name(name), id((char*)id), type(type) { sprintf(configTopic, "homeassistant/%s/rc-gateway/%s/config", type, id); + configs.add(this); } virtual void buildUniqueId(char* uniqueId) = 0; @@ -42,6 +45,7 @@ namespace Ha { identifiers.add(DEVICE_ID); } }; + List Component::configs; struct Command : Component { char commandTopic[TOPIC_SIZE]; diff --git a/gateway/include/mqtt.h b/gateway/include/mqtt.h index 1652ed6..b97d3ab 100644 --- a/gateway/include/mqtt.h +++ b/gateway/include/mqtt.h @@ -81,6 +81,12 @@ namespace Mqtt { ts.deleteTask(tPublishInit); } + void publishCleanupConfig() { + for (List::Container* c = Ha::Component::configs.first; c; c = c->next) { + publish(c->t->configTopic, ""); + } + } + void publishBme280() { // StaticJsonDocument<255> jsonDoc; // jsonDoc["temperature"] = Bme::data.temp; diff --git a/gateway/include/ota.h b/gateway/include/ota.h index eb7a37d..6ff608e 100644 --- a/gateway/include/ota.h +++ b/gateway/include/ota.h @@ -11,6 +11,7 @@ namespace Ota { void setup() { ArduinoOTA.onStart([]() { Serial.println("Starting OTA"); + Mqtt::publishCleanupConfig(); Mqtt::disconnect(); }); ArduinoOTA.onEnd([]() { diff --git a/gateway/include/utils.h b/gateway/include/utils.h new file mode 100644 index 0000000..0c0bbe5 --- /dev/null +++ b/gateway/include/utils.h @@ -0,0 +1,20 @@ +#pragma once + +template +struct List { + struct Container { + T* t; + Container* next; + Container(T* t) : t(t) {} + }; + + Container* first; + Container* last; + + void add(T* t) { + Container* c = new Container{t}; + first == nullptr ? first = c : last->next = c; + last = c; + } + +};