36 lines
1.1 KiB
Elixir
36 lines
1.1 KiB
Elixir
defmodule AugieWeb.SafetyLive do
|
||
alias Augie.Firmata.Commands
|
||
alias Phoenix.PubSub
|
||
use Phoenix.LiveView
|
||
|
||
@moduledoc false
|
||
|
||
def mount(_params, _context, socket) do
|
||
if connected?(socket) do
|
||
PubSub.subscribe(Augie.PubSub, "command.safety")
|
||
Commands.get_safety()
|
||
end
|
||
|
||
{:ok, assign(socket, state: :unknown)}
|
||
end
|
||
|
||
def handle_event("toggle-safety", _, %{assigns: %{state: :unknown}} = socket),
|
||
do: {:noreply, socket}
|
||
|
||
def handle_event("toggle-safety", _, %{assigns: %{state: :armed}} = socket),
|
||
do: {:noreply, assign(socket, state: :safe)}
|
||
|
||
def handle_event("toggle-safety", _, %{assigns: %{state: :safe}} = socket),
|
||
do: {:noreply, assign(socket, state: :armed)}
|
||
|
||
def handle_info({:safety, :armed}, socket), do: {:noreply, assign(socket, state: :armed)}
|
||
def handle_info({:safety, :safe}, socket), do: {:noreply, assign(socket, state: :safe)}
|
||
|
||
defp message(:unknown), do: "UNKNOWN"
|
||
defp message(:armed), do: "ARMED"
|
||
defp message(:safe), do: "SAFE"
|
||
|
||
defp emoji(:unknown), do: "ℹ️"
|
||
defp emoji(:armed), do: "⚠️"
|
||
defp emoji(:safe), do: "✅"
|
||
end
|