use commandTopic as a temporary object

This commit is contained in:
Nicu Hodos 2025-10-10 23:26:58 +02:00
parent 962300712f
commit 149a722fc7

View File

@ -152,9 +152,10 @@ namespace Ha {
inline static unordered_map<string, Command*> mapCommandIds;
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 });
mapCommandIds.insert({ string(cmp->id), this });
delete commandTopic;
}
virtual void onCommand(const char* msg) {
@ -166,15 +167,22 @@ namespace Ha {
}
protected:
char commandTopic[TOPIC_SIZE] = {};
onMessage f;
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;
}
private:
Component* cmp;
char* buildTopic() {
char* commandTopic = new char[TOPIC_SIZE];
snprintf(commandTopic, TOPIC_SIZE, BASE_TOPIC"/set", cmp->id);
return commandTopic;
}
};
struct State : Config {