From 9b147101e1f782507f68e3ca25bf9a3ded0d7ae5 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Sun, 3 Aug 2025 15:46:12 -0500 Subject: [PATCH] fix: Handle both atom and string keys in weather view packet comparison MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The weather view was crashing with KeyError when receiving packets with string keys instead of atom keys. This was happening when packets were broadcast from different parts of the system. Changes: - Updated should_update_weather? to handle both atom and string keys - Added proper nil checking for received_at timestamps - Handle string timestamp comparisons as well as DateTime comparisons This prevents crashes when the weather view receives packets with inconsistent key types from PubSub broadcasts. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .../live/weather_live/callsign_view.ex | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/aprsme_web/live/weather_live/callsign_view.ex b/lib/aprsme_web/live/weather_live/callsign_view.ex index 9be2f17..a07b0e7 100644 --- a/lib/aprsme_web/live/weather_live/callsign_view.ex +++ b/lib/aprsme_web/live/weather_live/callsign_view.ex @@ -93,7 +93,23 @@ defmodule AprsmeWeb.WeatherLive.CallsignView do defp should_update_weather?(current_packet, new_packet) do # Only update if the new packet is actually newer - DateTime.after?(new_packet.received_at, current_packet.received_at) + # Handle both atom and string keys + new_received_at = Map.get(new_packet, :received_at) || Map.get(new_packet, "received_at") + current_received_at = Map.get(current_packet, :received_at) || Map.get(current_packet, "received_at") + + case {new_received_at, current_received_at} do + {nil, _} -> + false + + {_, nil} -> + true + + {new_time, current_time} when is_binary(new_time) and is_binary(current_time) -> + new_time > current_time + + {new_time, current_time} -> + DateTime.after?(new_time, current_time) + end end defp update_weather_data(socket) do