return string when building protocol ids - avoid unused pointers
This commit is contained in:
parent
b5e13c9ba2
commit
77594581ff
@ -27,10 +27,9 @@ public:
|
|||||||
rcSwitch["raw_value"] = value;
|
rcSwitch["raw_value"] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char* buildId(const char* group, const unsigned char channel) {
|
static std::string buildId(const char* group, const unsigned char channel) {
|
||||||
char* uId = new char[30];
|
char uId[30];
|
||||||
sprintf(uId, "%s_%d", group, channel);
|
sprintf(uId, "%s_%d", group, channel);
|
||||||
return uId;
|
return std::string{ uId };
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -33,16 +33,4 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static char* buildId(const char* id) {
|
|
||||||
char* uId = new char[30];
|
|
||||||
sprintf(uId, "%s", id);
|
|
||||||
return uId;
|
|
||||||
}
|
|
||||||
|
|
||||||
static char* buildId(unsigned int id) {
|
|
||||||
char* uId = new char[30];
|
|
||||||
sprintf(uId, "%d", id);
|
|
||||||
return uId;
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
};
|
||||||
@ -55,7 +55,13 @@ struct PollinSwitch : Switch {
|
|||||||
unsigned char channel;
|
unsigned char channel;
|
||||||
|
|
||||||
PollinSwitch(const char* name, const char* group, const unsigned char channel, const char* area = nullptr)
|
PollinSwitch(const char* name, const char* group, const unsigned char channel, const char* area = nullptr)
|
||||||
: Switch(name, Protocol_1::buildId(group, channel)), group(group), channel(channel) {
|
: Switch(name, [group, channel]{
|
||||||
|
// copy id from string into a new pointer, to avoid memory leaks
|
||||||
|
string s = Protocol_1::buildId(group, channel);
|
||||||
|
char* uId = new char[s.length() + 1];
|
||||||
|
strcpy(uId, s.c_str());
|
||||||
|
return uId;
|
||||||
|
}()), group(group), channel(channel) {
|
||||||
mainDevice = (new DeviceConfig{id})->withName(name)->withManufacturer("Pollin")->withArea(area)->withParent(gatewayDevice);
|
mainDevice = (new DeviceConfig{id})->withName(name)->withManufacturer("Pollin")->withArea(area)->withParent(gatewayDevice);
|
||||||
deviceClass = "outlet";
|
deviceClass = "outlet";
|
||||||
p1Switches.insert({ string(id), this });
|
p1Switches.insert({ string(id), this });
|
||||||
|
|||||||
@ -233,11 +233,11 @@ namespace Ha {
|
|||||||
const char* unitMeasure = nullptr;
|
const char* unitMeasure = nullptr;
|
||||||
const char* valueTemplate = nullptr;
|
const char* valueTemplate = nullptr;
|
||||||
|
|
||||||
Sensor() : Component(name, Protocol_2::buildId(id), "sensor") {
|
Sensor() : Component(name, id, "sensor") {
|
||||||
withStateTopic();
|
withStateTopic();
|
||||||
}
|
}
|
||||||
|
|
||||||
Sensor(const char* name, const char* id) : Component(name, Protocol_2::buildId(id), "sensor") {
|
Sensor(const char* name, const char* id) : Component(name, id, "sensor") {
|
||||||
withStateTopic();
|
withStateTopic();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -56,7 +56,6 @@ namespace Board {
|
|||||||
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
|
JsonObjectConst rcSwitch = jsonDoc["rcSwitch"];
|
||||||
switch ((unsigned int)rcSwitch["protocol"]) {
|
switch ((unsigned int)rcSwitch["protocol"]) {
|
||||||
case 1: {
|
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"]);
|
string id = Protocol_1::buildId((const char*)rcSwitch["group"], (int)rcSwitch["channel"]);
|
||||||
Ha::Switch* el = p1Switches[id];
|
Ha::Switch* el = p1Switches[id];
|
||||||
if (el != nullptr) {
|
if (el != nullptr) {
|
||||||
@ -81,7 +80,7 @@ namespace Board {
|
|||||||
}
|
}
|
||||||
if (jsonDoc.containsKey("sensor")) {
|
if (jsonDoc.containsKey("sensor")) {
|
||||||
JsonObjectConst json = jsonDoc["sensor"];
|
JsonObjectConst json = jsonDoc["sensor"];
|
||||||
string id = Protocol_2::buildId((unsigned int)json["id"]);
|
string id = to_string((unsigned int)json["id"]);
|
||||||
char stateTopic[TOPIC_SIZE];
|
char stateTopic[TOPIC_SIZE];
|
||||||
sprintf(stateTopic, "homeassistant/sensor/%s/%s/state", MAIN_DEVICE_ID, id.c_str());
|
sprintf(stateTopic, "homeassistant/sensor/%s/%s/state", MAIN_DEVICE_ID, id.c_str());
|
||||||
Mqtt::publish(stateTopic, message);
|
Mqtt::publish(stateTopic, message);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user