unify switches under same list and get rid of Builder
This commit is contained in:
parent
a9d66e29e3
commit
6b37d61b5c
@ -76,7 +76,9 @@ namespace Ha {
|
|||||||
char stateTopic[TOPIC_SIZE];
|
char stateTopic[TOPIC_SIZE];
|
||||||
const char* area;
|
const char* area;
|
||||||
publisherFunc publisher;
|
publisherFunc publisher;
|
||||||
|
|
||||||
virtual void onCommand(const char* msg){}
|
virtual void onCommand(const char* msg){}
|
||||||
|
virtual void addToMap(){}
|
||||||
|
|
||||||
Switch(const char* name, const char* id, publisherFunc publisher = nullptr)
|
Switch(const char* name, const char* id, publisherFunc publisher = nullptr)
|
||||||
: Command(name, id, type), publisher(publisher) {
|
: Command(name, id, type), publisher(publisher) {
|
||||||
@ -113,27 +115,6 @@ namespace Ha {
|
|||||||
void publishState(bool state) {
|
void publishState(bool state) {
|
||||||
publisher(stateTopic, state ? "ON" : "OFF");
|
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 {
|
struct PollinSwitch : Switch {
|
||||||
@ -156,6 +137,10 @@ namespace Ha {
|
|||||||
device["manufacturer"] = "Pollin";
|
device["manufacturer"] = "Pollin";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void addToMap() override {
|
||||||
|
p1Switches.insert({string(id), this});
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct EasyHomeSwitch : Switch {
|
struct EasyHomeSwitch : Switch {
|
||||||
@ -181,6 +166,13 @@ namespace Ha {
|
|||||||
device["manufacturer"] = "Intertek";
|
device["manufacturer"] = "Intertek";
|
||||||
device["model"] = "Easy Home";
|
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 {
|
struct Sensor : Component {
|
||||||
|
|||||||
@ -9,6 +9,14 @@ using namespace std;
|
|||||||
|
|
||||||
Scheduler ts;
|
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"
|
#include "wifi.h"
|
||||||
|
|
||||||
namespace Board {
|
namespace Board {
|
||||||
@ -50,7 +58,7 @@ namespace Board {
|
|||||||
case 1: {
|
case 1: {
|
||||||
// buildId returns a new pointer, should it be deleted, or string will take care of it?
|
// 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"]);
|
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) {
|
if (el != nullptr) {
|
||||||
el->publishState((bool)rcSwitch["state"]);
|
el->publishState((bool)rcSwitch["state"]);
|
||||||
}
|
}
|
||||||
@ -60,12 +68,12 @@ namespace Board {
|
|||||||
break;
|
break;
|
||||||
default: {
|
default: {
|
||||||
unsigned long value = rcSwitch["value"];
|
unsigned long value = rcSwitch["value"];
|
||||||
auto range = Mqtt::onSwitches.equal_range(value);
|
auto range = onSwitches.equal_range(value);
|
||||||
for_each(range.first, range.second, [](unordered_multimap<unsigned long, Ha::Switch*>::value_type& x){
|
for_each(range.first, range.second, [](mapswitches::value_type& x){
|
||||||
x.second->publishState(true);
|
x.second->publishState(true);
|
||||||
});
|
});
|
||||||
range = Mqtt::offSwitches.equal_range(value);
|
range = offSwitches.equal_range(value);
|
||||||
for_each(range.first, range.second, [](unordered_multimap<unsigned long, Ha::Switch*>::value_type& x){
|
for_each(range.first, range.second, [](mapswitches::value_type& x){
|
||||||
x.second->publishState(false);
|
x.second->publishState(false);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -51,20 +51,11 @@ namespace Mqtt {
|
|||||||
(new Ha::PollinSwitch{"Meeting sensor", "00001", 1, publish})->withArea("Dining room")->withStateTopic(),
|
(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{"Fire Tv", "00001", 2, publish})->withArea("Living room")->withStateTopic(),
|
||||||
(new Ha::PollinSwitch{"Diningroom player", "00001", 3, publish})->withArea("Dining 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) {
|
void publishComponentConfig(Ha::Component& component) {
|
||||||
StaticJsonDocument<JSON_SIZE> jsonDoc;
|
StaticJsonDocument<JSON_SIZE> jsonDoc;
|
||||||
component.buildConfig(jsonDoc);
|
component.buildConfig(jsonDoc);
|
||||||
@ -83,14 +74,7 @@ namespace Mqtt {
|
|||||||
publishComponentConfig(*cmp);
|
publishComponentConfig(*cmp);
|
||||||
}
|
}
|
||||||
for (Ha::Switch* cmp : switches) {
|
for (Ha::Switch* cmp : switches) {
|
||||||
mapSwitches.insert({string(cmp->id), cmp});
|
cmp->addToMap();
|
||||||
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});
|
|
||||||
}
|
|
||||||
publishComponentConfig(*cmp);
|
publishComponentConfig(*cmp);
|
||||||
}
|
}
|
||||||
ts.deleteTask(tPublishInit);
|
ts.deleteTask(tPublishInit);
|
||||||
@ -123,12 +107,6 @@ namespace Mqtt {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (Ha::Switch* cmd : otherSwitches) {
|
|
||||||
if (String{ cmd->commandTopic }.equals(topic)) {
|
|
||||||
cmd->onCommand(msg);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user