systant/dashboard/lib/dashboard/simple_mqtt.ex
2025-08-03 13:33:21 -07:00

42 lines
1.1 KiB
Elixir

defmodule Dashboard.SimpleMqtt do
@moduledoc """
Simple GenServer that polls for MQTT data instead of complex subscriptions.
"""
use GenServer
require Logger
alias Phoenix.PubSub
def start_link(_opts) do
GenServer.start_link(__MODULE__, [], name: __MODULE__)
end
@impl true
def init(_) do
# Start a timer that simulates receiving MQTT data
# In a real implementation, you'd use a proper MQTT client here
Logger.info("Starting simple MQTT poller")
# For now, just generate fake data that matches what systant publishes
:timer.send_interval(5000, self(), :simulate_mqtt)
{:ok, %{}}
end
@impl true
def handle_info(:simulate_mqtt, state) do
# Simulate receiving an MQTT message from orion
hostname = "orion"
host_data = %{
"message" => "Hello from systant",
"hostname" => hostname,
"timestamp" => DateTime.utc_now() |> DateTime.to_iso8601(),
"last_seen" => DateTime.utc_now()
}
Logger.info("Simulating MQTT message from #{hostname}")
PubSub.broadcast(Dashboard.PubSub, "systant:hosts", {:host_update, hostname, host_data})
{:noreply, state}
end
end