defmodule AprsWeb.MapLive.Index do @moduledoc """ LiveView for displaying real-time APRS packets on a map """ use AprsWeb, :live_view alias Aprs.EncodingUtils alias AprsWeb.Endpoint alias AprsWeb.Helpers.AprsSymbols alias Parser.Types.MicE @default_center %{lat: 39.8283, lng: -98.5795} @default_zoom 5 @ip_api_url "https://ip-api.com/json/" @finch_name Aprs.Finch @default_replay_speed 1000 @impl true def mount(_params, _session, socket) do # Calculate one hour ago for packet age filtering one_hour_ago = DateTime.add(DateTime.utc_now(), -3600, :second) socket = assign(socket, packets: [], page_title: "APRS Map", # Track visible packets by callsign visible_packets: %{}, # Default bounds for USA map_bounds: %{ north: 49.0, south: 24.0, east: -66.0, west: -125.0 }, map_center: @default_center, map_zoom: @default_zoom, # Replay controls replay_active: false, replay_speed: @default_replay_speed, replay_paused: false, replay_packets: [], replay_index: 0, replay_timer_ref: nil, replay_start_time: nil, replay_end_time: nil, # Map of packet IDs to packet data for historical packets historical_packets: %{}, # Timestamp for filtering out old packets packet_age_threshold: one_hour_ago, # Flag to indicate if map is ready for replay map_ready: false, # Flag to prevent multiple replay starts replay_started: false, # Pending geolocation to zoom to after map is ready pending_geolocation: nil ) if connected?(socket) do IO.puts("Socket is connected, attempting to get IP location") Endpoint.subscribe("aprs_messages") # Get IP-based location on initial load IO.puts("Connect info: #{inspect(socket.private[:connect_info])}") IO.puts("Peer data: #{inspect(socket.private[:connect_info][:peer_data])}") ip = case socket.private[:connect_info][:peer_data][:address] do {a, b, c, d} -> "#{a}.#{b}.#{c}.#{d}" {a, b, c, d, e, f, g, h} -> "#{a}:#{b}:#{c}:#{d}:#{e}:#{f}:#{g}:#{h}" _ -> nil end IO.puts("Extracted IP address: #{inspect(ip)}") if ip do IO.puts("Starting IP geolocation task for IP: #{ip}") # Start as a separate task and await the result Task.start(fn -> try do get_ip_location(ip) rescue error -> IO.puts("Error in IP geolocation task: #{inspect(error)}") IO.puts("Stacktrace: #{inspect(__STACKTRACE__)}") send(self(), {:ip_location, @default_center}) end end) else IO.puts("No IP address found, skipping geolocation") end # Schedule regular cleanup of old packets from the map if connected?(socket), do: Process.send_after(self(), :cleanup_old_packets, 60_000) # Schedule initialization of replay after a short delay if connected?(socket), do: Process.send_after(self(), :initialize_replay, 2000) end {:ok, socket} end @impl true def handle_event("bounds_changed", %{"bounds" => bounds}, socket) do handle_bounds_update(bounds, socket) end @impl true def handle_event("update_bounds", %{"bounds" => bounds}, socket) do handle_bounds_update(bounds, socket) end @impl true def handle_event("locate_me", _params, socket) do # Send JavaScript command to request browser geolocation IO.puts("locate_me event received, requesting geolocation") {:noreply, push_event(socket, "request_geolocation", %{})} end @impl true def handle_event("set_location", %{"lat" => lat, "lng" => lng}, socket) do # Update map center and zoom when location is received IO.puts("set_location event received with lat=#{lat}, lng=#{lng}") # Ensure coordinates are floats lat_float = cond do is_binary(lat) -> String.to_float(lat) is_integer(lat) -> lat / 1.0 true -> lat end lng_float = cond do is_binary(lng) -> String.to_float(lng) is_integer(lng) -> lng / 1.0 true -> lng end IO.puts("Sending zoom_to_location event from set_location with lat=#{lat_float}, lng=#{lng_float}") socket = socket |> assign(map_center: %{lat: lat_float, lng: lng_float}, map_zoom: 12) |> push_event("zoom_to_location", %{lat: lat_float, lng: lng_float, zoom: 12}) {:noreply, socket} end @impl true def handle_event("toggle_replay", _params, socket) do if socket.assigns.replay_active do # Stop replay if socket.assigns.replay_timer_ref, do: Process.cancel_timer(socket.assigns.replay_timer_ref) # Clear historical packets from the map socket = socket |> push_event("clear_historical_packets", %{}) |> assign( replay_active: false, replay_timer_ref: nil, replay_paused: false, replay_packets: [], replay_index: 0, historical_packets: %{}, replay_started: false ) # Restart replay after a short delay Process.send_after(self(), :initialize_replay, 1000) {:noreply, socket} else # If not active, the user manually requested a replay restart {:noreply, start_historical_replay(socket)} end end @impl true def handle_event("pause_replay", _params, socket) do if socket.assigns.replay_active do if socket.assigns.replay_paused do # Resume replay timer_ref = Process.send_after(self(), :replay_next_packet, 1000) {:noreply, assign(socket, replay_paused: false, replay_timer_ref: timer_ref)} else # Pause replay if socket.assigns.replay_timer_ref, do: Process.cancel_timer(socket.assigns.replay_timer_ref) {:noreply, assign(socket, replay_paused: true, replay_timer_ref: nil)} end else {:noreply, socket} end end @impl true def handle_event("adjust_replay_speed", %{"speed" => speed}, socket) do speed_float = String.to_float(speed) {:noreply, assign(socket, replay_speed: speed_float)} end @impl true def handle_event("map_ready", _params, socket) do IO.puts("Map ready event received") socket = assign(socket, map_ready: true) # If we have pending geolocation, zoom to it now socket = if socket.assigns.pending_geolocation do %{lat: lat, lng: lng} = socket.assigns.pending_geolocation IO.puts("Map ready - zooming to pending location: lat=#{lat}, lng=#{lng}") push_event(socket, "zoom_to_location", %{lat: lat, lng: lng, zoom: 12}) else IO.puts("Map ready - no pending geolocation") socket end {:noreply, socket} end defp handle_bounds_update(bounds, socket) do # Update the map bounds from the client map_bounds = %{ north: bounds["north"], south: bounds["south"], east: bounds["east"], west: bounds["west"] } # Don't clear markers - let the client preserve those still in view # Recalculate packet count based on new bounds visible_packets = socket.assigns.visible_packets |> Enum.filter(fn {_callsign, packet} -> within_bounds?(packet, map_bounds) end) |> Map.new() # If replay is not active, update the replay packets based on the new bounds socket = if socket.assigns.replay_active do socket else # Clear any existing replay data when bounds change socket = assign(socket, replay_packets: [], replay_index: 0, historical_packets: %{}, map_ready: true ) # Don't automatically start replay on every bounds change # We'll handle this in initialize_replay to avoid too many restarts socket end {:noreply, assign(socket, map_bounds: map_bounds, visible_packets: visible_packets)} end @impl true def handle_info(msg, socket) do case msg do {:delayed_zoom, %{lat: lat, lng: lng}} -> IO.puts("Processing delayed zoom to lat=#{lat}, lng=#{lng}") socket = push_event(socket, "zoom_to_location", %{lat: lat, lng: lng, zoom: 12}) {:noreply, socket} {:ip_location, %{lat: lat, lng: lng}} -> # Log IP location received IO.puts("IP location received: lat=#{lat}, lng=#{lng}") # Ensure we're using numeric values for coordinates lat_float = cond do is_binary(lat) -> String.to_float(lat) is_integer(lat) -> lat / 1.0 true -> lat end lng_float = cond do is_binary(lng) -> String.to_float(lng) is_integer(lng) -> lng / 1.0 true -> lng end # Update map center first socket = assign(socket, map_center: %{lat: lat_float, lng: lng_float}, map_zoom: 12) # If map is ready, zoom to location immediately, otherwise store for later socket = if socket.assigns.map_ready do IO.puts("Map is ready, zooming to location immediately to lat=#{lat_float}, lng=#{lng_float}") push_event(socket, "zoom_to_location", %{lat: lat_float, lng: lng_float, zoom: 12}) else IO.puts("Map not ready yet, storing location lat=#{lat_float}, lng=#{lng_float} for later") assign(socket, pending_geolocation: %{lat: lat_float, lng: lng_float}) end {:noreply, socket} :initialize_replay -> # Only start replay if it hasn't been started yet if not socket.assigns.replay_started and socket.assigns.map_ready do socket = start_historical_replay(socket) {:noreply, assign(socket, replay_started: true)} else {:noreply, socket} end :replay_next_packet -> handle_replay_next_packet(socket) :cleanup_old_packets -> # Clean up packets older than 1 hour from the map display handle_cleanup_old_packets(socket) %{event: "packet", payload: payload} -> # Sanitize the packet to prevent encoding errors sanitized_packet = EncodingUtils.sanitize_packet(payload) # Add received timestamp if not present sanitized_packet = Map.put_new(sanitized_packet, :received_at, DateTime.utc_now()) # Only process packets with position data that are within current map bounds # AND are not older than 1 hour if has_position_data?(sanitized_packet) && within_bounds?(sanitized_packet, socket.assigns.map_bounds) && packet_within_time_threshold?(sanitized_packet, socket.assigns.packet_age_threshold) do # Convert to a simple map structure for JSON encoding packet_data = build_packet_data(sanitized_packet) # Only push if we have valid packet data if packet_data do # Generate a unique key for this packet callsign_key = "#{sanitized_packet.base_callsign}#{if sanitized_packet.ssid, do: "-#{sanitized_packet.ssid}", else: ""}" # Update visible packets tracking visible_packets = Map.put(socket.assigns.visible_packets, callsign_key, sanitized_packet) # Push the packet to the client-side JavaScript socket = socket |> push_event("new_packet", packet_data) |> assign(visible_packets: visible_packets) {:noreply, socket} else # Invalid packet data, skip it {:noreply, socket} end else # Ignore packets without position data, outside bounds, or too old {:noreply, socket} end end end # Handle replaying the next historical packet defp handle_replay_next_packet(socket) do %{ replay_packets: packets, replay_index: index, replay_speed: speed, historical_packets: historical_packets } = socket.assigns if index < length(packets) do # Get the next packet and advance the index packet = Enum.at(packets, index) next_index = index + 1 # Convert to a simple map structure for JSON encoding packet_data = build_packet_data(packet) # Only process packets with valid position data socket = if packet_data do # Add is_historical flag and timestamp packet_data = Map.merge(packet_data, %{ "is_historical" => true, "timestamp" => if(Map.has_key?(packet, :received_at), do: DateTime.to_iso8601(packet.received_at)) }) # Generate a unique key for this packet packet_id = "hist_#{if Map.has_key?(packet, :id), do: packet.id, else: System.unique_integer([:positive])}" # Update historical packets tracking new_historical_packets = Map.put(historical_packets, packet_id, packet) # Send packet data to client socket |> push_event("historical_packet", packet_data) |> assign( replay_index: next_index, historical_packets: new_historical_packets ) else # Skip packets without position data, just advance the index assign(socket, replay_index: next_index) end # Calculate delay for next packet (faster based on replay speed) delay_ms = trunc(1000 / speed) # Schedule the next packet timer_ref = Process.send_after(self(), :replay_next_packet, delay_ms) {:noreply, assign(socket, replay_timer_ref: timer_ref)} else # All packets replayed, start over with a short delay Process.send_after(self(), :initialize_replay, 10_000) # Set active to false but don't clear packets - will auto-restart socket = assign(socket, replay_active: false, replay_timer_ref: nil, replay_started: false ) {:noreply, socket} end end @impl true def render(assigns) do ~H"""