Application and basic MQTT subscriber

This commit is contained in:
2025-12-17 02:12:07 +02:00
parent d450229356
commit dd134a632c
6 changed files with 65 additions and 9 deletions
+10 -8
View File
@@ -3,16 +3,18 @@ defmodule NeonVertigoService do
Documentation for `NeonVertigoService`.
"""
@doc """
Hello world.
use Application
## Examples
@impl true
def start(_start_type, _start_args) do
children = [
# %{id: :emqtt, start: {:emqtt, :start_link, [[{:owner, self()}, {:username, "artemis"}, {:password, "artemis"}]]}},
{NeonVertigoService.Mqtt, [{}]}
]
iex> NeonVertigoService.hello()
:world
{:ok, _spid} = Supervisor.start_link(children, strategy: :one_for_one)
# {:ok, mqtt_pid} = Supervisor.start_child(spid, %{id: :emqtt, start: {:emqtt, :start_link, [[{:owner, self()}, {:username, "artemis"}, {:password, "artemis"}]]}})
# {:ok, _} = Supervisor.start_child(spid, {NeonVertigoService.Mqtt, [{mqtt_pid}]})
"""
def hello do
:world
end
end
+25
View File
@@ -0,0 +1,25 @@
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