85 lines
2.5 KiB
Elixir
85 lines
2.5 KiB
Elixir
defmodule AprsmeWeb.WeatherLive.CallsignView do
|
|
@moduledoc false
|
|
use AprsmeWeb, :live_view
|
|
|
|
alias Aprsme.Packets
|
|
alias AprsmeWeb.MapLive.PacketUtils
|
|
|
|
@impl true
|
|
def mount(%{"callsign" => callsign}, _session, socket) do
|
|
normalized_callsign = String.upcase(String.trim(callsign))
|
|
weather_packet = get_latest_weather_packet(normalized_callsign)
|
|
{start_time, end_time} = default_time_range()
|
|
weather_history = get_weather_history(normalized_callsign, start_time, end_time)
|
|
|
|
weather_history_json =
|
|
weather_history
|
|
|> Enum.map(fn pkt ->
|
|
dew_point =
|
|
if is_number(pkt.temperature) and is_number(pkt.humidity) do
|
|
calc_dew_point(pkt.temperature, pkt.humidity)
|
|
end
|
|
|
|
%{
|
|
timestamp: pkt.received_at,
|
|
temperature: pkt.temperature,
|
|
dew_point: dew_point,
|
|
humidity: pkt.humidity,
|
|
pressure: pkt.pressure,
|
|
wind_direction: pkt.wind_direction,
|
|
wind_speed: pkt.wind_speed,
|
|
rain_1h: pkt.rain_1h,
|
|
rain_24h: pkt.rain_24h,
|
|
rain_since_midnight: pkt.rain_since_midnight,
|
|
luminosity: pkt.luminosity
|
|
}
|
|
end)
|
|
|> Jason.encode!()
|
|
|
|
socket =
|
|
socket
|
|
|> assign(:callsign, normalized_callsign)
|
|
|> assign(:weather_packet, weather_packet)
|
|
|> assign(:page_title, "Weather for #{normalized_callsign}")
|
|
|> assign(:weather_history, weather_history)
|
|
|> assign(:weather_history_json, weather_history_json)
|
|
|
|
{:ok, socket}
|
|
end
|
|
|
|
defp get_latest_weather_packet(callsign) do
|
|
# Get the most recent packet for this callsign that is a weather report
|
|
%{callsign: callsign, limit: 10}
|
|
|> Packets.get_recent_packets()
|
|
|> Enum.find(&PacketUtils.weather_packet?/1)
|
|
end
|
|
|
|
defp get_weather_history(callsign, start_time, end_time) do
|
|
Packets.get_weather_packets(callsign, start_time, end_time, %{limit: 500})
|
|
end
|
|
|
|
defp default_time_range do
|
|
now = DateTime.utc_now()
|
|
{DateTime.add(now, -24 * 3600, :second), now}
|
|
end
|
|
|
|
defp calc_dew_point(temp, humidity) when is_number(temp) and is_number(humidity) do
|
|
temp - (100 - humidity) / 5
|
|
end
|
|
|
|
defp calc_dew_point(_, _), do: nil
|
|
|
|
@doc """
|
|
Gets weather field value, returning "0" instead of "N/A" for missing numeric data.
|
|
"""
|
|
def get_weather_field_zero(packet, key) do
|
|
value = PacketUtils.get_weather_field(packet, key)
|
|
|
|
# Return "0" for missing numeric fields, keep other values as-is
|
|
case value do
|
|
"N/A" -> "0"
|
|
nil -> "0"
|
|
_ -> value
|
|
end
|
|
end
|
|
end
|