From 6b37d61b5cf655b295110990e7c76f3b2a893d7a Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Tue, 30 Apr 2024 23:08:22 +0200 Subject: [PATCH] unify switches under same list and get rid of Builder --- gateway/include/ha.h | 34 +++++++++++++--------------------- gateway/include/huzzah.h | 18 +++++++++++++----- gateway/include/mqtt.h | 30 ++++-------------------------- 3 files changed, 30 insertions(+), 52 deletions(-) diff --git a/gateway/include/ha.h b/gateway/include/ha.h index 2079534..62fb34d 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -76,7 +76,9 @@ namespace Ha { 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) { @@ -113,27 +115,6 @@ namespace Ha { void publishState(bool state) { 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 { @@ -156,6 +137,10 @@ namespace Ha { device["manufacturer"] = "Pollin"; } + void addToMap() override { + p1Switches.insert({string(id), this}); + } + }; struct EasyHomeSwitch : Switch { @@ -181,6 +166,13 @@ namespace Ha { device["manufacturer"] = "Intertek"; device["model"] = "Easy Home"; } + + void addToMap() override { + for (int i = 0; i < 8; i++) { + onSwitches.insert({on[i], this}); + offSwitches.insert({off[i], this}); + } + } }; struct Sensor : Component { diff --git a/gateway/include/huzzah.h b/gateway/include/huzzah.h index 29ee4e3..8fbfd02 100644 --- a/gateway/include/huzzah.h +++ b/gateway/include/huzzah.h @@ -9,6 +9,14 @@ using namespace std; Scheduler ts; +namespace Ha { + struct Switch; +} +typedef unordered_multimap mapswitches; +mapswitches onSwitches; +mapswitches offSwitches; +unordered_map p1Switches; + #include "wifi.h" namespace Board { @@ -50,7 +58,7 @@ namespace Board { case 1: { // buildId returns a new pointer, should it be deleted, or string will take care of it? string id = Protocol_1::buildId((const char*)rcSwitch["group"], (int)rcSwitch["channel"]); - Ha::Switch* el = Mqtt::mapSwitches[id]; + Ha::Switch* el = p1Switches[id]; if (el != nullptr) { el->publishState((bool)rcSwitch["state"]); } @@ -60,12 +68,12 @@ namespace Board { break; default: { unsigned long value = rcSwitch["value"]; - auto range = Mqtt::onSwitches.equal_range(value); - for_each(range.first, range.second, [](unordered_multimap::value_type& x){ + auto range = onSwitches.equal_range(value); + for_each(range.first, range.second, [](mapswitches::value_type& x){ x.second->publishState(true); }); - range = Mqtt::offSwitches.equal_range(value); - for_each(range.first, range.second, [](unordered_multimap::value_type& x){ + range = offSwitches.equal_range(value); + for_each(range.first, range.second, [](mapswitches::value_type& x){ x.second->publishState(false); }); } diff --git a/gateway/include/mqtt.h b/gateway/include/mqtt.h index a70ef9c..ccb4d84 100644 --- a/gateway/include/mqtt.h +++ b/gateway/include/mqtt.h @@ -51,20 +51,11 @@ namespace Mqtt { (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::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() }; - Ha::EasyHomeSwitch* otherSwitches[] = { - (Ha::Switch::Builder{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()->build(), - (Ha::Switch::Builder{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()->build() - }; - - unordered_map mapSwitches; - unordered_multimap onSwitches; - unordered_multimap offSwitches; - void publishComponentConfig(Ha::Component& component) { StaticJsonDocument jsonDoc; component.buildConfig(jsonDoc); @@ -83,14 +74,7 @@ namespace Mqtt { publishComponentConfig(*cmp); } for (Ha::Switch* cmp : switches) { - mapSwitches.insert({string(cmp->id), cmp}); - publishComponentConfig(*cmp); - } - for (Ha::EasyHomeSwitch* cmp : otherSwitches) { - for (int i = 0; i < 8; i++) { - onSwitches.insert({cmp->on[i], cmp}); - offSwitches.insert({cmp->off[i], cmp}); - } + cmp->addToMap(); publishComponentConfig(*cmp); } ts.deleteTask(tPublishInit); @@ -123,12 +107,6 @@ namespace Mqtt { return; } } - for (Ha::Switch* cmd : otherSwitches) { - if (String{ cmd->commandTopic }.equals(topic)) { - cmd->onCommand(msg); - return; - } - } } void setup() {