94a47d863ca7848841d23dc2cac5d652ab87a897
The MQTT broker does *not* send the client's last will and testament message when the client disconnects gracefully. I thought it did because my other Rust/MQTT project, MQTTDPMS, seems to behave that way. It turns out, though, that in that project, the client never actually disconnects gracefully. It has no signal handlers, so when it receives SIGINT or SIGTERM, it just exits immediately. This leaves the OS to forcefully close the TCP connection, so the broker sends the will message. Since the Browser HUD process *does* have signal handlers, when it receives a signal, it shuts down gracefully. As Rust drops objects during shut down, the MQTT client eventually disconnects cleanly, so the broker does not send the will message. In order to notify Home Assistant that the device is now unavailable, we have to explicitly send the offline message before disconnecting the MQTT client. I've added a `Notify` object that lives for the entire life of the process and is passed in to the session. When a signal is received, this object wakes up the asynchronous tasks that perform the pre-shutdown operations. One such task is spawned by the `MqttClient::run` method; it sends the offline message when notified, then disconnects the MQTT client. In order to share the MQTT client object between this new task and the message receive loop, it has to be wrapped in an `Arc` and a `Mutex`.
Description
No description provided
Languages
Rust
99.9%
Shell
0.1%