weewx-proxy/lib/weewx_proxy/mqtt.ex
Daniel Kempkens 42edf668a4
All checks were successful
Build / build (push) Successful in 4m23s
feat(mqtt): re-publish in a way home-assistant understands better
2024-08-09 17:16:22 +02:00

86 lines
3.1 KiB
Elixir

defmodule WeewxProxy.Mqtt do
@moduledoc false
use Supervisor
@name __MODULE__
@spec child_spec(term) :: Supervisor.child_spec()
def child_spec(_arg) do
%{
id: __MODULE__,
start: {__MODULE__, :start_link, []},
restart: :permanent,
shutdown: 5000,
type: :supervisor
}
end
@spec start_link :: Supervisor.on_start()
def start_link do
Supervisor.start_link(__MODULE__, [], name: @name)
end
# Callbacks
@impl true
def init([]) do
children = [
{Tortoise311.Connection,
[
name: WeewxProxy.Mqtt.WeewxBroker,
client_id: Application.fetch_env!(:weewx_proxy, :mqtt_weewx_client_id),
server:
{Tortoise311.Transport.Tcp,
host: Application.fetch_env!(:weewx_proxy, :mqtt_weewx_host),
port: Application.fetch_env!(:weewx_proxy, :mqtt_weewx_port)},
user_name: Application.fetch_env!(:weewx_proxy, :mqtt_weewx_user),
password: Application.fetch_env!(:weewx_proxy, :mqtt_weewx_password),
handler: {Tortoise311.Handler.Logger, []}
]},
{Tortoise311.Connection,
[
name: WeewxProxy.Mqtt.LocalBroker,
client_id: LocalBroker,
server:
{Tortoise311.Transport.Tcp,
host: Application.fetch_env!(:weewx_proxy, :mqtt_local_host),
port: Application.fetch_env!(:weewx_proxy, :mqtt_local_port)},
user_name: Application.fetch_env!(:weewx_proxy, :mqtt_local_user),
password: Application.fetch_env!(:weewx_proxy, :mqtt_local_password),
handler: {Tortoise311.Handler.Logger, []}
]},
{Tortoise311.Connection,
[
name: WeewxProxy.Mqtt.SdrIngest,
client_id: Application.fetch_env!(:weewx_proxy, :mqtt_sdr_client_id),
server:
{Tortoise311.Transport.Tcp,
host: Application.fetch_env!(:weewx_proxy, :mqtt_local_host),
port: Application.fetch_env!(:weewx_proxy, :mqtt_local_port)},
user_name: Application.fetch_env!(:weewx_proxy, :mqtt_local_user),
password: Application.fetch_env!(:weewx_proxy, :mqtt_local_password),
subscriptions: ["rtl433"],
handler: {WeewxProxy.Sdr.Ecowitt, []}
]},
{Tortoise311.Connection,
[
name: WeewxProxy.Mqtt.ModbusIngest,
client_id: Application.fetch_env!(:weewx_proxy, :mqtt_modbus_client_id),
server:
{Tortoise311.Transport.Tcp,
host: Application.fetch_env!(:weewx_proxy, :mqtt_local_host),
port: Application.fetch_env!(:weewx_proxy, :mqtt_local_port)},
user_name: Application.fetch_env!(:weewx_proxy, :mqtt_local_user),
password: Application.fetch_env!(:weewx_proxy, :mqtt_local_password),
subscriptions: ["deye/#", "bitshake/#"],
handler: {WeewxProxy.Modbus.Deye, []}
]}
]
:sdr_ecowitt = :ets.new(:sdr_ecowitt, [:set, :public, :named_table, {:read_concurrency, true}])
:purpleair = :ets.new(:purpleair, [:set, :public, :named_table, {:read_concurrency, true}])
Supervisor.init(children, strategy: :one_for_one, max_restarts: 9, max_seconds: 5)
end
end