Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/jackalope/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ defmodule Jackalope.Handler do
@type topic_levels :: [String.t()]
@type payload :: term()
@type last_will :: [topic: topic, payload: payload, qos: non_neg_integer()]
@type socket :: any()
@type server :: atom()

@doc """
Called when the MQTT connection changes status
Expand All @@ -25,6 +27,14 @@ defmodule Jackalope.Handler do
"""
@callback connection(status :: :up | :down) :: any()

@doc """
Called when connected to an MQTT server

This can be used to inform other parts of the system about the type
of connection and the connected socket.
"""
@callback connected(server :: server(), socket()) :: any()

@doc """
Produces the last will message for the current connection, or nil if the last will in the connection options is to be used
Example: [topic: hub_serial_number/message", payload: %{code: "going_down", msg: "Last will message"}, qos: 1]
Expand Down
5 changes: 5 additions & 0 deletions lib/jackalope/handler/logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ defmodule Jackalope.Handler.Logger do
Logger.info("Connection status is: #{inspect(status)}")
end

@impl Jackalope.Handler
def connected(server, socket) do
Logger.info("Connected via #{inspect(server)} socket #{inspect(socket)}")
end

@impl Jackalope.Handler
def subscription(status, topic_filter) do
Logger.info("Subscription change: #{inspect(topic_filter)} is #{inspect(status)}")
Expand Down
1 change: 1 addition & 0 deletions lib/jackalope/tortoise_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ defmodule Jackalope.TortoiseClient do
case Tortoise311.Connection.connection(state.client_id, active: true) do
{:ok, _connection} ->
{:ok, _} = Tortoise311.Events.register(state.client_id, :status)
{:ok, _} = Tortoise311.Events.register(state.client_id, :connection)
{:noreply, state}

{:error, :timeout} ->
Expand Down
15 changes: 15 additions & 0 deletions lib/jackalope/tortoise_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ defmodule Jackalope.TortoiseHandler do
{:ok, state}
end

@impl Tortoise311.Handler
def connected(transport_module, socket, %State{} = state) do
if function_exported?(state.handler, :connected, 2) do
server =
case transport_module do
Tortoise311.Transport.SSL -> :ssl
Tortoise311.Transport.Tcp -> :tcp
end

_ignored = state.handler.connected(server, socket)
end

{:ok, state}
end

@impl Tortoise311.Handler
def subscription(status, topic_filter, %State{} = state) when status in [:up, :down] do
if function_exported?(state.handler, :subscription, 2) do
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ defmodule Jackalope.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps() do
[
{:tortoise311, "~> 0.12"},
# {:tortoise311, git: "git@github.com:smartrent/tortoise311.git", branch: "main"},
{:dialyxir, "~> 1.4", only: [:test, :dev], runtime: false},
# {:tortoise311, "~> 0.12"},
{:tortoise311, git: "git@github.com:smartrent/tortoise311.git", branch: "connection_monitor"},
{:credo, "~> 1.4", only: [:dev, :test], runtime: false},
{:ex_doc, "~> 0.22", only: :docs, runtime: false}
]
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"makeup_erlang": {:hex, :makeup_erlang, "1.0.0", "6f0eff9c9c489f26b69b61440bf1b238d95badae49adac77973cbacae87e3c2e", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "ea7a9307de9d1548d2a72d299058d1fd2339e3d398560a0e46c27dab4891e4d2"},
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
"tortoise311": {:hex, :tortoise311, "0.12.0", "05c1f78da9aa3f7563ad66c71d2cb7904890797437078e86173d65d2a2d06f64", [:mix], [{:gen_state_machine, "~> 2.0 or ~> 3.0", [hex: :gen_state_machine, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "b8bd5b1b603f75355d02c655e496a2cbb59c754e45014bcb86968899297197bb"},
"tortoise311": {:git, "git@github.com:smartrent/tortoise311.git", "24f13ab51e130e08ec89b993e7c22621a04e398a", [branch: "connection_monitor"]},
}