Skip to content

Commit

Permalink
fix(mqtt): Fix mqtt getting stuck forever requiring a restart
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypfer committed Dec 29, 2021
1 parent 89667ef commit 42a5613
Showing 1 changed file with 9 additions and 36 deletions.
45 changes: 9 additions & 36 deletions backend/lib/mqtt/MqttController.js
Expand Up @@ -81,38 +81,7 @@ class MqttController {

this.config.onUpdate(async (key) => {
if (key === "mqtt") {
if (this.currentConfig.enabled) {
const newConfig = this.config.get("mqtt");

const deconfOptions = {
cleanHomie: newConfig.enabled ? true : newConfig.interfaces.homie.cleanAttributesOnShutdown,
cleanHass: newConfig.enabled ? true : newConfig.interfaces.homeassistant.cleanAutoconfOnShutdown,
unsubscribe: !!newConfig.enabled
};

await this.reconfigure(async () => {
// If we're shutting down indefinitely, respect user settings
// If we're just reconfiguring, take everything down with state == init so consumers know what we're up to
await this.robotHandle.deconfigure(deconfOptions);

if (this.currentConfig.interfaces.homeassistant.enabled) {
await this.hassController.deconfigure(deconfOptions);
}
}, {
targetState: newConfig.enabled ? HomieCommonAttributes.STATE.INIT : HomieCommonAttributes.STATE.DISCONNECTED
});

if (!newConfig.enabled) {
try {
await this.setState(HomieCommonAttributes.STATE.LOST);
await this.setState(HomieCommonAttributes.STATE.DISCONNECTED);
} catch (e) {
Logger.warn("Failed to set MQTT state", e);
}
}

await this.disconnect();
}
await this.shutdown();

this.loadConfig();

Expand Down Expand Up @@ -312,6 +281,7 @@ class MqttController {
Logger.info("Connected to non standard compliant MQTT Broker.");
} else {
Logger.error("MQTT error:", e.toString());

if (this.isInitialized()) {
(async () => {
// Do not use .reconfigure() since it will try to publish to MQTT
Expand Down Expand Up @@ -413,6 +383,10 @@ class MqttController {
if (!this.client) {
return;
}

//disable automatic reconnects
this.client.options.reconnectPeriod = 0;

this.stopAutorefreshService();

await this.reconfigure(async () => {
Expand Down Expand Up @@ -569,20 +543,19 @@ class MqttController {
* @return {Promise<void>}
*/
async unsubscribe(handle) {
const topics = handle.getInterestingTopics();
if (Object.keys(topics).length === 0) {
if (Object.keys(this.subscriptions).length === 0) {
return;
}

try {
await this.asyncClient.unsubscribe(Object.keys(topics));
await this.asyncClient.unsubscribe(Object.keys(this.subscriptions));
} catch (e) {
if (e.message !== "client disconnecting" && e.message !== "Connection closed") {
throw e;
}
}

for (const topic of Object.keys(topics)) {
for (const topic of Object.keys(this.subscriptions)) {
delete this.subscriptions[topic];
}
}
Expand Down

0 comments on commit 42a5613

Please sign in to comment.