From bd530d49fb4629c89954399dec34f0f580d114aa Mon Sep 17 00:00:00 2001 From: Jon Carstens Date: Thu, 12 Aug 2021 13:35:50 -0600 Subject: [PATCH] add NervesHubLink.reconnect/0 --- lib/nerves_hub_link.ex | 21 +++++++++++++++++++++ lib/nerves_hub_link/application.ex | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/nerves_hub_link.ex b/lib/nerves_hub_link.ex index f01683d..18ef091 100644 --- a/lib/nerves_hub_link.ex +++ b/lib/nerves_hub_link.ex @@ -1,4 +1,7 @@ defmodule NervesHubLink do + alias NervesHubLink.Supervisor, as: NHLSupervisor + alias NervesHubLink.{ConsoleChannel, DeviceChannel, Socket} + @doc """ Checks if the device is connected to the NervesHub channel. """ @@ -18,4 +21,22 @@ defmodule NervesHubLink do """ @spec status :: NervesHubLinkCommon.UpdateManager.State.status() defdelegate status(), to: NervesHubLinkCommon.UpdateManager + + @doc """ + Restart the socket and device channel + """ + @spec reconnect() :: :ok + def reconnect() do + # Stop the socket last + _ = Supervisor.terminate_child(NHLSupervisor, ConsoleChannel) + _ = Supervisor.terminate_child(NHLSupervisor, DeviceChannel) + _ = Supervisor.terminate_child(NHLSupervisor, Socket) + + # Start the socket first + _ = Supervisor.restart_child(NHLSupervisor, Socket) + _ = Supervisor.restart_child(NHLSupervisor, ConsoleChannel) + _ = Supervisor.restart_child(NHLSupervisor, DeviceChannel) + + :ok + end end diff --git a/lib/nerves_hub_link/application.ex b/lib/nerves_hub_link/application.ex index cc44c07..8921cc8 100644 --- a/lib/nerves_hub_link/application.ex +++ b/lib/nerves_hub_link/application.ex @@ -27,7 +27,7 @@ defmodule NervesHubLink.Application do [ {UpdateManager, fwup_config}, Connection, - {PhoenixClient.Socket, {config.socket, [name: Socket]}}, + {PhoenixClient.Socket, {config.socket, [id: Socket, name: Socket]}}, {DeviceChannel, [socket: Socket, params: config.params]} ] |> add_console_child(config)