feat: accept calibration readings only from a chosen tag

This commit is contained in:
2026-05-21 21:46:45 +02:00
parent eb22fbc789
commit b0d9a7dbf8
6 changed files with 164 additions and 61 deletions
+14 -13
View File
@@ -35,26 +35,27 @@ defmodule Localiser.RSSI.Buffer do
defp flush_batches(batches) do
Enum.each(batches, fn {tag_id, readings} ->
case Registry.lookup(Localiser.Registry, {:filter, tag_id}) do
[{_pid, _}] ->
measurements = Enum.flat_map(readings, &resolve_measurement/1)
if measurements != [], do: TagFilter.ingest(tag_id, measurements)
# Calibration routing runs regardless of whether the tag has a filter registered,
# so scan-mode readings from unknown tags still reach the sensor server.
normal = Enum.flat_map(readings, &route_reading(tag_id, &1))
[] ->
:ok
if normal != [] do
case Registry.lookup(Localiser.Registry, {:filter, tag_id}) do
[{_pid, _}] -> TagFilter.ingest(tag_id, normal)
[] -> :ok
end
end
end)
end
# Resolves a raw RSSI reading to a measurement with sensor location and distance.
# If the sensor is in calibration mode, feeds the reading to Sensor.Server instead
# and returns [] so the sample is excluded from Tag.Filter measurements.
# If the sensor server isn't running, returns [].
defp resolve_measurement(%{sensor_id: sensor_id, rssi: rssi, tx_power: tx_power}) do
# Routes one reading. If the sensor is in any calibration state, forwards to Sensor.Server
# (with tag_id so scan-mode can advertise it) and returns [] to exclude from Tag.Filter.
# Otherwise returns a normal measurement map.
defp route_reading(tag_id, %{sensor_id: sensor_id, rssi: rssi, tx_power: tx_power}) do
case Registry.lookup(Localiser.Registry, {:sensor, sensor_id}) do
[{_pid, _}] ->
if SensorServer.calibrating?(sensor_id) do
SensorServer.calibration_reading(sensor_id, rssi)
if SensorServer.in_calibration_mode?(sensor_id) do
SensorServer.calibration_reading(sensor_id, tag_id, rssi)
[]
else
[SensorServer.measure(sensor_id, rssi, tx_power)]