publish cleanup config on OTA updates

This commit is contained in:
Nicu Hodos 2024-05-02 13:27:05 +02:00
parent 071e363c14
commit 551c1300a6
4 changed files with 31 additions and 0 deletions

View File

@ -1,6 +1,7 @@
#pragma once
#include <ArduinoJson.h>
#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<Component> 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> Component::configs;
struct Command : Component {
char commandTopic[TOPIC_SIZE];

View File

@ -81,6 +81,12 @@ namespace Mqtt {
ts.deleteTask(tPublishInit);
}
void publishCleanupConfig() {
for (List<Ha::Component>::Container* c = Ha::Component::configs.first; c; c = c->next) {
publish(c->t->configTopic, "");
}
}
void publishBme280() {
// StaticJsonDocument<255> jsonDoc;
// jsonDoc["temperature"] = Bme::data.temp;

View File

@ -11,6 +11,7 @@ namespace Ota {
void setup() {
ArduinoOTA.onStart([]() {
Serial.println("Starting OTA");
Mqtt::publishCleanupConfig();
Mqtt::disconnect();
});
ArduinoOTA.onEnd([]() {

20
gateway/include/utils.h Normal file
View File

@ -0,0 +1,20 @@
#pragma once
template <class T>
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;
}
};