diff --git a/morse/lib/morse/server.ex b/morse/lib/morse/server.ex index a27bf4d..e2c6980 100644 --- a/morse/lib/morse/server.ex +++ b/morse/lib/morse/server.ex @@ -5,8 +5,8 @@ defmodule Morse.Server do GenServer.start_link(__MODULE__, {nil, 0}, name: __MODULE__) end - def start_morse do - GenServer.call(__MODULE__, :start) + def toggle_morse do + GenServer.call(__MODULE__, :toggle) end def update_progress(progress) do @@ -27,9 +27,11 @@ defmodule Morse.Server do end @impl true - def handle_call(:start, _from, {pid, _progress} = state) do + def handle_call(:toggle, _from, {pid, _progress}) do if worker_alive?(pid) do - {:reply, {:error, :already_started}, state} + Morse.Worker.kill(pid) + apply(pubsub(), :broadcast, [Ui.PubSub, "morse_progress", 0]) + {:reply, :ok, {nil, 0}} else pid = spawn(&Morse.Worker.signal/0) {:reply, :ok, {pid, 0}} diff --git a/morse/lib/morse/worker.ex b/morse/lib/morse/worker.ex index af9d7dd..9b40d53 100644 --- a/morse/lib/morse/worker.ex +++ b/morse/lib/morse/worker.ex @@ -32,6 +32,11 @@ defmodule Morse.Worker do update_progress(100, 100) end + def kill(pid) do + Process.exit(pid, :kill) + toggle_lamp(@off) + end + defp signal_symbol({?., _index}, _length) do toggle_lamp(@on) Process.sleep(@sleep_short) diff --git a/ui/assets/css/ruby.css b/ui/assets/css/ruby.css index d93be1d..bf569ee 100644 --- a/ui/assets/css/ruby.css +++ b/ui/assets/css/ruby.css @@ -1,42 +1,42 @@ body { - background-color: lightblue; + background-color: lightblue; } h1 { - font-size: 30px; - font-family: "Verdana", Verdana, sans-serif; + font-size: 30px; + font-family: "Verdana", Verdana, sans-serif; } h2 { - font-size: 25px; - font-family: "Verdana", Verdana, sans-serif; - font-weight: normal; + font-size: 25px; + font-family: "Verdana", Verdana, sans-serif; + font-weight: normal; } p { - font-size: 20px; - font-family: "Verdana", Verdana, sans-serif; + font-size: 20px; + font-family: "Verdana", Verdana, sans-serif; } #buttonwrap { - text-align: center; + text-align: center; } .button { - background-color: #4CAF50; /* Green */ - border: none; - color: white; - padding: 15px 32px; - text-align: center; - text-decoration: none; - display: inline-block; - font-size: 16px; - width: 70%; - height: 100px; + background-color: #4CAF50; /* Green */ + border: none; + color: white; + padding: 15px 32px; + text-align: center; + text-decoration: none; + display: inline-block; + font-size: 16px; + width: 70%; + height: 100px; } -.button[disabled=disabled] { - background-color: gray; +.button.stop { + background-color: red; } #hintbutton { diff --git a/ui/lib/ui_web/live/morse_live.ex b/ui/lib/ui_web/live/morse_live.ex index 16863a7..2632ce4 100644 --- a/ui/lib/ui_web/live/morse_live.ex +++ b/ui/lib/ui_web/live/morse_live.ex @@ -12,10 +12,11 @@ defmodule UiWeb.MorseLive do {:ok, assign(socket, default_assigns(ip))} end - def handle_event("start_morse", _value, %{assigns: %{ip: ip}} = socket) do - if :ok == Morse.Server.start_morse() do + def handle_event("toggle_morse", _value, %{assigns: %{ip: ip}} = socket) do + if Morse.Server.in_progress?() do spawn fn -> Ui.TelegramBot.message("#{ip} pressed the button!") end end + Morse.Server.toggle_morse() {:noreply, socket} end diff --git a/ui/lib/ui_web/templates/page/morse.html.leex b/ui/lib/ui_web/templates/page/morse.html.leex index 80efaaf..fbb562e 100644 --- a/ui/lib/ui_web/templates/page/morse.html.leex +++ b/ui/lib/ui_web/templates/page/morse.html.leex @@ -4,9 +4,9 @@