feat: dynamically manage floors/rooms/tags
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
defmodule Localiser.Localisation.Floor.Manager do
|
||||
@moduledoc """
|
||||
Global GenServer that reacts to floor lifecycle events and drives Floor.Supervisor.
|
||||
|
||||
- {:floor_created, floor} → start a Floor.Server for the new floor
|
||||
- {:floor_deleted, floor_id} → terminate the Floor.Server subtree
|
||||
"""
|
||||
|
||||
use GenServer
|
||||
|
||||
alias Localiser.Localisation.Floor
|
||||
|
||||
def start_link(_args) do
|
||||
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init(:ok) do
|
||||
Phoenix.PubSub.subscribe(Localiser.PubSub, "floors")
|
||||
{:ok, :ok}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_info({:floor_created, floor}, state) do
|
||||
case Registry.lookup(Localiser.Registry, {:floor_server, floor.id}) do
|
||||
[] -> Floor.Supervisor.start_floor_server(floor)
|
||||
_ -> :ok
|
||||
end
|
||||
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
def handle_info({:floor_deleted, floor_id}, state) do
|
||||
case Registry.lookup(Localiser.Registry, {:floor_server, floor_id}) do
|
||||
[{pid, _}] -> DynamicSupervisor.terminate_child(Floor.Supervisor, pid)
|
||||
[] -> :ok
|
||||
end
|
||||
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
def handle_info(_msg, state), do: {:noreply, state}
|
||||
end
|
||||
@@ -3,6 +3,7 @@ defmodule Localiser.Localisation.Floor.Server do
|
||||
|
||||
alias Localiser.Localisation.Room
|
||||
alias Localiser.Localisation.Sensor
|
||||
|
||||
def start_link(floor) do
|
||||
Supervisor.start_link(__MODULE__, floor, name: via(floor.id))
|
||||
end
|
||||
@@ -15,6 +16,7 @@ defmodule Localiser.Localisation.Floor.Server do
|
||||
def init(floor) do
|
||||
children = [
|
||||
{Room.Supervisor, floor},
|
||||
{Room.Manager, floor},
|
||||
{Sensor.Supervisor, floor},
|
||||
{Sensor.Manager, floor}
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user