Skip to content

Commit

Permalink
Send connection types to NervesHub
Browse files Browse the repository at this point in the history
Adds a callback to let the device have a hook for knowing that its
connected successfully to nerves hub. This allows sending connection
types up the socket after fully connected. Connection types are
"ethernet", "wifi", "cellular".
  • Loading branch information
oestrich committed Mar 21, 2023
1 parent 4c128a1 commit 66a85a1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/nerves_hub_link/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ defmodule NervesHubLink.Client do
"""
@callback handle_error(any()) :: :ok

@doc """
Callback to perform an action when the device has connected to NervesHub.
"""
@callback connected() :: :ok

@doc """
Callback to identify the device from NervesHub.
"""
Expand Down Expand Up @@ -139,6 +144,13 @@ defmodule NervesHubLink.Client do
end
end

@doc """
This function is called internally by NervesHubLink to notify clients of connecting to NervesHub.
"""
def connected() do
apply_wrap(mod(), :connected, [])
end

@doc """
This function is called internally by NervesHubLink to identify a device.
"""
Expand Down
3 changes: 3 additions & 0 deletions lib/nerves_hub_link/client/default.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ defmodule NervesHubLink.Client.Default do
Logger.warn("[NervesHubLink] error: #{inspect(error)}")
end

@impl NervesHubLink.Client
def connected(), do: :ok

@impl NervesHubLink.Client
def identify() do
Logger.info("[NervesHubLink] identifying")
Expand Down
10 changes: 10 additions & 0 deletions lib/nerves_hub_link/socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ defmodule NervesHubLink.Socket do
GenServer.cast(__MODULE__, {:send_update_status, status})
end

def send_connection_types(types) do
GenServer.cast(__MODULE__, {:send_connection_types, types})
end

def check_connection(type) do
GenServer.call(__MODULE__, {:check_connection, type})
end
Expand Down Expand Up @@ -88,6 +92,7 @@ defmodule NervesHubLink.Socket do
def handle_join(@device_topic, reply, socket) do
Logger.debug("[#{inspect(__MODULE__)}] Joined Device channel")
NervesHubLink.Connection.connected()
NervesHubLink.Client.connected()
_ = handle_join_reply(reply)
{:ok, socket}
end
Expand Down Expand Up @@ -127,6 +132,11 @@ defmodule NervesHubLink.Socket do
{:noreply, socket}
end

def handle_cast({:send_connection_types, types}, socket) do
_ = push(socket, @device_topic, "connection_types", %{value: types})
{:noreply, socket}
end

@impl Slipstream
##
# Device API messages
Expand Down

0 comments on commit 66a85a1

Please sign in to comment.