diff --git a/lib/localiser/localisation/tag/filter.ex b/lib/localiser/localisation/tag/filter.ex index 692de2f..f79d26d 100644 --- a/lib/localiser/localisation/tag/filter.ex +++ b/lib/localiser/localisation/tag/filter.ex @@ -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} -> diff --git a/lib/localiser/web/controllers/tag_controller.ex b/lib/localiser/web/controllers/tag_controller.ex index 3416649..82f1d63 100644 --- a/lib/localiser/web/controllers/tag_controller.ex +++ b/lib/localiser/web/controllers/tag_controller.ex @@ -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