feat: include last_seen in tag responses

This commit is contained in:
2026-05-16 20:53:54 +02:00
parent 0f0f377e98
commit 57075f66e8
2 changed files with 21 additions and 5 deletions
+14 -2
View File
@@ -11,7 +11,8 @@ defmodule Localiser.Localisation.Tag.Filter do
:filter_module,
:filter_state,
:rooms,
current_room_id: nil
current_room_id: nil,
last_seen: nil
]
def start_link(tag) do
@@ -28,6 +29,13 @@ defmodule Localiser.Localisation.Tag.Filter do
GenServer.cast(via(tag_id), {:ingest, measurements})
end
def get_live_state(tag_id) do
case Registry.lookup(Localiser.Registry, {:filter, tag_id}) do
[{pid, _}] -> GenServer.call(pid, :get_live_state)
[] -> nil
end
end
# Hot-swap the filter module. Reinitialises filter state with new module.
def swap_filter(tag_id, new_module, opts \\ []) do
GenServer.call(via(tag_id), {:swap_filter, new_module, opts})
@@ -67,10 +75,14 @@ defmodule Localiser.Localisation.Tag.Filter do
if new_room_id, do: RoomServer.tag_entered(new_room_id, state.tag_id)
end
{:noreply, %{state | filter_state: new_filter_state, current_room_id: new_room_id}}
{:noreply, %{state | filter_state: new_filter_state, current_room_id: new_room_id, last_seen: DateTime.utc_now()}}
end
@impl true
def handle_call(:get_live_state, _from, state) do
{:reply, %{room_id: state.current_room_id, last_seen: state.last_seen}, state}
end
def handle_call({:swap_filter, new_module, opts}, _from, state) do
case new_module.init([], opts) do
{:ok, new_filter_state} ->
@@ -95,10 +95,14 @@ defmodule Localiser.Web.Controllers.TagController do
end
defp render_tag(tag) do
live = TagFilter.get_live_state(tag.tag_id)
%{
id: tag.id,
tag_id: tag.tag_id,
name: tag.name
id: tag.id,
tag_id: tag.tag_id,
name: tag.name,
room_id: live && live.room_id,
last_seen: live && live.last_seen
}
end