Split Morse.Server into Application and Server.
Rename Morse.Signaler to Worker.
This commit is contained in:
parent
375a58ef14
commit
3432ef75b2
5 changed files with 19 additions and 13 deletions
12
morse/lib/morse/application.ex
Normal file
12
morse/lib/morse/application.ex
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
defmodule Morse.Application do
|
||||||
|
use Application
|
||||||
|
|
||||||
|
def start(_type, _args) do
|
||||||
|
children = [
|
||||||
|
{Morse.Server, nil}
|
||||||
|
]
|
||||||
|
|
||||||
|
opts = [strategy: :one_for_one, name: Morse.Supervisor]
|
||||||
|
Supervisor.start_link(children, opts)
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,13 +1,7 @@
|
||||||
defmodule MorseServer do
|
defmodule Morse.Server do
|
||||||
use GenServer
|
use GenServer
|
||||||
use Application
|
|
||||||
|
|
||||||
@impl true
|
def start_link(_) do
|
||||||
def start(_type, _args) do
|
|
||||||
start_link()
|
|
||||||
end
|
|
||||||
|
|
||||||
def start_link do
|
|
||||||
GenServer.start_link(__MODULE__, {nil, 0}, name: __MODULE__)
|
GenServer.start_link(__MODULE__, {nil, 0}, name: __MODULE__)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -28,7 +22,7 @@ defmodule MorseServer do
|
||||||
def handle_call(:start, _from, {pid, _progress} = state) do
|
def handle_call(:start, _from, {pid, _progress} = state) do
|
||||||
cond do
|
cond do
|
||||||
pid == nil or not Process.alive?(pid) ->
|
pid == nil or not Process.alive?(pid) ->
|
||||||
pid = spawn(&MorseSignaler.signal/0)
|
pid = spawn(&Morse.Worker.signal/0)
|
||||||
{:reply, :ok, {pid, 0}}
|
{:reply, :ok, {pid, 0}}
|
||||||
|
|
||||||
true ->
|
true ->
|
|
@ -1,4 +1,4 @@
|
||||||
defmodule MorseSignaler do
|
defmodule Morse.Worker do
|
||||||
alias Circuits.GPIO
|
alias Circuits.GPIO
|
||||||
|
|
||||||
@moduledoc """
|
@moduledoc """
|
||||||
|
@ -29,7 +29,7 @@ defmodule MorseSignaler do
|
||||||
# Update progress for clients, and signals the rest of the sentence.
|
# Update progress for clients, and signals the rest of the sentence.
|
||||||
defp update_progress(gpio, symbols) do
|
defp update_progress(gpio, symbols) do
|
||||||
100 - length(symbols) / String.length(secret_code()) * 100
|
100 - length(symbols) / String.length(secret_code()) * 100
|
||||||
|> MorseServer.update_progress()
|
|> Morse.Server.update_progress()
|
||||||
if symbols != [] do
|
if symbols != [] do
|
||||||
signal_sentence(gpio, symbols)
|
signal_sentence(gpio, symbols)
|
||||||
end
|
end
|
|
@ -16,7 +16,7 @@ defmodule Morse.MixProject do
|
||||||
[
|
[
|
||||||
extra_applications: [:logger],
|
extra_applications: [:logger],
|
||||||
env: [morse_message: "...---...", relay_pin: 17],
|
env: [morse_message: "...---...", relay_pin: 17],
|
||||||
mod: {MorseServer, []}
|
mod: {Morse.Application, []}
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ defmodule UiWeb.PageController do
|
||||||
|
|
||||||
def start(conn, _params) do
|
def start(conn, _params) do
|
||||||
response =
|
response =
|
||||||
case MorseServer.start_morse() do
|
case Morse.Server.start_morse() do
|
||||||
:ok -> "Started."
|
:ok -> "Started."
|
||||||
{:error, :already_started} -> "The process is still in progress..."
|
{:error, :already_started} -> "The process is still in progress..."
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue