docs: add per-subsystem README files
Add inline documentation for all src/ subsystems covering purpose, responsibilities, public API, and usage notes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
# MQTTTransport
|
||||
|
||||
## Summary
|
||||
|
||||
`MQTTTransport` wraps `MQTTDriver` and connects it to the `CommandBus`. It subscribes to the device control topic, parses incoming JSON messages into `CommandEnvelope` objects, and dispatches them. Command replies are published back to the device data topic. Unsolicited events (telemetry, bell strikes) reach clients via `broadcast()`, which also publishes to the data topic. The underlying `MQTTDriver` manages the broker connection lifecycle, reconnect backoff, and heartbeat independently; `MQTTTransport` only owns the message-routing glue.
|
||||
|
||||
---
|
||||
|
||||
## Public API
|
||||
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `MQTTTransport(MQTTDriver& mqtt, CommandBus& bus)` | Constructor. Does not start the connection. |
|
||||
| `void begin()` | Wires the message callback onto `MQTTDriver`, then calls `MQTTDriver::begin()` to configure the AsyncMqttClient. Does not connect to the broker — connection is triggered by `onNetworkConnected()`. |
|
||||
| `void loop()` | No-op. AsyncMqttClient is fully event-driven. |
|
||||
| `bool isHealthy() const` | Returns `MQTTDriver::isConnected()`. |
|
||||
| `String name() const` | Returns `"MQTT"`. |
|
||||
| `void broadcast(const String& msg)` | Publishes `msg` to the device data topic (`vesper/{uid}/data`). |
|
||||
| `void onNetworkConnected()` | Forwards to `MQTTDriver::onNetworkConnected()`, which schedules the broker connection after a 2-second stabilisation delay. |
|
||||
| `void onNetworkDisconnected()` | Forwards to `MQTTDriver::onNetworkDisconnected()`. |
|
||||
|
||||
---
|
||||
|
||||
## Initialization
|
||||
|
||||
`CommunicationRouter::begin()` calls `_mqttTransport.begin()`, then adds the transport to the bus with `_bus.addTransport(&_mqttTransport)`. Network events from `Networking` arrive via `CommunicationRouter::onNetworkConnected()` / `onNetworkDisconnected()`, which forward to this transport.
|
||||
|
||||
---
|
||||
|
||||
## Message Format
|
||||
|
||||
Incoming control messages must be JSON with at least a `"cmd"` field:
|
||||
|
||||
```json
|
||||
{ "v": 2, "cmd": "relay.set_durations", "contents": { "bells": { "0": 500 } } }
|
||||
```
|
||||
|
||||
`"v"` defaults to `2` if omitted. `"v": 1` triggers legacy translation before dispatch.
|
||||
|
||||
Responses are published to the data topic:
|
||||
```json
|
||||
{ "status": "SUCCESS", "type": "relay.set_durations", "message": "Bell durations updated and saved" }
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Dependencies
|
||||
|
||||
- `MQTTDriver` — broker connection, publish/subscribe, reconnect, heartbeat
|
||||
- `CommandBus` — dispatch and broadcast
|
||||
- `ArduinoJson`, `Logging`
|
||||
|
||||
---
|
||||
|
||||
## Gotchas / Notes
|
||||
|
||||
- **Both reply and broadcast publish to the same data topic.** The broker does not distinguish between command replies and unsolicited events — clients must inspect `"type"` and `"status"` to differentiate.
|
||||
- **Broker connection is deferred.** `begin()` does not connect. The connection is triggered by `onNetworkConnected()`, which starts a 2-second non-blocking stabilisation timer in `MQTTDriver`.
|
||||
- **MQTT must be enabled in config.** `MQTTDriver::connect()` is a no-op if `mqttConfig.enabled` is false. Toggle with `mqtt.enable` / `mqtt.disable` commands.
|
||||
- **No per-message QoS.** Command replies and broadcasts are published at QoS 0. Heartbeats use QoS 1 with `retain = true`.
|
||||
- **JSON parse errors drop the message silently** (with a `LOG_WARNING`). The client receives no error response.
|
||||
Reference in New Issue
Block a user