diff --git a/lib/nerves_hub_web/channels/device_channel.ex b/lib/nerves_hub_web/channels/device_channel.ex index 00eff4184..5dae67a06 100644 --- a/lib/nerves_hub_web/channels/device_channel.ex +++ b/lib/nerves_hub_web/channels/device_channel.ex @@ -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 diff --git a/lib/nerves_hub_web/channels/device_socket.ex b/lib/nerves_hub_web/channels/device_socket.ex index 60066a061..f9d78fdf7 100644 --- a/lib/nerves_hub_web/channels/device_socket.ex +++ b/lib/nerves_hub_web/channels/device_socket.ex @@ -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 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..397ac53ff --- /dev/null +++ b/lib/nerves_hub_web/channels/features_channel.ex @@ -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