diff --git a/lib/nerves_hub_web/channels/device_channel.ex b/lib/nerves_hub_web/channels/device_channel.ex index 22bf0fec2..297a3012f 100644 --- a/lib/nerves_hub_web/channels/device_channel.ex +++ b/lib/nerves_hub_web/channels/device_channel.ex @@ -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) @@ -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 + 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/device_socket.ex b/lib/nerves_hub_web/channels/device_socket.ex index 17b7291a7..35c0983f6 100644 --- a/lib/nerves_hub_web/channels/device_socket.ex +++ b/lib/nerves_hub_web/channels/device_socket.ex @@ -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 diff --git a/lib/nerves_hub_web/channels/features_channel.ex b/lib/nerves_hub_web/channels/features_channel.ex new file mode 100644 index 000000000..12a1cbe59 --- /dev/null +++ b/lib/nerves_hub_web/channels/features_channel.ex @@ -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