Update CLAUDE.md and fix final Tortoise handler return value

- Add dashboard development commands (just dashboard, mix phx.server)
- Document Dashboard.Application and Dashboard.MqttSubscriber components
- Add comprehensive dashboard section with MQTT configuration details
- Include critical implementation notes for Tortoise handler return values
- Fix handle_message to return {:ok, state} instead of [] to prevent crashes

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
ryan 2025-08-03 19:44:41 -07:00
parent c8e8e1dc24
commit 4a928b7067
2 changed files with 27 additions and 2 deletions

View File

@ -23,6 +23,10 @@ mix test test/systant_test.exs
# Enter development shell (via Nix) # Enter development shell (via Nix)
nix develop nix develop
# Run dashboard (Phoenix LiveView)
cd dashboard && mix phx.server
# or use justfile: just dashboard
``` ```
### Production ### Production
@ -41,6 +45,8 @@ This is an Elixir OTP application that serves as a systemd daemon for MQTT-based
### Core Components ### Core Components
- **Systant.Application** (`lib/systant/application.ex`): OTP application supervisor that starts the MQTT client - **Systant.Application** (`lib/systant/application.ex`): OTP application supervisor that starts the MQTT client
- **Systant.MqttClient** (`lib/systant/mqtt_client.ex`): GenServer that handles MQTT connection, publishes stats every 30 seconds, and listens for commands - **Systant.MqttClient** (`lib/systant/mqtt_client.ex`): GenServer that handles MQTT connection, publishes stats every 30 seconds, and listens for commands
- **Dashboard.Application** (`dashboard/lib/dashboard/application.ex`): Phoenix LiveView dashboard application
- **Dashboard.MqttSubscriber** (`dashboard/lib/dashboard/mqtt_subscriber.ex`): Real-time MQTT subscriber that feeds data to the LiveView dashboard
- **Configuration**: MQTT settings configurable via environment variables or config files - **Configuration**: MQTT settings configurable via environment variables or config files
### Key Libraries ### Key Libraries
@ -73,6 +79,25 @@ The NixOS module supports:
- Systemd service with security hardening - Systemd service with security hardening
- Auto-restart and logging to systemd journal - Auto-restart and logging to systemd journal
## Dashboard
The project includes a Phoenix LiveView dashboard (`dashboard/`) that provides real-time monitoring of all systant instances.
### Dashboard Features
- Real-time host status updates via MQTT subscription
- LiveView interface showing all connected hosts
- Automatic reconnection and error handling
### Dashboard MQTT Configuration
- Subscribes to `systant/+/stats` to receive updates from all hosts
- Uses hostname-based client ID: `systant-dashboard-${hostname}` to avoid conflicts
- Connects to `mqtt.home:1883` (same broker as systant instances)
### Important Implementation Notes
- **Tortoise Handler**: The `handle_message/3` callback must return `{:ok, state}`, not `[]`
- **Topic Parsing**: Topics may arrive as lists or strings, handle both formats
- **Client ID Conflicts**: Use unique client IDs to prevent connection instability
### Future Plans ### Future Plans
- Integration with Home Assistant via custom MQTT integration - Integration with Home Assistant via custom MQTT integration
- Expandable command handling for host-specific automation - Expandable command handling for host-specific automation

View File

@ -63,7 +63,7 @@ defmodule Dashboard.MqttSubscriber do
{:ok, state} {:ok, state}
end end
def handle_message(topic, payload, _state) do def handle_message(topic, payload, state) do
topic_parts = if is_binary(topic), do: String.split(topic, "/"), else: topic topic_parts = if is_binary(topic), do: String.split(topic, "/"), else: topic
case topic_parts do case topic_parts do
["systant", hostname, "stats"] -> ["systant", hostname, "stats"] ->
@ -83,7 +83,7 @@ defmodule Dashboard.MqttSubscriber do
_ -> _ ->
:ok :ok
end end
[] {:ok, state}
end end
@impl true @impl true