From 3432ef75b2247cdfd58f40c4cb132a0ff8a241ec Mon Sep 17 00:00:00 2001 From: Pim Kunis Date: Thu, 1 Aug 2019 12:21:32 +0200 Subject: [PATCH] Split Morse.Server into Application and Server. Rename Morse.Signaler to Worker. --- morse/lib/morse/application.ex | 12 ++++++++++++ morse/lib/{morse_server.ex => morse/server.ex} | 12 +++--------- morse/lib/{morse_signaler.ex => morse/worker.ex} | 4 ++-- morse/mix.exs | 2 +- ui/lib/ui_web/controllers/page_controller.ex | 2 +- 5 files changed, 19 insertions(+), 13 deletions(-) create mode 100644 morse/lib/morse/application.ex rename morse/lib/{morse_server.ex => morse/server.ex} (82%) rename morse/lib/{morse_signaler.ex => morse/worker.ex} (96%) diff --git a/morse/lib/morse/application.ex b/morse/lib/morse/application.ex new file mode 100644 index 0000000..1153139 --- /dev/null +++ b/morse/lib/morse/application.ex @@ -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 diff --git a/morse/lib/morse_server.ex b/morse/lib/morse/server.ex similarity index 82% rename from morse/lib/morse_server.ex rename to morse/lib/morse/server.ex index 8965cdf..10b768e 100644 --- a/morse/lib/morse_server.ex +++ b/morse/lib/morse/server.ex @@ -1,13 +1,7 @@ -defmodule MorseServer do +defmodule Morse.Server do use GenServer - use Application - @impl true - def start(_type, _args) do - start_link() - end - - def start_link do + def start_link(_) do GenServer.start_link(__MODULE__, {nil, 0}, name: __MODULE__) end @@ -28,7 +22,7 @@ defmodule MorseServer do def handle_call(:start, _from, {pid, _progress} = state) do cond do pid == nil or not Process.alive?(pid) -> - pid = spawn(&MorseSignaler.signal/0) + pid = spawn(&Morse.Worker.signal/0) {:reply, :ok, {pid, 0}} true -> diff --git a/morse/lib/morse_signaler.ex b/morse/lib/morse/worker.ex similarity index 96% rename from morse/lib/morse_signaler.ex rename to morse/lib/morse/worker.ex index cfaca4d..5b7d09b 100644 --- a/morse/lib/morse_signaler.ex +++ b/morse/lib/morse/worker.ex @@ -1,4 +1,4 @@ -defmodule MorseSignaler do +defmodule Morse.Worker do alias Circuits.GPIO @moduledoc """ @@ -29,7 +29,7 @@ defmodule MorseSignaler do # Update progress for clients, and signals the rest of the sentence. defp update_progress(gpio, symbols) do 100 - length(symbols) / String.length(secret_code()) * 100 - |> MorseServer.update_progress() + |> Morse.Server.update_progress() if symbols != [] do signal_sentence(gpio, symbols) end diff --git a/morse/mix.exs b/morse/mix.exs index e59828b..d29be8a 100644 --- a/morse/mix.exs +++ b/morse/mix.exs @@ -16,7 +16,7 @@ defmodule Morse.MixProject do [ extra_applications: [:logger], env: [morse_message: "...---...", relay_pin: 17], - mod: {MorseServer, []} + mod: {Morse.Application, []} ] end diff --git a/ui/lib/ui_web/controllers/page_controller.ex b/ui/lib/ui_web/controllers/page_controller.ex index 6bb88f2..f38cf3b 100644 --- a/ui/lib/ui_web/controllers/page_controller.ex +++ b/ui/lib/ui_web/controllers/page_controller.ex @@ -15,7 +15,7 @@ defmodule UiWeb.PageController do def start(conn, _params) do response = - case MorseServer.start_morse() do + case Morse.Server.start_morse() do :ok -> "Started." {:error, :already_started} -> "The process is still in progress..." end