-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
defmodule SmartHomeFirmware.FeatureManager do | ||
@moduledoc """ | ||
Manages what features are currently enabled and which ones aren't. | ||
""" | ||
require Logger | ||
|
||
# Feature flags allow certain processes to be turned on and off. | ||
# Here we define the flags and associated modules | ||
@feature_flags [ | ||
lock: SmartHomeFirmware.Lock, | ||
display: SmartHomeFirmware.NotImplemented | ||
] | ||
|
||
use GenServer | ||
|
||
alias SmartHomeFirmware.FeatureSupervisor | ||
|
||
def start_link(opts) do | ||
GenServer.start_link(__MODULE__, opts, name: __MODULE__) | ||
end | ||
|
||
def init(_opts) do | ||
SmartHomeFirmware.State.subscribe(:self) | ||
{:ok, %{}} | ||
end | ||
|
||
def handle_info({:store_update, :self, %{feature_flags: flags}}, state) do | ||
# flags are stored as strings, we need to convert them to atoms | ||
flags | ||
|> Enum.map(&String.to_existing_atom(&1)) | ||
|> enable_features() | ||
|
||
{:noreply, state} | ||
end | ||
|
||
@doc """ | ||
enable_features takes a list of features and enables them on the firmware. | ||
E.g. [:lock, :display] | ||
""" | ||
def enable_features(features) do | ||
Logger.info(inspect features) | ||
features | ||
|> Enum.map(&Keyword.get(@feature_flags, &1)) | ||
|> Enum.each(&DynamicSupervisor.start_child(FeatureSupervisor, &1)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule SmartHomeFirmware.FeatureSupervisor do | ||
use DynamicSupervisor | ||
|
||
|
||
@moduledoc """ | ||
This supervisor allows us to dynamically toggle on and off features of | ||
the firmware. | ||
""" | ||
|
||
def start_link(opts) do | ||
DynamicSupervisor.start_link(__MODULE__, opts, name: __MODULE__) | ||
end | ||
|
||
def init(_opts) do | ||
DynamicSupervisor.init(strategy: :one_for_one) | ||
end | ||
|
||
end |