Skip to content

Commit

Permalink
Add configuration for handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
mattludwigs committed Jul 15, 2020
1 parent 51f3b44 commit 467d7d5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion lib/grizzly/firmware_updates/firmware_update_supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
16 changes: 15 additions & 1 deletion lib/grizzly/inclusions/inclusion_runner_supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 467d7d5

Please sign in to comment.