publish cleanup config on OTA updates
This commit is contained in:
parent
071e363c14
commit
551c1300a6
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
#define JSON_SIZE 512
|
#define JSON_SIZE 512
|
||||||
#define TOPIC_SIZE 255
|
#define TOPIC_SIZE 255
|
||||||
@ -14,9 +15,11 @@ namespace Ha {
|
|||||||
char* id;
|
char* id;
|
||||||
const char* type;
|
const char* type;
|
||||||
char configTopic[TOPIC_SIZE];
|
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) {
|
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);
|
sprintf(configTopic, "homeassistant/%s/rc-gateway/%s/config", type, id);
|
||||||
|
configs.add(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void buildUniqueId(char* uniqueId) = 0;
|
virtual void buildUniqueId(char* uniqueId) = 0;
|
||||||
@ -42,6 +45,7 @@ namespace Ha {
|
|||||||
identifiers.add(DEVICE_ID);
|
identifiers.add(DEVICE_ID);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
List<Component> Component::configs;
|
||||||
|
|
||||||
struct Command : Component {
|
struct Command : Component {
|
||||||
char commandTopic[TOPIC_SIZE];
|
char commandTopic[TOPIC_SIZE];
|
||||||
|
|||||||
@ -81,6 +81,12 @@ namespace Mqtt {
|
|||||||
ts.deleteTask(tPublishInit);
|
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() {
|
void publishBme280() {
|
||||||
// StaticJsonDocument<255> jsonDoc;
|
// StaticJsonDocument<255> jsonDoc;
|
||||||
// jsonDoc["temperature"] = Bme::data.temp;
|
// jsonDoc["temperature"] = Bme::data.temp;
|
||||||
|
|||||||
@ -11,6 +11,7 @@ namespace Ota {
|
|||||||
void setup() {
|
void setup() {
|
||||||
ArduinoOTA.onStart([]() {
|
ArduinoOTA.onStart([]() {
|
||||||
Serial.println("Starting OTA");
|
Serial.println("Starting OTA");
|
||||||
|
Mqtt::publishCleanupConfig();
|
||||||
Mqtt::disconnect();
|
Mqtt::disconnect();
|
||||||
});
|
});
|
||||||
ArduinoOTA.onEnd([]() {
|
ArduinoOTA.onEnd([]() {
|
||||||
|
|||||||
20
gateway/include/utils.h
Normal file
20
gateway/include/utils.h
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user