Skip to content

Commit

Permalink
v1.2.42
Browse files Browse the repository at this point in the history
- Fixed bug in *Modules.ParameterSet* when setting *ConfigureOption.* fields (not saving MQTT client settings)
- Fixed memory leak in *Modules.ParameterGet* API method
- Fixed memory leak in MQTT client lifecycle
  • Loading branch information
genemars committed Dec 9, 2024
1 parent 429f1e9 commit d4522fe
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/data/JsonStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace Data {

} else {

// TODO: report error / disable scheduler
// TODO: report error

}
}
Expand All @@ -81,7 +81,7 @@ namespace Data {

} else {

// TODO: report error / disable scheduler
// TODO: report error

}
}
Expand Down
44 changes: 27 additions & 17 deletions src/net/MQTTClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,15 @@ namespace Net {
static MQTTRequestHandler* requestHandler;

MQTTClient() {
setLoopInterval(5000);
setLoopInterval(2000);
};

void loop() override {

if (isEnabled && !clientStarted) {
if (stopRequested) {
stop();
stopRequested = false;
} else if (isEnabled && !clientStarted) {
start();
}

Expand All @@ -98,8 +101,8 @@ namespace Net {
auto port = String();
auto tls = String();
auto webSockets = String();
auto username = String();
auto password = String();
username = "";
password = "";
for (ModuleParameter* p: parameters) {
if(p->name.equals("ConfigureOptions.ServerAddress")) {
address = String(p->value);
Expand All @@ -116,28 +119,26 @@ namespace Net {
}
}

auto brokerUrl = new String();
if (tls.equals("on")) {
*brokerUrl = webSockets.equals("on") ? "wss://" : "mqtts://";
brokerUrl = webSockets.equals("on") ? "wss://" : "mqtts://";
} else {
*brokerUrl = webSockets.equals("on") ? "ws://" : "mqtt://";
brokerUrl = webSockets.equals("on") ? "ws://" : "mqtt://";
}
*brokerUrl += address + String(":") + port;
brokerUrl += address + String(":") + port;

stop();

mqtt_cfg.uri = brokerUrl->c_str();
mqtt_cfg.uri = brokerUrl.c_str();
mqtt_cfg.username = username.c_str();
mqtt_cfg.password = password.c_str();

stopRequested = true;
}

void enable() {
isEnabled = true;
}
void disable() {
isEnabled = false;
stop();
stopRequested = true;
}

void start() {
Expand All @@ -150,12 +151,16 @@ namespace Net {
arduino_esp_crt_bundle_attach(&conf);
*/

esp_mqtt_client_destroy(client);
client = esp_mqtt_client_init(&mqtt_cfg);
/* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */
esp_mqtt_client_register_event(client, static_cast<esp_mqtt_event_id_t>(ESP_EVENT_ANY_ID), mqtt_event_handler, nullptr);
if (client != nullptr) {
/* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */
esp_mqtt_client_register_event(client, static_cast<esp_mqtt_event_id_t>(ESP_EVENT_ANY_ID),
mqtt_event_handler, nullptr);

if (esp_mqtt_client_start(client) == ESP_OK) {
clientStarted = true;
if (esp_mqtt_client_start(client) == ESP_OK) {
clientStarted = true;
}
}
}

Expand All @@ -164,6 +169,7 @@ namespace Net {
void stop() {

if (clientStarted) {
esp_mqtt_client_disconnect(client);
esp_mqtt_client_stop(client);
clientStarted = false;
}
Expand All @@ -175,8 +181,12 @@ namespace Net {
}

private:
bool clientStarted = false;
String brokerUrl;
String username;
String password;
bool isEnabled = false;
bool stopRequested = false;
bool clientStarted = false;
esp_mqtt_client_handle_t client = nullptr;
esp_mqtt_client_config_t mqtt_cfg { .uri = "" };

Expand Down
19 changes: 12 additions & 7 deletions src/service/api/HomeGenieHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ namespace Service { namespace API {
*/
auto jsonParameter = HomeGenie::createModuleParameter(parameter->name.c_str(), parameter->value.c_str(), parameter->updateTime.c_str());
responseCallback->writeAll(jsonParameter);
free((void*)jsonParameter);
return true;
}
}
Expand All @@ -343,8 +344,13 @@ namespace Service { namespace API {
} else {
auto propName = request->getOption(2);
auto propValue = WebServer::urlDecode(request->getOption(3));
auto p = ModuleParameter(propName, propValue);
parameters.add(p);
if (!propName.isEmpty()) {
auto p = ModuleParameter(propName, propValue);
parameters.add(p);
}
}
if (parameters.size() == 0) {
return false;
}
// Update module parameters
if (module != nullptr) {
Expand All @@ -358,18 +364,17 @@ namespace Service { namespace API {
m.value = p.value;
homeGenie->getEventRouter().signalEvent(m);
}
responseCallback->writeAll(ApiHandlerResponseText::OK);
return true;
if (!isProgramConfiguration) {
responseCallback->writeAll(ApiHandlerResponseText::OK);
return true;
}
}
// Update program configuration
if (isProgramConfiguration) {
// TODO: move this to a "ProgramManager" class (extend actual ProgramStore)
#ifndef DISABLE_MQTT_CLIENT
if (address.equals(MQTT_NETWORK_CONFIGURATION)) {
auto mqttNetwork = homeGenie->programs.getItem(MQTT_NETWORK_CONFIGURATION);
for (const ModuleParameter& p: parameters) {
mqttNetwork->setProperty(p.name, p.value);
}
homeGenie->programs.save();
homeGenie->getNetManager().getMQTTClient().configure(mqttNetwork->properties);
}
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

#define VERSION_MAJOR 1
#define VERSION_MINOR 2
#define VERSION_PATCH 41
#define VERSION_PATCH 42

#define STRING_VALUE(...) STRING_VALUE__(__VA_ARGS__)
#define STRING_VALUE__(...) #__VA_ARGS__
Expand Down

0 comments on commit d4522fe

Please sign in to comment.