use commandTopic as a temporary object
This commit is contained in:
parent
962300712f
commit
149a722fc7
14
src/ha.h
14
src/ha.h
@ -152,9 +152,10 @@ namespace Ha {
|
|||||||
inline static unordered_map<string, Command*> mapCommandIds;
|
inline static unordered_map<string, Command*> mapCommandIds;
|
||||||
|
|
||||||
Command(Component* cmp, onMessage f) : f(f), cmp(cmp) {
|
Command(Component* cmp, onMessage f) : f(f), cmp(cmp) {
|
||||||
snprintf(commandTopic, sizeof(commandTopic), BASE_TOPIC"/set", cmp->id);
|
auto commandTopic = buildTopic();
|
||||||
mapCommandTopics.insert({ string(commandTopic), this });
|
mapCommandTopics.insert({ string(commandTopic), this });
|
||||||
mapCommandIds.insert({ string(cmp->id), this });
|
mapCommandIds.insert({ string(cmp->id), this });
|
||||||
|
delete commandTopic;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void onCommand(const char* msg) {
|
virtual void onCommand(const char* msg) {
|
||||||
@ -166,15 +167,22 @@ namespace Ha {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
char commandTopic[TOPIC_SIZE] = {};
|
|
||||||
onMessage f;
|
onMessage f;
|
||||||
|
|
||||||
void buildConfig(JsonDocument& jsonDoc) override {
|
void buildConfig(JsonDocument& jsonDoc) override {
|
||||||
jsonDoc["command_topic"] = (const char*)commandTopic;
|
auto commandTopic = buildTopic();
|
||||||
|
jsonDoc["command_topic"] = commandTopic;
|
||||||
|
delete commandTopic;
|
||||||
if (retain) jsonDoc["retain"] = true;
|
if (retain) jsonDoc["retain"] = true;
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
Component* cmp;
|
Component* cmp;
|
||||||
|
|
||||||
|
char* buildTopic() {
|
||||||
|
char* commandTopic = new char[TOPIC_SIZE];
|
||||||
|
snprintf(commandTopic, TOPIC_SIZE, BASE_TOPIC"/set", cmp->id);
|
||||||
|
return commandTopic;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct State : Config {
|
struct State : Config {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user