From e14326c367edb28febfb1066648f5c0fd99c2fb2 Mon Sep 17 00:00:00 2001 From: Jon Carstens Date: Mon, 2 Sep 2024 12:24:35 -0600 Subject: [PATCH] Add example for use with Geo feature --- lib/nerves_hub_web/channels/device_channel.ex | 16 +++------------- lib/nerves_hub_web/channels/features_channel.ex | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/nerves_hub_web/channels/device_channel.ex b/lib/nerves_hub_web/channels/device_channel.ex index 5dae67a06..0b8bc2287 100644 --- a/lib/nerves_hub_web/channels/device_channel.ex +++ b/lib/nerves_hub_web/channels/device_channel.ex @@ -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 diff --git a/lib/nerves_hub_web/channels/features_channel.ex b/lib/nerves_hub_web/channels/features_channel.ex index e60009387..12a1cbe59 100644 --- a/lib/nerves_hub_web/channels/features_channel.ex +++ b/lib/nerves_hub_web/channels/features_channel.ex @@ -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 @@ -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}