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

67 lines
1.7 KiB
Elixir
Raw Normal View History

2021-11-07 09:01:39 +00:00
defmodule GeoTherminator.PumpAPI.Device.PubSub do
2023-01-11 18:37:27 +00:00
require GeoTherminator.PumpAPI.Auth.InstallationInfo
2021-11-07 09:01:39 +00:00
alias Phoenix.PubSub
2023-01-11 18:37:27 +00:00
alias GeoTherminator.PumpAPI.Auth.InstallationInfo
2021-11-07 09:01:39 +00:00
@installation_topic "installation:"
2023-01-11 18:37:27 +00:00
@spec subscribe_installation(InstallationInfo.t()) :: :ok
2021-11-07 09:01:39 +00:00
def subscribe_installation(installation) do
2023-01-11 18:37:27 +00:00
:ok =
PubSub.subscribe(
__MODULE__,
@installation_topic <> to_string(InstallationInfo.record(installation, :id))
)
2021-11-07 09:01:39 +00:00
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