Fix dashboard MQTT connection and remove simulation code
- Replace SimpleMqtt simulation with real MqttSubscriber - Fix String.split bug when handling MQTT topic parsing - Use hostname-based client ID to avoid MQTT client conflicts - Add process management tools (hivemind, just) to development environment 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -19,17 +19,27 @@ defmodule Dashboard.MqttSubscriber do
|
||||
|
||||
@impl true
|
||||
def init(_opts) do
|
||||
# Start MQTT connection in a supervised way
|
||||
{:ok, _pid} = Tortoise.Supervisor.start_child(
|
||||
:dashboard_mqtt,
|
||||
client_id: :dashboard_mqtt,
|
||||
# Start MQTT connection directly with hostname-based client ID to avoid conflicts
|
||||
{:ok, hostname} = :inet.gethostname()
|
||||
client_id = "systant-dashboard-#{hostname}"
|
||||
connection_opts = [
|
||||
client_id: client_id,
|
||||
server: {Tortoise.Transport.Tcp, host: "mqtt.home", port: 1883},
|
||||
handler: {__MODULE__, []},
|
||||
subscriptions: [{"systant/+/stats", 0}]
|
||||
)
|
||||
|
||||
Logger.info("Dashboard MQTT subscriber started")
|
||||
{:ok, %{hosts: %{}}}
|
||||
]
|
||||
|
||||
case Tortoise.Connection.start_link(connection_opts) do
|
||||
{:ok, _pid} ->
|
||||
Logger.info("Dashboard MQTT subscriber connected successfully")
|
||||
{:ok, %{hosts: %{}}}
|
||||
{:error, {:already_started, _pid}} ->
|
||||
Logger.info("Dashboard MQTT connection already exists, reusing")
|
||||
{:ok, %{hosts: %{}}}
|
||||
{:error, reason} ->
|
||||
Logger.error("Failed to connect to MQTT broker: #{inspect(reason)}")
|
||||
{:stop, reason}
|
||||
end
|
||||
end
|
||||
|
||||
@impl true
|
||||
@@ -43,11 +53,19 @@ defmodule Dashboard.MqttSubscriber do
|
||||
end
|
||||
|
||||
# Tortoise handler callbacks
|
||||
def connection(_status, _state), do: []
|
||||
def subscription(_status, _topic, _state), do: []
|
||||
def connection(status, state) do
|
||||
Logger.info("MQTT connection status: #{status}")
|
||||
{:ok, state}
|
||||
end
|
||||
|
||||
def subscription(status, topic, state) do
|
||||
Logger.info("MQTT subscription status for #{topic}: #{status}")
|
||||
{:ok, state}
|
||||
end
|
||||
|
||||
def handle_message(topic, payload, _state) do
|
||||
case String.split(topic, "/") do
|
||||
topic_parts = if is_binary(topic), do: String.split(topic, "/"), else: topic
|
||||
case topic_parts do
|
||||
["systant", hostname, "stats"] ->
|
||||
case Jason.decode(payload) do
|
||||
{:ok, data} ->
|
||||
@@ -74,5 +92,6 @@ defmodule Dashboard.MqttSubscriber do
|
||||
{:noreply, %{state | hosts: updated_hosts}}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def terminate(_reason, _state), do: []
|
||||
end
|
||||
Reference in New Issue
Block a user