Application and basic MQTT subscriber
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user