geo-therminator/lib/geo_therminator/pump_api/device/pub_sub.ex

60 lines
1.6 KiB
Elixir
Raw Normal View History

2021-11-07 09:01:39 +00:00
defmodule GeoTherminator.PumpAPI.Device.PubSub do
alias Phoenix.PubSub
@installation_topic "installation:"
@spec subscribe_installation(GeoTherminator.PumpAPI.Auth.InstallationInfo.t()) :: :ok
def subscribe_installation(installation) do
:ok = PubSub.subscribe(__MODULE__, @installation_topic <> to_string(installation.id))
end
@spec broadcast_device(GeoTherminator.PumpAPI.Device.t()) :: :ok
def broadcast_device(device) do
:ok =
PubSub.broadcast!(
__MODULE__,
@installation_topic <> to_string(device.id),
{:device, device}
)
end
@spec broadcast_status(
GeoTherminator.PumpAPI.Device.t(),
GeoTherminator.PumpAPI.Device.Status.t()
) :: :ok
def broadcast_status(device, status) do
:ok =
PubSub.broadcast!(
__MODULE__,
@installation_topic <> to_string(device.id),
{:status, status}
)
end
@spec broadcast_registers(
GeoTherminator.PumpAPI.Device.t(),
GeoTherminator.PumpAPI.Device.RegisterCollection.t()
) :: :ok
def broadcast_registers(device, registers) do
:ok =
PubSub.broadcast!(
__MODULE__,
@installation_topic <> to_string(device.id),
{:registers, registers}
)
end
@spec broadcast_opstat(
GeoTherminator.PumpAPI.Device.t(),
GeoTherminator.PumpAPI.Device.OpStat.t()
) :: :ok
def broadcast_opstat(device, opstat) do
:ok =
PubSub.broadcast!(
__MODULE__,
@installation_topic <> to_string(device.id),
{:opstat, opstat}
)
end
end