Go to file
Dustin 94a47d863c Send offline message on shutdown
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`.
2023-01-07 22:17:03 -06:00
src Send offline message on shutdown 2023-01-07 22:17:03 -06:00
.gitattributes Initial commit 2022-12-30 09:10:05 -06:00
.gitignore Add basic MQTT client functionality 2022-12-30 13:49:01 -06:00
Cargo.lock Add Home Assistant integration 2023-01-07 17:21:17 -06:00
Cargo.toml Add Home Assistant integration 2023-01-07 17:21:17 -06:00
rustfmt.toml Initial commit 2022-12-30 09:10:05 -06:00