diff --git a/lib/aprsme/device_parser.ex b/lib/aprsme/device_parser.ex index 45e0546..f9c7643 100644 --- a/lib/aprsme/device_parser.ex +++ b/lib/aprsme/device_parser.ex @@ -9,12 +9,17 @@ defmodule Aprsme.DeviceParser do """ @spec extract_device_identifier(map()) :: String.t() | nil def extract_device_identifier(packet_data) do - # Try to extract from various possible locations - packet_data - |> Map.get(:device_identifier) - |> case do - nil -> extract_from_data_extended(packet_data) - identifier -> identifier + cond do + Map.has_key?(packet_data, :destination) and not is_nil(packet_data[:destination]) -> + packet_data[:destination] + Map.has_key?(packet_data, "destination") and not is_nil(packet_data["destination"]) -> + packet_data["destination"] + Map.has_key?(packet_data, :device_identifier) and not is_nil(packet_data[:device_identifier]) -> + packet_data[:device_identifier] + Map.has_key?(packet_data, "device_identifier") and not is_nil(packet_data["device_identifier"]) -> + packet_data["device_identifier"] + true -> + extract_from_data_extended(packet_data) end end diff --git a/lib/aprsme/packets.ex b/lib/aprsme/packets.ex index c94a528..4a1f073 100644 --- a/lib/aprsme/packets.ex +++ b/lib/aprsme/packets.ex @@ -45,6 +45,11 @@ defmodule Aprsme.Packets do # require Logger # Logger.debug("Sanitized packet_attrs before insert: #{inspect(packet_attrs)}") packet_attrs = Map.new(packet_attrs, fn {k, v} -> {k, sanitize_packet_strings(v)} end) + # Set device_identifier to parsed value, fallback to destination if nil + parsed_device_id = Aprsme.DeviceParser.extract_device_identifier(packet_data) + device_id = parsed_device_id || Map.get(packet_attrs, :destination) + packet_attrs = Map.put(packet_attrs, :device_identifier, device_id) + # Logger.debug("Inserting packet with device_identifier=#{inspect(device_id)}, destination=#{inspect(Map.get(packet_attrs, :destination))}") insert_packet(packet_attrs, packet_data) rescue error -> diff --git a/lib/aprsme_web/live/info_live/show.ex b/lib/aprsme_web/live/info_live/show.ex index 5c86cb8..6b191cd 100644 --- a/lib/aprsme_web/live/info_live/show.ex +++ b/lib/aprsme_web/live/info_live/show.ex @@ -55,10 +55,12 @@ defmodule AprsmeWeb.InfoLive.Show do |> uniq_by(& &1.sender) |> Enum.map(fn p -> dist = haversine(lat, lon, p.lat, p.lon) + course = calculate_course(lat, lon, p.lat, p.lon) %{ callsign: p.sender, distance: format_distance(dist), + course: course, last_heard: PacketUtils.get_timestamp(p), packet: p } @@ -105,4 +107,19 @@ defmodule AprsmeWeb.InfoLive.Show do defp format_distance(km) do "#{Float.round(km, 2)} km" end + + defp calculate_course(lat1, lon1, lat2, lon2) do + # Calculate bearing from point 1 to point 2 + dlon = :math.pi() / 180 * (lon2 - lon1) + + lat1_rad = :math.pi() / 180 * lat1 + lat2_rad = :math.pi() / 180 * lat2 + + y = :math.sin(dlon) * :math.cos(lat2_rad) + x = :math.cos(lat1_rad) * :math.sin(lat2_rad) - :math.sin(lat1_rad) * :math.cos(lat2_rad) * :math.cos(dlon) + + bearing = :math.atan2(y, x) * 180 / :math.pi() + # Convert to 0-360 range + if bearing < 0, do: bearing + 360, else: bearing + end end diff --git a/lib/aprsme_web/live/info_live/show.html.heex b/lib/aprsme_web/live/info_live/show.html.heex index 9246b9e..a917e89 100644 --- a/lib/aprsme_web/live/info_live/show.html.heex +++ b/lib/aprsme_web/live/info_live/show.html.heex @@ -85,36 +85,7 @@
- {@packet.comment} -
-