unify switches under same list and get rid of Builder

This commit is contained in:
Nicu Hodos 2024-04-30 23:08:22 +02:00
parent a9d66e29e3
commit 6b37d61b5c
3 changed files with 30 additions and 52 deletions

View File

@ -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 <class T>
struct Builder {
T* t;
Builder(T* t) : t(t) {}
T* build() {
return t;
}
Builder<T>* withArea(const char* area) {
t->withArea(area);
return this;
}
Builder<T>* 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 {

View File

@ -9,6 +9,14 @@ using namespace std;
Scheduler ts;
namespace Ha {
struct Switch;
}
typedef unordered_multimap<unsigned long, Ha::Switch*> mapswitches;
mapswitches onSwitches;
mapswitches offSwitches;
unordered_map<string, Ha::Switch*> 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<unsigned long, Ha::Switch*>::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<unsigned long, Ha::Switch*>::value_type& x){
range = offSwitches.equal_range(value);
for_each(range.first, range.second, [](mapswitches::value_type& x){
x.second->publishState(false);
});
}

View File

@ -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<Ha::EasyHomeSwitch>{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<Ha::EasyHomeSwitch>{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<string, Ha::Switch*> mapSwitches;
unordered_multimap<unsigned long, Ha::Switch*> onSwitches;
unordered_multimap<unsigned long, Ha::Switch*> offSwitches;
void publishComponentConfig(Ha::Component& component) {
StaticJsonDocument<JSON_SIZE> 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() {