Skip to content

Commit

Permalink
Add example for use with Geo feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jjcarstens committed Sep 2, 2024
1 parent b5c5b53 commit e14326c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
16 changes: 3 additions & 13 deletions lib/nerves_hub_web/channels/device_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -458,19 +458,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})

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

{:reply, :ok, assign(socket, :device, device)}
def handle_in("location:update", location, socket) do
# Backwards compatibility for Geo feature
NervesHubWeb.FeaturesChannel.handle_in("geo:location:update", location, socket)
end

def handle_in("connection_types", %{"values" => types}, %{assigns: %{device: device}} = socket) do
Expand Down
16 changes: 16 additions & 0 deletions lib/nerves_hub_web/channels/features_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule NervesHubWeb.FeaturesChannel do
use Phoenix.Channel

alias Phoenix.Socket.Broadcast
alias NervesHub.Devices

@impl Phoenix.Channel
def join("features", payload, socket) do
Expand All @@ -22,6 +23,21 @@ defmodule NervesHubWeb.FeaturesChannel do
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}
Expand Down

0 comments on commit e14326c

Please sign in to comment.