Commit Graph

6 Commits (e92d8c9cef09c674f839496c70d3d79b3d2000ed)

Author SHA1 Message Date
Dustin e92d8c9cef mqtt: Break out of receive loop on disconnect
Apparently, the `AsyncReceiver` stream produces nested `Option` objects.
The outer is `None` if "the stream is exhausted," which is somehow
different than the connection being closed; the inner `Option` is `None`
in that case.

We were originally ignoring the inner `None`, but just causes the
async task to go into a busy loop when connection is closed.  We need to
break out of the loop there instead.
2023-01-07 22:22:49 -06:00
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
Dustin d4f2c73eca Add Home Assistant integration
Home Assistant integration is done via [MQTT Discovery][0].  The
application publishes configuration to a special topic, which Home
Assistant receives and uses to create entities and optionally assign
them to devices.

To start with, we're exposing two entites to Home Assistant for each
attached monitor: one for the current URL and one for the current title
of the window.  The URL is exposed as a "text" sensor, which allows the
state to be changed directly; when the state changes, the new value is
puoblished to the "command" topic and thus triggering a navigation.

Since the client can only have a single "will" message, every entity
will be marked as available/unavailable together.  This is probably not
an issue, but it does make it impossible to indicate a monitor is no
longer attached.

Note: for some reason, the will message doesn't seem to get sent when the
client disconnects.  I am not sure why...

[0]: https://www.home-assistant.io/docs/mqtt/discovery/
2023-01-07 17:21:17 -06:00
Dustin 4820d0f6cd Begin MQTT control implementation
The pieces are starting to come together.  To control the browser via
MQTT messages, the `MqttClient` dispatches messages via a
`MessageHandler`, which parses them and makes the appropriate Marionette
requests.  The `MessageHandler` trait defines callback methods for each
MQTT control operation, which currently is just `navigate`.  The
operation type is determined by the MQTT topic on which the message was
received.

Several new types are necessary to make this work.  The `MessageHandler`
trait and implementation are of course the core, reacting to incoming
MQTT messages.  In order for the handler to be able to *send* MQTT
messages, though, it needs a reference to the Paho MQTT client.  The
`MqttPublisher` provides a convenient wrapper around the client, with
specific methods for each type of message to send.  Finally, there's the
`MessageType` enumeration, which works in conjunction with the
`TopicMatcher` to match topic names to message types using topic filter
patterns.
2022-12-30 19:06:27 -06:00
Dustin 41c87d87af mqtt: Add MqttClient::connect method
Separating the `connect` call out of the `MqttClient::new` function
makes is such that we do not have to create a new object for each
iteration of the initial connection loop.  Instead, we just create one
object and repeatedly call its `connect` method until it succeeds
2022-12-30 14:39:10 -06:00
Dustin ee8ed0c644 Add basic MQTT client functionality
Naturally, we need a way to configure the MQTT connection parameters
(host, port, username, etc.).  For that, we'll use a TOML configuration
file, which is read at startup and deserialized into a structure owned
by the Session.

The Session object now has a `run` method, which establishes the MQTT
connection and then repeatedly waits for messages from the broker.  It
will continuously attempt to connect to the broker until it succeeds.
This way, if the broker is unavailable when the application starts, it
will eventually connect when it becomes available.  Once the initial
connection is established, the client will automatically reconnect if it
gets disconnected later.

Since the `run` method loops forever and never returns, we need to use a
separate Tokio task to manage it.  We keep the task handle so we can
cancel the task when the application shuts down.
2022-12-30 13:49:01 -06:00