unify Commands and use map to trigger onCommand

This commit is contained in:
Nicu Hodos 2024-05-15 08:04:21 +02:00
parent 7b53efe1ca
commit ae525d7e15
2 changed files with 24 additions and 26 deletions

View File

@ -139,10 +139,22 @@ namespace Ha {
} }
}; };
typedef void (*onMessage)(const char* msg);
struct Command : Component { struct Command : Component {
char commandTopic[TOPIC_SIZE]; char commandTopic[TOPIC_SIZE];
onMessage f;
static unordered_map<string, Command*> mapCommands;
Command(const char* name, const char* id, const char* type) : Component(name, id, type) { Command(const char* name, const char* id, const char* type, onMessage f) : Component(name, id, type), f(f) {
buildCommandTopic();
mapCommands.insert({ string(commandTopic), this });
}
virtual void onCommand(const char* msg) {
if (f) f(msg);
}
virtual void buildCommandTopic() {
sprintf(commandTopic, "homeassistant/%s/%s/%s", type, MAIN_DEVICE_ID, id); sprintf(commandTopic, "homeassistant/%s/%s/%s", type, MAIN_DEVICE_ID, id);
} }
@ -156,12 +168,11 @@ namespace Ha {
} }
}; };
unordered_map<string, Command*> Command::mapCommands;
typedef void (*onMessage)(const char* msg);
struct Button : Command { struct Button : Command {
onMessage f;
Button(const char* name, const char* id, onMessage f = nullptr) : Command(name, id, "button"), f(f) {} Button(const char* name, const char* id, onMessage f = nullptr) : Command(name, id, "button", f) {}
}; };
@ -177,9 +188,9 @@ namespace Ha {
struct Switch : Command, StateConfig<Switch> { struct Switch : Command, StateConfig<Switch> {
virtual void onCommand(const char* msg){} Switch(const char* name, const char* id, onMessage f = nullptr) : Command(name, id, "switch", f) {}
Switch(const char* name, const char* id) : Command(name, id, "switch") { void buildCommandTopic() override {
sprintf(commandTopic, "homeassistant/%s/%s/%s/set", type, MAIN_DEVICE_ID, id); sprintf(commandTopic, "homeassistant/%s/%s/%s/set", type, MAIN_DEVICE_ID, id);
} }

View File

@ -31,21 +31,18 @@ namespace Mqtt {
return client.publish(topic, 0, true, message); return client.publish(topic, 0, true, message);
} }
Button* buttons[] = { Command* commands[] = {
Builder<Button>::instance(new Button{"Restart", "restart", Builder<Button>::instance(new Button{"Restart", "restart",
[](const char* msg) { [](const char* msg) {
if (String { "PRESS" }.equals(msg)) ESP.restart(); if (String { "PRESS" }.equals(msg)) ESP.restart();
} }
}).asDevice(gatewayDevice).build() }).asDevice(gatewayDevice).build(),
}; (new EasyHomeSwitch{"FritzBox", "easy_home_a", (unsigned long[4]) { 4483136, 4626800, 4661552, 4819632 }, (unsigned long[4]) { 4326544, 4537104, 4767520, 4972704 }, "Basement"})->withStateTopic(),
(new EasyHomeSwitch{"Outside", "easy_home_b", (unsigned long[4]) { 4483140, 4626804, 4661556, 4819636 }, (unsigned long[4]) { 4326548, 4537108, 4767524, 4972708 }, "Basement"})->withStateTopic(),
Ha::Switch* switches[] = {
(new PollinSwitch{"Meeting sensor", "00001", 1, "Dining room"})->withStateTopic(), (new PollinSwitch{"Meeting sensor", "00001", 1, "Dining room"})->withStateTopic(),
(new PollinSwitch{"Fire Tv", "00001", 2, "Living room"})->withStateTopic(), (new PollinSwitch{"Fire Tv", "00001", 2, "Living room"})->withStateTopic(),
(new PollinSwitch{"Diningroom player", "00001", 3, "Dining room"})->withStateTopic(), (new PollinSwitch{"Diningroom player", "00001", 3, "Dining room"})->withStateTopic(),
(new PollinSwitch{"Train", "11111", 4, "Playroom"})->withStateTopic(), (new PollinSwitch{"Train", "11111", 4, "Playroom"})->withStateTopic()
(new EasyHomeSwitch{"FritzBox", "easy_home_a", (unsigned long[4]) { 4483136, 4626800, 4661552, 4819632 }, (unsigned long[4]) { 4326544, 4537104, 4767520, 4972704 }, "Basement"})->withStateTopic(),
(new EasyHomeSwitch{"Outside", "easy_home_b", (unsigned long[4]) { 4483140, 4626804, 4661556, 4819636 }, (unsigned long[4]) { 4326548, 4537108, 4767524, 4972708 }, "Basement"})->withStateTopic()
}; };
Ha::Sensor* sensors[] = { Ha::Sensor* sensors[] = {
@ -81,18 +78,8 @@ namespace Mqtt {
char msg[len + 1]; char msg[len + 1];
memcpy(msg, payload, len); memcpy(msg, payload, len);
msg[len] = 0; msg[len] = 0;
for (Ha::Button* cmd : buttons) { Command* cmd = Command::mapCommands[string{ topic }];
if (String{ cmd->commandTopic }.equals(topic) && cmd->f != nullptr) { if (cmd) cmd->onCommand(msg);
cmd->f(msg);
return;
}
}
for (Ha::Switch* cmd : switches) {
if (String{ cmd->commandTopic }.equals(topic)) {
cmd->onCommand(msg);
return;
}
}
} }
void setup() { void setup() {