Jalle19 / eda-modbus-bridge

An HTTP/MQTT bridge for Enervent EDA ventilation units
GNU General Public License v3.0
15 stars 3 forks source link

Try to reconnect to MQTT broker if disconnected #36

Closed Jalle19 closed 2 years ago

Jalle19 commented 2 years ago

Woke up and the MQTT part didn't work at all. Unfortunately I couldn't get at the logs since I have HTTP request spamming the log, and Home Assistant doesn't seem to store (or at least expose) logs that far back.

tomrosenback commented 2 years ago

This should be handled by the MQTT library, check https://github.com/mqttjs/MQTT.js#about-reconnection

Jalle19 commented 2 years ago

It sounds like that section is only relevant when using the library from a browser context (in which case WebSockets are involved). I'll have to do some testing.

tomrosenback commented 2 years ago
mqttClient.on('connect', async () => {
    console.log("MQTT broker connected")
})

mqttClient.on('close', async () => {
    console.log('MQTT broker closed connection')

    setTimeout(async () => {
        console.log('Reconnect to MQTT broker')
        client.reconnect()
    }, 5000)
})

Made some quick and dirty tests locally, above at least works when the broker dissappears after the first successful connectAsync. Does not handle the failed connection in the catch part.

Jalle19 commented 2 years ago

That probably on tries to reconnect once?

Jalle19 commented 2 years ago

https://github.com/mqttjs/MQTT.js/pull/101#issuecomment-49051189 maybe this is the actual issue?

tomrosenback commented 2 years ago

That probably on tries to reconnect once?

Could you please elaborate? mqttClient.on('close'... happens everytime a reconnect fail.

tomrosenback commented 2 years ago

mqttjs/MQTT.js#101 (comment) maybe this is the actual issue?

You are refering to the comment about the fact that after the client has reconnected it don´t subscribe to the topics previously subscribed? I thought you had problems also with the publishing?

Also it seems the MQTT really does default try to reconnect every second. https://github.com/mqttjs/MQTT.js/blob/7ec4b8fd602e220f50693cb83f082dab764ed3f2/lib/client.js#L29

Jalle19 commented 2 years ago

Yeah, maybe I had some other issue, it's hard to tell since I never could get the complete log :/

tomrosenback commented 2 years ago

Noticed after updating to 1.1.0 of the HA addon https://github.com/Jalle19/home-assistant-addon-repository which uses 2.1.0 of eda-modbus-bridge the MQTT connection failed on startup of the addon causing MQTT connection remaining disconnected until manual restart of addon. Restarting the addon manually helped in this case.