From 467d7d5cbcd0d7bf06a6e4ea4fd872f594dd7763 Mon Sep 17 00:00:00 2001 From: Matt Ludwigs Date: Wed, 15 Jul 2020 11:22:18 -0700 Subject: [PATCH] Add configuration for handlers --- .circleci/config.yml | 4 +-- README.md | 29 +++++++++++++++++++ .../firmware_update_supervisor.ex | 16 +++++++++- .../inclusions/inclusion_runner_supervisor.ex | 16 +++++++++- mix.exs | 2 +- 5 files changed, 62 insertions(+), 5 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index a8eed243..6ffbe857 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -43,7 +43,7 @@ jobs: - v1-mix-cache-{{ checksum "mix.lock" }} - run: mix deps.get - run: mix compile --warnings-as-errors - - run: mix test --include inclusion --include integration + - run: mix test --include inclusion --include integration --include firmware_update - run: mix format --check-formatted - run: mix docs - run: mix hex.build @@ -72,7 +72,7 @@ jobs: - v1-mix-cache-{{ checksum "mix.lock" }} - run: mix deps.get - run: mix compile --warnings-as-errors - - run: mix test --include inclusion --include integration + - run: mix test --include inclusion --include integration --include firmware_update - run: mix format --check-formatted - run: MIX_ENV=docs mix docs - run: MIX_ENV=docs mix hex.build diff --git a/README.md b/README.md index 47cad2b4..7a81c500 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,35 @@ config :grizzly, For more information see `Grizzly.Runtime` module +## Handlers + +Grizzly manages the low level details of around things that require more than +just sending one off commands like including/excluding a device and firmware +updates. + +However, sometimes your higher level application will have some logic that you +will want to preform during the process. So, there are handler behaviours for +this types of processes. You can read more about those behaviours in their +respective modules. + +You can provide the handler for any such process that accepts one either at +runtime or through mix configuration options. The configuration option looks +like: + +```elixir +config :grizzly, + handlers: %{ + firmware_update: MyFirmwareUpdateHandler, + inclusion: MyInclusionHandler + } +``` + +If you do not pass any handler at runtime or configure any for the process will +send messages to the calling process. + +You can override the configured handlers at runtime by passing in the `:handler` +option to these processes. + ## Z-Wave Bridge Configuration Grizzly defers the low-level Z-Wave protocol handling to a combination of third diff --git a/lib/grizzly/firmware_updates/firmware_update_supervisor.ex b/lib/grizzly/firmware_updates/firmware_update_supervisor.ex index cf9c67c8..6952f884 100644 --- a/lib/grizzly/firmware_updates/firmware_update_supervisor.ex +++ b/lib/grizzly/firmware_updates/firmware_update_supervisor.ex @@ -11,7 +11,7 @@ defmodule Grizzly.FirmwareUpdates.FirmwareUpdateRunnerSupervisor do @spec start_runner([FirmwareUpdates.opt()]) :: DynamicSupervisor.on_start_child() def start_runner(opts \\ []) do - opts = Keyword.merge([handler: self()], opts) + opts = ensure_handler(opts) child_spec = FirmwareUpdateRunner.child_spec(opts) DynamicSupervisor.start_child(__MODULE__, child_spec) end @@ -21,4 +21,18 @@ defmodule Grizzly.FirmwareUpdates.FirmwareUpdateRunnerSupervisor do # Only one firmware update runner can be running at a time DynamicSupervisor.init(strategy: :one_for_one, max_children: 1) end + + defp ensure_handler(opts) do + Keyword.put_new_lazy(opts, :handler, &get_firmware_update_handler/0) + end + + defp get_firmware_update_handler() do + case Application.get_env(:girzzly, :handlers) do + nil -> + self() + + handlers -> + Map.get(handlers, :firmware_update, self()) + end + end end diff --git a/lib/grizzly/inclusions/inclusion_runner_supervisor.ex b/lib/grizzly/inclusions/inclusion_runner_supervisor.ex index cc5575ed..76c42220 100644 --- a/lib/grizzly/inclusions/inclusion_runner_supervisor.ex +++ b/lib/grizzly/inclusions/inclusion_runner_supervisor.ex @@ -11,7 +11,7 @@ defmodule Grizzly.Inclusions.InclusionRunnerSupervisor do @spec start_runner([Inclusions.opt()]) :: DynamicSupervisor.on_start_child() def start_runner(opts \\ []) do - opts = Keyword.merge([handler: self()], opts) + opts = ensure_handler(opts) child_spec = InclusionRunner.child_spec(opts) DynamicSupervisor.start_child(__MODULE__, child_spec) end @@ -21,4 +21,18 @@ defmodule Grizzly.Inclusions.InclusionRunnerSupervisor do # Only one inclusion runner can be running at a time DynamicSupervisor.init(strategy: :one_for_one, max_children: 1) end + + defp ensure_handler(opts) do + Keyword.put_new_lazy(opts, :handler, &get_inclusion_handler/0) + end + + defp get_inclusion_handler() do + case Application.get_env(:girzzly, :handlers) do + nil -> + self() + + handlers -> + Map.get(handlers, :inclusion, self()) + end + end end diff --git a/mix.exs b/mix.exs index 88a0d132..37f2ebd2 100644 --- a/mix.exs +++ b/mix.exs @@ -71,7 +71,7 @@ defmodule Grizzly.MixProject do defp aliases() do [ - test: ["test --exclude integration --exclude inclusion"] + test: ["test --exclude integration --exclude inclusion --exclude firmware_update"] ] end end