Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add device features support #1479

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/nerves_hub_web/channels/device_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ defmodule NervesHubWeb.DeviceChannel do

send(self(), :device_registation)

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

socket =
socket
|> assign(:device, device)
Expand Down Expand Up @@ -360,14 +365,9 @@ defmodule NervesHubWeb.DeviceChannel do
end
end

def handle_in("location:update", location, %{assigns: %{device: device}} = socket) do
metadata = Map.put(device.connection_metadata, "location", location)

{:ok, device} = Devices.update_device(device, %{connection_metadata: metadata})

device_internal_broadcast!(device, "location:updated", location)

{:reply, :ok, assign(socket, :device, device)}
def handle_in("location:update", location, socket) do
# Backwards compatibility for Geo feature
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the only devices that use Geo are ours from testing and we could absolutely break that contract and fix those devices.

The functionality has been added in nerves_hub_web but the other end only exists in branches of nerves_hub_link extension stuff.

NervesHubWeb.FeaturesChannel.handle_in("geo:location:update", location, socket)
end

def handle_in("connection_types", %{"values" => types}, %{assigns: %{device: device}} = socket) do
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 @@ -12,6 +12,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
52 changes: 52 additions & 0 deletions lib/nerves_hub_web/channels/features_channel.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
defmodule NervesHubWeb.FeaturesChannel do
use Phoenix.Channel

alias Phoenix.Socket.Broadcast
alias NervesHub.Devices

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

topic = "device:#{socket.assigns.device.id}:features"
NervesHubWeb.DeviceEndpoint.subscribe(topic)

{:ok, attach_list, socket}
end

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

@impl Phoenix.Channel
def handle_in("geo:location:update", location, %{assigns: %{device: device}} = socket) do
metadata = Map.put(device.connection_metadata, "location", location)

{:ok, device} = Devices.update_device(device, %{connection_metadata: metadata})

_ =
NervesHubWeb.DeviceEndpoint.broadcast(
"device:#{device.identifier}:internal",
"location:updated",
location
)

{:noreply, assign(socket, :device, device)}
end

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
Loading