fix: add wind direction normalization in changeset

Normalize wind direction values to valid range (0-359 degrees) during
packet insertion. Invalid wind direction values (360 or > 360, or
negative) are automatically set to 0, preventing bad data from entering
the database.

This complements the fix in the APRS parser and ensures data integrity
at the application level as well.
This commit is contained in:
Graham McIntire 2026-03-22 13:49:59 -05:00
parent 449c81d6f2
commit b5c9388399
No known key found for this signature in database

View file

@ -149,6 +149,7 @@ defmodule Aprsme.Packet do
|> maybe_set_has_position()
|> normalize_symbols()
|> normalize_course()
|> normalize_wind_direction()
end
defp normalize_symbols(changeset) do
@ -202,6 +203,29 @@ defmodule Aprsme.Packet do
end
end
defp normalize_wind_direction(changeset) do
wind_direction = get_field(changeset, :wind_direction) || get_change(changeset, :wind_direction)
case wind_direction do
nil ->
changeset
dir when is_integer(dir) and dir >= 0 and dir <= 359 ->
changeset
360 ->
# 360 degrees = 0 degrees (full circle)
put_change(changeset, :wind_direction, 0)
dir when is_integer(dir) ->
# Invalid wind direction (negative or > 360), normalize to 0
put_change(changeset, :wind_direction, 0)
_ ->
changeset
end
end
@spec maybe_create_geometry_from_lat_lon(Ecto.Changeset.t()) :: Ecto.Changeset.t()
defp maybe_create_geometry_from_lat_lon(changeset) do
lat = get_field(changeset, :lat) || get_change(changeset, :lat)