make publisher accesible to entire Ha namespace

This commit is contained in:
Nicu Hodos 2024-05-02 12:44:30 +02:00
parent 6b37d61b5c
commit 071e363c14
2 changed files with 14 additions and 14 deletions

View File

@ -7,6 +7,7 @@
#define DEVICE_ID "rc-gateway" #define DEVICE_ID "rc-gateway"
namespace Ha { namespace Ha {
uint16_t (*publisher)(const char* topic, const char* message);
struct Component { struct Component {
const char* name; const char* name;
@ -70,18 +71,16 @@ namespace Ha {
}; };
typedef uint16_t (*publisherFunc)(const char* topic, const char* message);
struct Switch : Command { struct Switch : Command {
static constexpr const char* type = "switch"; static constexpr const char* type = "switch";
char stateTopic[TOPIC_SIZE]; char stateTopic[TOPIC_SIZE];
const char* area; const char* area;
publisherFunc publisher;
virtual void onCommand(const char* msg){} virtual void onCommand(const char* msg){}
virtual void addToMap(){} virtual void addToMap(){}
Switch(const char* name, const char* id, publisherFunc publisher = nullptr) Switch(const char* name, const char* id)
: Command(name, id, type), publisher(publisher) { : Command(name, id, type) {
sprintf(commandTopic, "homeassistant/%s/rc-gateway/%s/set", type, id); sprintf(commandTopic, "homeassistant/%s/rc-gateway/%s/set", type, id);
} }
@ -121,8 +120,8 @@ namespace Ha {
const char* group; const char* group;
unsigned char channel; unsigned char channel;
PollinSwitch(const char* name, const char* group, const unsigned char channel, publisherFunc publisher) PollinSwitch(const char* name, const char* group, const unsigned char channel)
: Switch(name, Protocol_1::buildId(group, channel), publisher), group(group), channel(channel) { : Switch(name, Protocol_1::buildId(group, channel)), group(group), channel(channel) {
sprintf(commandTopic, "homeassistant/%s/rc-gateway/%s/set", type, id); sprintf(commandTopic, "homeassistant/%s/rc-gateway/%s/set", type, id);
} }
@ -147,8 +146,8 @@ namespace Ha {
unsigned long on[8] = {4326554, 4537114, 4767530, 4972714}; unsigned long on[8] = {4326554, 4537114, 4767530, 4972714};
unsigned long off[8] = {4483146, 4626810, 4661562, 4819642}; unsigned long off[8] = {4483146, 4626810, 4661562, 4819642};
EasyHomeSwitch(const char* name, const char* id, unsigned long on[4], unsigned long off[4], publisherFunc publisher) EasyHomeSwitch(const char* name, const char* id, unsigned long on[4], unsigned long off[4])
: Switch(name, id, publisher) { : Switch(name, id) {
memcpy(&this->on[4], on, 4*sizeof(unsigned long)); memcpy(&this->on[4], on, 4*sizeof(unsigned long));
memcpy(&this->off[4], off, 4*sizeof(unsigned long)); memcpy(&this->off[4], off, 4*sizeof(unsigned long));
sprintf(commandTopic, "homeassistant/%s/rc-gateway/%s/set", type, id); sprintf(commandTopic, "homeassistant/%s/rc-gateway/%s/set", type, id);

View File

@ -48,12 +48,12 @@ namespace Mqtt {
}; };
Ha::Switch* switches[] = { Ha::Switch* switches[] = {
(new Ha::PollinSwitch{"Meeting sensor", "00001", 1, publish})->withArea("Dining room")->withStateTopic(), (new Ha::PollinSwitch{"Meeting sensor", "00001", 1})->withArea("Dining room")->withStateTopic(),
(new Ha::PollinSwitch{"Fire Tv", "00001", 2, publish})->withArea("Living room")->withStateTopic(), (new Ha::PollinSwitch{"Fire Tv", "00001", 2})->withArea("Living room")->withStateTopic(),
(new Ha::PollinSwitch{"Diningroom player", "00001", 3, publish})->withArea("Dining room")->withStateTopic(), (new Ha::PollinSwitch{"Diningroom player", "00001", 3})->withArea("Dining room")->withStateTopic(),
(new Ha::PollinSwitch{"Train", "11111", 4, publish})->withArea("Playroom")->withStateTopic(), (new Ha::PollinSwitch{"Train", "11111", 4})->withArea("Playroom")->withStateTopic(),
(new Ha::EasyHomeSwitch{"FritzBox", "easy_home_a", (unsigned long[4]) { 4483136, 4626800, 4661552, 4819632 }, (unsigned long[4]) { 4326544, 4537104, 4767520, 4972704 }, publish})->withArea("Basement")->withStateTopic(), (new Ha::EasyHomeSwitch{"FritzBox", "easy_home_a", (unsigned long[4]) { 4483136, 4626800, 4661552, 4819632 }, (unsigned long[4]) { 4326544, 4537104, 4767520, 4972704 }})->withArea("Basement")->withStateTopic(),
(new Ha::EasyHomeSwitch{"Outside", "easy_home_b", (unsigned long[4]) { 4483140, 4626804, 4661556, 4819636 }, (unsigned long[4]) { 4326548, 4537108, 4767524, 4972708 }, publish})->withArea("Basement")->withStateTopic() (new Ha::EasyHomeSwitch{"Outside", "easy_home_b", (unsigned long[4]) { 4483140, 4626804, 4661556, 4819636 }, (unsigned long[4]) { 4326548, 4537108, 4767524, 4972708 }})->withArea("Basement")->withStateTopic()
}; };
void publishComponentConfig(Ha::Component& component) { void publishComponentConfig(Ha::Component& component) {
@ -70,6 +70,7 @@ namespace Mqtt {
// for (Ha::Component* cmp : sensors) { // for (Ha::Component* cmp : sensors) {
// publishComponentConfig(*cmp); // publishComponentConfig(*cmp);
// } // }
Ha::publisher = publish;
for (Ha::Component* cmp : buttons) { for (Ha::Component* cmp : buttons) {
publishComponentConfig(*cmp); publishComponentConfig(*cmp);
} }