Beacons: - Scaffolded with phx.gen.live then reworked so reads are public and mutations go through a live_session gated by the new :require_admin on_mount hook in UserAuth - Beacon schema stores frequency (MHz), callsign, grid, lat/lon, power (W), and height above ground (m); grid auto-derives from lat/lon when left blank via Maidenhead.from_latlon - Adds Maidenhead.from_latlon/3 so we can compute grids locally instead of hitting an external API Users admin page: - /users and /users/:id/edit (admin-only) for listing, editing (callsign/name/email/is_admin), and deleting other users - Adds Accounts.list_users, admin_update_user, delete_user, and a dedicated admin_changeset on the User schema - Nav gains a "Users" link for admins and a "Beacons" link for everyone
86 lines
2.6 KiB
Elixir
86 lines
2.6 KiB
Elixir
defmodule Microwaveprop.Beacons.Beacon do
|
|
@moduledoc """
|
|
A beacon transmitter. Frequency in MHz, coordinates as lat/lon,
|
|
grid as Maidenhead (auto-computed from lat/lon when not supplied),
|
|
power in watts, height above ground in meters.
|
|
"""
|
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
|
|
|
alias Microwaveprop.Radio.Maidenhead
|
|
|
|
@primary_key {:id, :binary_id, autogenerate: true}
|
|
@foreign_key_type :binary_id
|
|
schema "beacons" do
|
|
field :frequency_mhz, :float
|
|
field :callsign, :string
|
|
field :grid, :string
|
|
field :lat, :float
|
|
field :lon, :float
|
|
field :power_watts, :float
|
|
field :height_m, :float
|
|
field :user_id, :binary_id
|
|
|
|
timestamps(type: :utc_datetime)
|
|
end
|
|
|
|
@required_fields [:frequency_mhz, :callsign, :lat, :lon, :power_watts, :height_m]
|
|
|
|
def changeset(beacon, attrs) do
|
|
beacon
|
|
|> cast(attrs, [:frequency_mhz, :callsign, :grid, :lat, :lon, :power_watts, :height_m])
|
|
|> update_change(:callsign, fn cs -> cs && String.upcase(String.trim(cs)) end)
|
|
|> validate_required(@required_fields)
|
|
|> validate_number(:frequency_mhz, greater_than: 0)
|
|
|> validate_number(:power_watts, greater_than_or_equal_to: 0)
|
|
|> validate_number(:height_m, greater_than_or_equal_to: 0)
|
|
|> validate_number(:lat, greater_than_or_equal_to: -90, less_than_or_equal_to: 90)
|
|
|> validate_number(:lon, greater_than_or_equal_to: -180, less_than_or_equal_to: 180)
|
|
|> validate_length(:callsign, min: 3, max: 10)
|
|
|> maybe_fill_grid()
|
|
|> validate_grid_format()
|
|
end
|
|
|
|
# If grid is blank but lat/lon are valid, derive it.
|
|
defp maybe_fill_grid(changeset) do
|
|
grid = get_field(changeset, :grid)
|
|
lat = get_field(changeset, :lat)
|
|
lon = get_field(changeset, :lon)
|
|
|
|
if grid in [nil, ""] and is_number(lat) and is_number(lon) do
|
|
put_change(changeset, :grid, Maidenhead.from_latlon(lat, lon, 6))
|
|
else
|
|
changeset
|
|
end
|
|
end
|
|
|
|
defp validate_grid_format(changeset) do
|
|
case get_field(changeset, :grid) do
|
|
nil ->
|
|
add_error(changeset, :grid, "can't be blank")
|
|
|
|
grid ->
|
|
if Maidenhead.valid?(grid) do
|
|
update_change(changeset, :grid, &normalize_grid/1)
|
|
else
|
|
add_error(changeset, :grid, "is not a valid Maidenhead grid")
|
|
end
|
|
end
|
|
end
|
|
|
|
# Field uppercase, square digits, subsquare lowercase (standard form)
|
|
defp normalize_grid(nil), do: nil
|
|
|
|
defp normalize_grid(grid) do
|
|
grid = String.trim(grid)
|
|
len = String.length(grid)
|
|
|
|
if len >= 6 do
|
|
grid |> String.slice(0, 4) |> String.upcase() |> Kernel.<>(String.downcase(String.slice(grid, 4, len - 4)))
|
|
else
|
|
String.upcase(grid)
|
|
end
|
|
end
|
|
end
|