Skip to content

Commit

Permalink
Added feature flags
Browse files Browse the repository at this point in the history
  • Loading branch information
th0mas committed Jul 20, 2020
1 parent a9b34aa commit 1fc3544
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/smart_home_firmware/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ defmodule SmartHomeFirmware.Application do
# Don't add stuff here that needs a Network connection!
# Add it in `NetworkSupervisor`
{SmartHomeFirmware.State, name: SmartHomeFirmware},
SmartHomeFirmware.Lock,
SmartHomeFirmware.FeatureSupervisor,
SmartHomeFirmware.FeatureManager,
SmartHomeFirmware.HubClient
] ++ children(target())

Expand Down
8 changes: 7 additions & 1 deletion lib/smart_home_firmware/client/hub_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,16 @@ defmodule SmartHomeFirmware.HubClient do
end

defp handle_handshake_resp(body) do
# TODO: Adapt backend to modularize this more -
# I don't like having to split this out here.
SmartHomeFirmware.State.put(:lock, %{
mode: body["mode"],
uuid: body["uuid"],
name: body["name"]
})

SmartHomeFirmware.State.put(:self, %{
name: body["name"],
feature_flags: body["feature_flags"]
})
end

Expand Down
47 changes: 47 additions & 0 deletions lib/smart_home_firmware/feature_manager.ex
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
18 changes: 18 additions & 0 deletions lib/smart_home_firmware/feature_supervisor.ex
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

0 comments on commit 1fc3544

Please sign in to comment.