This repository has been archived on 2024-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
augie/webapp/lib/augie_web/live/gps_live.ex
2020-05-02 19:09:46 +12:00

69 lines
1.5 KiB
Elixir

defmodule AugieWeb.GpsLive do
use Phoenix.LiveView
alias Augie.Sensor.GPS
alias Phoenix.PubSub
@moduledoc """
A LiveView with displaus the data from the GPS.
"""
@empty_sample %{
status: nil,
latitude: nil,
longitude: nil,
altitude: nil,
heading: nil,
speed: nil,
satellites: nil
}
@map_base_uri "https://maps.googleapis.com/maps/api/staticmap"
@map_opts [size: "640x320", key: "AIzaSyBibH_1Yibm3gshxsQDUKw7mjaH9SyMrgw", maptype: "hybrid"]
def mount(_params, _context, socket) do
if connected?(socket), do: PubSub.subscribe(Augie.PubSub, "GPS")
socket =
socket
|> assign(sample_to_assigns(@empty_sample))
|> assign(map_src: map_url(zoom: 1))
{:ok, socket}
end
def handle_info(%GPS{} = sample, socket) do
socket =
socket
|> assign(sample_to_assigns(sample))
|> assign(map_url(sample_to_map_opts(sample)))
{:noreply, socket}
end
defp sample_to_assigns(sample) do
sample
|> Enum.map(fn
{key, nil} -> {key, "-"}
{key, value} when is_float(value) -> {key, Float.round(value, 2)}
{key, value} -> {key, value}
end)
end
defp sample_to_map_opts(sample) do
location = "#{sample.latitude},#{sample.longitude}"
[
center: location,
marker: "color:red|#{location}",
zoom: 15
]
end
defp map_url(opts) do
query =
@map_opts
|> Keyword.merge(opts)
|> URI.encode_query()
"#{@map_base_uri}?#{query}"
end
end