From 071e363c14a565770545825f2e15e8d15aa694b7 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Thu, 2 May 2024 12:44:30 +0200 Subject: [PATCH] make publisher accesible to entire Ha namespace --- gateway/include/ha.h | 15 +++++++-------- gateway/include/mqtt.h | 13 +++++++------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/gateway/include/ha.h b/gateway/include/ha.h index 62fb34d..f5b726e 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -7,6 +7,7 @@ #define DEVICE_ID "rc-gateway" namespace Ha { + uint16_t (*publisher)(const char* topic, const char* message); struct Component { const char* name; @@ -70,18 +71,16 @@ namespace Ha { }; - typedef uint16_t (*publisherFunc)(const char* topic, const char* message); struct Switch : Command { static constexpr const char* type = "switch"; char stateTopic[TOPIC_SIZE]; const char* area; - publisherFunc publisher; virtual void onCommand(const char* msg){} virtual void addToMap(){} - Switch(const char* name, const char* id, publisherFunc publisher = nullptr) - : Command(name, id, type), publisher(publisher) { + Switch(const char* name, const char* id) + : Command(name, id, type) { sprintf(commandTopic, "homeassistant/%s/rc-gateway/%s/set", type, id); } @@ -121,8 +120,8 @@ namespace Ha { const char* group; unsigned char channel; - PollinSwitch(const char* name, const char* group, const unsigned char channel, publisherFunc publisher) - : Switch(name, Protocol_1::buildId(group, channel), publisher), group(group), channel(channel) { + PollinSwitch(const char* name, const char* group, const unsigned char channel) + : Switch(name, Protocol_1::buildId(group, channel)), group(group), channel(channel) { 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 off[8] = {4483146, 4626810, 4661562, 4819642}; - EasyHomeSwitch(const char* name, const char* id, unsigned long on[4], unsigned long off[4], publisherFunc publisher) - : Switch(name, id, publisher) { + EasyHomeSwitch(const char* name, const char* id, unsigned long on[4], unsigned long off[4]) + : Switch(name, id) { memcpy(&this->on[4], on, 4*sizeof(unsigned long)); memcpy(&this->off[4], off, 4*sizeof(unsigned long)); sprintf(commandTopic, "homeassistant/%s/rc-gateway/%s/set", type, id); diff --git a/gateway/include/mqtt.h b/gateway/include/mqtt.h index ccb4d84..1652ed6 100644 --- a/gateway/include/mqtt.h +++ b/gateway/include/mqtt.h @@ -48,12 +48,12 @@ namespace Mqtt { }; Ha::Switch* switches[] = { - (new Ha::PollinSwitch{"Meeting sensor", "00001", 1, publish})->withArea("Dining room")->withStateTopic(), - (new Ha::PollinSwitch{"Fire Tv", "00001", 2, publish})->withArea("Living room")->withStateTopic(), - (new Ha::PollinSwitch{"Diningroom player", "00001", 3, publish})->withArea("Dining room")->withStateTopic(), - (new Ha::PollinSwitch{"Train", "11111", 4, publish})->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{"Outside", "easy_home_b", (unsigned long[4]) { 4483140, 4626804, 4661556, 4819636 }, (unsigned long[4]) { 4326548, 4537108, 4767524, 4972708 }, publish})->withArea("Basement")->withStateTopic() + (new Ha::PollinSwitch{"Meeting sensor", "00001", 1})->withArea("Dining room")->withStateTopic(), + (new Ha::PollinSwitch{"Fire Tv", "00001", 2})->withArea("Living room")->withStateTopic(), + (new Ha::PollinSwitch{"Diningroom player", "00001", 3})->withArea("Dining room")->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 }})->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) { @@ -70,6 +70,7 @@ namespace Mqtt { // for (Ha::Component* cmp : sensors) { // publishComponentConfig(*cmp); // } + Ha::publisher = publish; for (Ha::Component* cmp : buttons) { publishComponentConfig(*cmp); }