Add ability to stop the morse.
This commit is contained in:
parent
0e722b813e
commit
7ea4dd1752
5 changed files with 37 additions and 29 deletions
|
@ -5,8 +5,8 @@ defmodule Morse.Server do
|
||||||
GenServer.start_link(__MODULE__, {nil, 0}, name: __MODULE__)
|
GenServer.start_link(__MODULE__, {nil, 0}, name: __MODULE__)
|
||||||
end
|
end
|
||||||
|
|
||||||
def start_morse do
|
def toggle_morse do
|
||||||
GenServer.call(__MODULE__, :start)
|
GenServer.call(__MODULE__, :toggle)
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_progress(progress) do
|
def update_progress(progress) do
|
||||||
|
@ -27,9 +27,11 @@ defmodule Morse.Server do
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_call(:start, _from, {pid, _progress} = state) do
|
def handle_call(:toggle, _from, {pid, _progress}) do
|
||||||
if worker_alive?(pid) 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
|
else
|
||||||
pid = spawn(&Morse.Worker.signal/0)
|
pid = spawn(&Morse.Worker.signal/0)
|
||||||
{:reply, :ok, {pid, 0}}
|
{:reply, :ok, {pid, 0}}
|
||||||
|
|
|
@ -32,6 +32,11 @@ defmodule Morse.Worker do
|
||||||
update_progress(100, 100)
|
update_progress(100, 100)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def kill(pid) do
|
||||||
|
Process.exit(pid, :kill)
|
||||||
|
toggle_lamp(@off)
|
||||||
|
end
|
||||||
|
|
||||||
defp signal_symbol({?., _index}, _length) do
|
defp signal_symbol({?., _index}, _length) do
|
||||||
toggle_lamp(@on)
|
toggle_lamp(@on)
|
||||||
Process.sleep(@sleep_short)
|
Process.sleep(@sleep_short)
|
||||||
|
|
|
@ -1,42 +1,42 @@
|
||||||
body {
|
body {
|
||||||
background-color: lightblue;
|
background-color: lightblue;
|
||||||
}
|
}
|
||||||
|
|
||||||
h1 {
|
h1 {
|
||||||
font-size: 30px;
|
font-size: 30px;
|
||||||
font-family: "Verdana", Verdana, sans-serif;
|
font-family: "Verdana", Verdana, sans-serif;
|
||||||
}
|
}
|
||||||
|
|
||||||
h2 {
|
h2 {
|
||||||
font-size: 25px;
|
font-size: 25px;
|
||||||
font-family: "Verdana", Verdana, sans-serif;
|
font-family: "Verdana", Verdana, sans-serif;
|
||||||
font-weight: normal;
|
font-weight: normal;
|
||||||
}
|
}
|
||||||
|
|
||||||
p {
|
p {
|
||||||
font-size: 20px;
|
font-size: 20px;
|
||||||
font-family: "Verdana", Verdana, sans-serif;
|
font-family: "Verdana", Verdana, sans-serif;
|
||||||
}
|
}
|
||||||
|
|
||||||
#buttonwrap {
|
#buttonwrap {
|
||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
.button {
|
.button {
|
||||||
background-color: #4CAF50; /* Green */
|
background-color: #4CAF50; /* Green */
|
||||||
border: none;
|
border: none;
|
||||||
color: white;
|
color: white;
|
||||||
padding: 15px 32px;
|
padding: 15px 32px;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
font-size: 16px;
|
font-size: 16px;
|
||||||
width: 70%;
|
width: 70%;
|
||||||
height: 100px;
|
height: 100px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.button[disabled=disabled] {
|
.button.stop {
|
||||||
background-color: gray;
|
background-color: red;
|
||||||
}
|
}
|
||||||
|
|
||||||
#hintbutton {
|
#hintbutton {
|
||||||
|
|
|
@ -12,10 +12,11 @@ defmodule UiWeb.MorseLive do
|
||||||
{:ok, assign(socket, default_assigns(ip))}
|
{:ok, assign(socket, default_assigns(ip))}
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_event("start_morse", _value, %{assigns: %{ip: ip}} = socket) do
|
def handle_event("toggle_morse", _value, %{assigns: %{ip: ip}} = socket) do
|
||||||
if :ok == Morse.Server.start_morse() do
|
if Morse.Server.in_progress?() do
|
||||||
spawn fn -> Ui.TelegramBot.message("#{ip} pressed the button!") end
|
spawn fn -> Ui.TelegramBot.message("#{ip} pressed the button!") end
|
||||||
end
|
end
|
||||||
|
Morse.Server.toggle_morse()
|
||||||
|
|
||||||
{:noreply, socket}
|
{:noreply, socket}
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,9 +4,9 @@
|
||||||
|
|
||||||
<div id="buttonwrap">
|
<div id="buttonwrap">
|
||||||
<%= if @in_progress? do %>
|
<%= if @in_progress? do %>
|
||||||
<button phx-click="start_morse" class="button" <%= if @in_progress?, do: ~s(disabled=disabled) %>>In progress...</button>
|
<button phx-click="toggle_morse" class="button stop">Stop</button>
|
||||||
<% else %>
|
<% else %>
|
||||||
<button phx-click="start_morse" class="button">Start</button>
|
<button phx-click="toggle_morse" class="button">Start</button>
|
||||||
<% end %>
|
<% end %>
|
||||||
<div id="response-block">
|
<div id="response-block">
|
||||||
<h2>Progress:</h2>
|
<h2>Progress:</h2>
|
||||||
|
|
Loading…
Reference in a new issue