Skip to content

Commit

Permalink
Add device features support
Browse files Browse the repository at this point in the history
Start of device features (previously "extensions") which allow for
specialized features on device to report data and interactions
safely outside the update mechanism.
  • Loading branch information
jjcarstens committed Sep 1, 2024
1 parent 9cb7633 commit 61b9720
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/nerves_hub_web/channels/device_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ defmodule NervesHubWeb.DeviceChannel do
schedule_health_check()
end

# device.allow_features?
if true do
push(socket, "features:get", %{})
end

{:noreply, socket}
end

Expand Down
1 change: 1 addition & 0 deletions lib/nerves_hub_web/channels/device_socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule NervesHubWeb.DeviceSocket do

channel("console", NervesHubWeb.ConsoleChannel)
channel("device", NervesHubWeb.DeviceChannel)
channel("features", NervesHubWeb.FeaturesChannel)

# Default 90 seconds max age for the signature
@default_max_hmac_age 90
Expand Down
36 changes: 36 additions & 0 deletions lib/nerves_hub_web/channels/features_channel.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule NervesHubWeb.FeaturesChannel do
use Phoenix.Channel

alias Phoenix.Socket.Broadcast

@impl Phoenix.Channel
def join("features", payload, socket) do
enable_list =
for {feature, ver} <- payload, into: %{} do
{feature, allowed?(feature, ver)}
end

topic = "device:#{socket.assigns.device.id}:features"
Phoenix.PubSub.subscribe(NervesHub.PubSub, topic)

{:ok, enable_list, socket}
end

defp allowed?(feature, ver) do
# TODO: Some conditions for allow/disallow feature
true
end

@impl Phoenix.Channel
def handle_in(event, payload, socket) do
dbg({event, payload})
{:noreply, socket}
end

@impl Phoenix.Channel
# TODO: Get specific on messages passed to device
def handle_info(%Broadcast{event: event, payload: payload}, socket) do
push(socket, event, payload)
{:noreply, socket}
end
end

0 comments on commit 61b9720

Please sign in to comment.