Implement GenServer and Application for morse module.
This commit is contained in:
parent
ef38b81dd6
commit
0c989c06d0
4 changed files with 61 additions and 33 deletions
|
@ -2,37 +2,24 @@ defmodule UiWeb.PageController do
|
|||
use UiWeb, :controller
|
||||
|
||||
def index(conn, _params) do
|
||||
conn
|
||||
|> send_resp(201, "")
|
||||
conn |> send_resp(201, "")
|
||||
end
|
||||
|
||||
def instructions(conn, _params) do
|
||||
conn
|
||||
|> render(:instructions)
|
||||
conn |> render(:instructions)
|
||||
end
|
||||
|
||||
def morse(conn, _params) do
|
||||
conn
|
||||
|> render(:morse)
|
||||
conn |> render(:morse)
|
||||
end
|
||||
|
||||
def start(conn, _params) do
|
||||
now = System.system_time(:second)
|
||||
response =
|
||||
case MorseServer.start_morse() do
|
||||
:ok -> "Started."
|
||||
{:error, :already_started} -> "The process is still in progress..."
|
||||
end
|
||||
|
||||
case get_start_time() do
|
||||
start_time when start_time + 35 <= now ->
|
||||
System.put_env("MORSE_START_TIME", Integer.to_string(now))
|
||||
Morse.signal()
|
||||
text(conn, "Done.")
|
||||
_ ->
|
||||
text(conn, "It is still in progress...")
|
||||
end
|
||||
end
|
||||
|
||||
defp get_start_time() do
|
||||
case System.get_env("MORSE_START_TIME") do
|
||||
nil -> 0
|
||||
start_time -> String.to_integer(start_time)
|
||||
end
|
||||
conn |> text(response)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue