feat: add current room occupancy endpoint

This commit is contained in:
2026-05-16 20:55:00 +02:00
parent 8182776701
commit e4e4065c2b
3 changed files with 57 additions and 5 deletions
@@ -2,12 +2,21 @@ defmodule Localiser.Web.Controllers.TagController do
use Phoenix.Controller, formats: [:json]
use OpenApiSpex.ControllerSpecs
alias Localiser.Domain.Tags
alias Localiser.Domain.{Rooms, Tags}
alias Localiser.Localisation.Tag.Filter, as: TagFilter
alias Localiser.Web.Schemas
tags ["Tags"]
security [%{"bearerAuth" => []}]
operation :occupancy,
summary: "Get every tag's current room",
responses: [
ok: {"Tag occupancy list", "application/json",
%OpenApiSpex.Schema{type: :array, items: Schemas.TagOccupancy}},
unauthorized: {"Unauthorized", "application/json", Schemas.Error}
]
operation :index,
summary: "List all tags",
responses: [
@@ -50,6 +59,18 @@ defmodule Localiser.Web.Controllers.TagController do
unauthorized: {"Unauthorized", "application/json", Schemas.Error}
]
def occupancy(conn, _params) do
tag_to_room = build_tag_to_room_map()
result =
Tags.list_tags()
|> Enum.map(fn tag ->
%{id: tag.id, tag_id: tag.tag_id, name: tag.name, room_id: Map.get(tag_to_room, tag.tag_id)}
end)
json(conn, result)
end
def index(conn, _params) do
json(conn, Enum.map(Tags.list_tags(), &render_tag/1))
end
@@ -94,6 +115,20 @@ defmodule Localiser.Web.Controllers.TagController do
send_resp(conn, :no_content, "")
end
defp build_tag_to_room_map do
Rooms.list_rooms()
|> Enum.flat_map(fn room ->
occupants =
case Registry.lookup(Localiser.Registry, {:room, room.id}) do
[{pid, _}] -> GenServer.call(pid, :get_occupants)
[] -> MapSet.new()
end
Enum.map(MapSet.to_list(occupants), &{&1, room.id})
end)
|> Map.new()
end
defp render_tag(tag) do
live = TagFilter.get_live_state(tag.tag_id)
+1
View File
@@ -71,6 +71,7 @@ defmodule Localiser.Web.Router do
resources "/floors", FloorController, except: [:new, :edit]
resources "/floors/:floor_id/rooms", RoomController, except: [:new, :edit]
get "/tags/occupancy", TagController, :occupancy
resources "/tags", TagController, except: [:new, :edit]
# Sensors
+20 -4
View File
@@ -67,11 +67,13 @@ defmodule Localiser.Web.Schemas do
OpenApiSpex.schema(%{
title: "Tag", type: :object,
properties: %{
id: %Schema{type: :integer},
tag_id: %Schema{type: :string},
name: %Schema{type: :string}
id: %Schema{type: :integer},
tag_id: %Schema{type: :string},
name: %Schema{type: :string},
room_id: %Schema{type: :integer, nullable: true},
last_seen: %Schema{type: :string, format: :"date-time", nullable: true}
},
required: [:id, :tag_id, :name]
required: [:id, :tag_id, :name, :room_id, :last_seen]
})
end
@@ -303,6 +305,20 @@ defmodule Localiser.Web.Schemas do
})
end
defmodule TagOccupancy do
require OpenApiSpex
OpenApiSpex.schema(%{
title: "TagOccupancy", type: :object,
properties: %{
id: %Schema{type: :integer},
tag_id: %Schema{type: :string},
name: %Schema{type: :string},
room_id: %Schema{type: :integer, nullable: true}
},
required: [:id, :tag_id, :name, :room_id]
})
end
defmodule TagParams do
require OpenApiSpex
OpenApiSpex.schema(%{