give maps more meaningful names and introduce StatefulCommand

This commit is contained in:
Nicu Hodos 2024-10-24 18:26:36 +02:00
parent 50e6c2586d
commit 7755950a27
2 changed files with 22 additions and 20 deletions

View File

@ -259,11 +259,11 @@ namespace Ha {
struct Command : Component {
bool retain = false;
static unordered_map<string, Command*> mapCommands;
static unordered_map<string, Command*> mapCommandTopics;
Command(const char* name, const char* id, const char* type, onMessage f) : Component(name, id, type), f(f) {
snprintf(commandTopic, sizeof(commandTopic), BASE_TOPIC"/set", type, MAIN_DEVICE_ID, id);
mapCommands.insert({ string(commandTopic), this });
mapCommandTopics.insert({ string(commandTopic), this });
}
virtual void onCommand(const char* msg) {
@ -292,7 +292,6 @@ namespace Ha {
};
struct StateConfig {
static unordered_map<string, Command*> mapStateTopics;
char stateTopic[TOPIC_SIZE];
StateConfig(Component* cmp) : cmp(cmp) {}
@ -312,38 +311,41 @@ namespace Ha {
}
};
struct Switch : Command, StateConfig {
struct StatefulCommand : Command, StateConfig {
static unordered_map<string, Command*> mapRestoreStateTopics;
Switch(const char* name, const char* id, onMessage f = nullptr) : Command(name, id, "switch", f), StateConfig(this) {}
StatefulCommand(const char* name, const char* id, const char* type, onMessage f)
: Command(name, id, "number", f), StateConfig(this) {}
void restoreFromState() {
mapRestoreStateTopics.insert({stateTopic, this});
}
};
struct Switch : StatefulCommand {
Switch(const char* name, const char* id, onMessage f = nullptr) : StatefulCommand(name, id, "switch", f) {}
void updateState(bool state) {
StateConfig::updateState(state ? "ON" : "OFF");
}
void restoreFromState() {
mapStateTopics.insert({stateTopic, this});
}
protected:
void buildCommandConfig(JsonDocument& jsonDoc) override {
StateConfig::buildConfig(jsonDoc);
}
};
struct Number : Command, StateConfig {
struct Number : StatefulCommand {
unsigned int min, max, step;
Number(const char* name, const char* id, unsigned int min, unsigned int max, unsigned int step, onMessage f)
: Command(name, id, "number", f), StateConfig(this), min(min), max(max), step(step) {}
: StatefulCommand(name, id, "number", f), min(min), max(max), step(step) {}
void updateState(unsigned int value) {
StateConfig::updateState(to_string(value).c_str());
}
void restoreFromState() {
mapStateTopics.insert({stateTopic, this});
}
protected:
void buildCommandConfig(JsonDocument& jsonDoc) override {
StateConfig::buildConfig(jsonDoc);
@ -433,7 +435,7 @@ namespace Ha {
List<Component> Component::components;
List<AbstractBuilder> AbstractBuilder::builders;
unordered_map<string, Command*> Command::mapCommands;
unordered_map<string, Command*> Command::mapCommandTopics;
unordered_map<string, Sensor*> Sensor::mapSensors;
unordered_map<string, Command*> StateConfig::mapStateTopics;
unordered_map<string, Command*> StatefulCommand::mapRestoreStateTopics;
}

View File

@ -47,13 +47,13 @@ namespace Mqtt {
memcpy(msg, payload, len);
msg[len] = 0;
auto strTopic = string{ topic };
auto cmd = Command::mapCommands[strTopic];
auto cmd = Command::mapCommandTopics[strTopic];
if (cmd) cmd->onCommand(msg);
cmd = StateConfig::mapStateTopics[strTopic];
cmd = StatefulCommand::mapRestoreStateTopics[strTopic];
if (cmd) {
cmd->onCommand(msg);
StateConfig::mapStateTopics.erase(strTopic);
StatefulCommand::mapRestoreStateTopics.erase(strTopic);
}
}