diff --git a/gateway/include/ha.h b/gateway/include/ha.h index 2a7fbda..41d7694 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -114,6 +114,26 @@ namespace Ha { publisher(stateTopic, state ? "ON" : "OFF"); } + template + struct Builder { + T* t; + + Builder(T* t) : t(t) {} + + T* build() { + return t; + } + + Builder* withArea(const char* area) { + t->withArea(area); + return this; + } + + Builder* withStateTopic() { + t->withStateTopic(); + return this; + } + }; }; struct PollinSwitch : Switch { @@ -139,11 +159,13 @@ namespace Ha { }; struct EasyHomeSwitch : Switch { - unsigned long *on; - unsigned long *off; + unsigned long on[8] = {4972714, 4767530, 4537114, 4326554}; + unsigned long off[8] = {4483146, 4626810, 4819642, 4661562}; EasyHomeSwitch(const char* name, const char* id, unsigned long on[4], unsigned long off[4], publisherFunc publisher) - : Switch(name, id, publisher), on(on), off(off) { + : Switch(name, id, publisher) { + memccpy(&this->on[4], on, 4, sizeof(unsigned long)); + memccpy(&this->off[4], off, 4, sizeof(unsigned long)); sprintf(commandTopic, "homeassistant/%s/rc-gateway/%s/set", type, id); } diff --git a/gateway/include/huzzah.h b/gateway/include/huzzah.h index 6cba31f..29ee4e3 100644 --- a/gateway/include/huzzah.h +++ b/gateway/include/huzzah.h @@ -60,15 +60,14 @@ namespace Board { break; default: { unsigned long value = rcSwitch["value"]; - Ha::Switch* aSwitch = Mqtt::onSwitches[value]; - if (aSwitch != nullptr) { - aSwitch->publishState(true); - } else { - aSwitch = Mqtt::offSwitches[value]; - if (aSwitch != nullptr) { - aSwitch->publishState(false); - } - } + auto range = Mqtt::onSwitches.equal_range(value); + for_each(range.first, range.second, [](unordered_multimap::value_type& x){ + x.second->publishState(true); + }); + range = Mqtt::offSwitches.equal_range(value); + for_each(range.first, range.second, [](unordered_multimap::value_type& x){ + x.second->publishState(false); + }); } } } diff --git a/gateway/include/mqtt.h b/gateway/include/mqtt.h index 4ce061d..4731fed 100644 --- a/gateway/include/mqtt.h +++ b/gateway/include/mqtt.h @@ -54,15 +54,16 @@ namespace Mqtt { (new Ha::PollinSwitch{"Train", "11111", 4, publish})->withArea("Playroom")->withStateTopic() }; - Ha::EasyHomeSwitch* fritz = new Ha::EasyHomeSwitch{"FritzBox", "easy_home_a", (unsigned long[4]){4483136, 4626800, 4819632, 4661552}, (unsigned long[4]){4767520, 4537104, 4326544, 4972704}, publish}; - Ha::EasyHomeSwitch* otherSwitches[] = { - fritz + (Ha::Switch::Builder{new Ha::EasyHomeSwitch{"FritzBox", "easy_home_a", (unsigned long[4]) { 4483136, 4626800, 4819632, 4661552 }, (unsigned long[4]) { 4767520, 4537104, 4326544, 4972704 }, publish}}) + .withArea("Basement")->withStateTopic()->build(), + (Ha::Switch::Builder{new Ha::EasyHomeSwitch{"Outside", "easy_home_b", (unsigned long[4]) { 4661556, 4819636, 4626804, 4483140 }, (unsigned long[4]) { 4326548, 4537108, 4767524, 4972708 }, publish}}) + .withArea("Basement")->withStateTopic()->build() }; unordered_map mapSwitches; - unordered_map onSwitches; - unordered_map offSwitches; + unordered_multimap onSwitches; + unordered_map offSwitches; void publishComponentConfig(Ha::Component& component) { StaticJsonDocument jsonDoc; @@ -78,8 +79,6 @@ namespace Mqtt { // for (Ha::Component* cmp : sensors) { // publishComponentConfig(*cmp); // } - fritz->withArea("Basement"); - fritz->withStateTopic(); for (Ha::Component* cmp : buttons) { publishComponentConfig(*cmp); }