feat: dynamically manage floors/rooms/tags

This commit is contained in:
2026-04-16 16:57:45 +02:00
parent 34ddbe669e
commit c3fd8b950c
11 changed files with 238 additions and 22 deletions
@@ -0,0 +1,47 @@
defmodule Localiser.Localisation.Room.Manager do
@moduledoc """
Per-floor GenServer that reacts to room lifecycle events and drives Room.Supervisor.
- {:room_created, room} → start a Room.Server (if room belongs to this floor)
- {:room_deleted, room_id, floor_id} → terminate the Room.Server (if on this floor)
"""
use GenServer
alias Localiser.Localisation.Room
def start_link(floor) do
GenServer.start_link(__MODULE__, floor.id, name: via(floor.id))
end
def via(floor_id) do
{:via, Registry, {Localiser.Registry, {:room_manager, floor_id}}}
end
@impl true
def init(floor_id) do
Phoenix.PubSub.subscribe(Localiser.PubSub, "rooms")
{:ok, %{floor_id: floor_id}}
end
@impl true
def handle_info({:room_created, %{floor_id: floor_id} = room}, %{floor_id: floor_id} = state) do
case Registry.lookup(Localiser.Registry, {:room, room.id}) do
[] -> Room.Supervisor.start_room_server(floor_id, room)
_ -> :ok
end
{:noreply, state}
end
def handle_info({:room_deleted, room_id, floor_id}, %{floor_id: floor_id} = state) do
case Registry.lookup(Localiser.Registry, {:room, room_id}) do
[{pid, _}] -> DynamicSupervisor.terminate_child(Room.Supervisor.via(floor_id), pid)
[] -> :ok
end
{:noreply, state}
end
def handle_info(_msg, state), do: {:noreply, state}
end
+15
View File
@@ -27,6 +27,8 @@ defmodule Localiser.Localisation.Room.Server do
@impl true
def init(room) do
Phoenix.PubSub.subscribe(@pubsub, "rooms")
state = %__MODULE__{
id: room.id,
name: room.name,
@@ -65,6 +67,19 @@ defmodule Localiser.Localisation.Room.Server do
{:reply, state.occupants, state}
end
@impl true
def handle_info({:room_updated, %{id: id} = room}, %{id: id} = state) do
{:noreply, %{state |
name: room.name,
offset_x: room.offset_x || 0.0,
offset_y: room.offset_y || 0.0,
width: room.width || 0.0,
height: room.height || 0.0
}}
end
def handle_info(_msg, state), do: {:noreply, state}
defp broadcast(room_id, occupants) do
Phoenix.PubSub.broadcast(
@pubsub,