Made a more proper supervisor tree for the app
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
defmodule NeonVertigoService.Mqtt do
|
||||
use GenServer
|
||||
|
||||
def start_link(_) do
|
||||
GenServer.start_link(__MODULE__, {})
|
||||
end
|
||||
|
||||
def init(_) do
|
||||
mqtt_config = Application.fetch_env!(:neon_vertigo_service, :mqtt_params)
|
||||
{:ok, mqtt_pid} = :emqtt.start_link([{:owner, self()} | mqtt_config])
|
||||
{:ok, _mqtt_props} = :emqtt.connect(mqtt_pid)
|
||||
Process.link(mqtt_pid)
|
||||
|
||||
:emqtt.subscribe(mqtt_pid, %{}, [{"$share/neon-vertigo-service/test/+", [{:qos, 1}]}])
|
||||
|
||||
{:ok, {mqtt_pid}}
|
||||
end
|
||||
|
||||
# Handling MQTT messages
|
||||
def handle_info({:publish, msg}, state) do
|
||||
IO.puts("Received message")
|
||||
IO.inspect(msg)
|
||||
{:noreply, state}
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,35 @@
|
||||
defmodule NeonVertigoService.Mqtt.Service do
|
||||
use GenServer
|
||||
|
||||
def start_link(_) do
|
||||
GenServer.start_link(__MODULE__, [], name: __MODULE__)
|
||||
end
|
||||
|
||||
def init(_) do
|
||||
{:ok, {}, {:continue, :init_mqtt}}
|
||||
end
|
||||
|
||||
# Callbacks
|
||||
def handle_continue(:init_mqtt, {}) do
|
||||
mqtt_config = Application.fetch_env!(:neon_vertigo_service, :mqtt_params)
|
||||
{:ok, mqtt_pid} = Supervisor.start_child(
|
||||
NeonVertigoService.Mqtt.Supervisor,
|
||||
%{
|
||||
id: :neon_vertigo_emqtt,
|
||||
start: {:emqtt, :start_link, [[owner: self(), name: :neon_vertigo_service_emqtt] ++ mqtt_config]}
|
||||
}
|
||||
)
|
||||
{:ok, _mqtt_props} = :emqtt.connect(mqtt_pid)
|
||||
|
||||
:emqtt.subscribe(mqtt_pid, %{}, [{"$share/neon-vertigo-service/test/+", [{:qos, 1}]}])
|
||||
|
||||
{:noreply, {mqtt_pid}}
|
||||
end
|
||||
|
||||
# Handling MQTT messages
|
||||
def handle_info({:publish, msg}, state) do
|
||||
IO.puts("Received message")
|
||||
IO.inspect(msg)
|
||||
{:noreply, state}
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,18 @@
|
||||
defmodule NeonVertigoService.Mqtt.Supervisor do
|
||||
use Supervisor
|
||||
|
||||
def start_link(init_arg) do
|
||||
Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
|
||||
end
|
||||
|
||||
# Callbacks
|
||||
|
||||
@impl true
|
||||
def init(_init_arg) do
|
||||
children = [
|
||||
NeonVertigoService.Mqtt.Service
|
||||
]
|
||||
|
||||
Supervisor.init(children, strategy: :one_for_all)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user