feat: accept calibration readings only from a chosen tag
This commit is contained in:
@@ -23,6 +23,16 @@ defmodule Localiser.Web.Channels.CalibrationChannel do
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
def handle_info({:calibration_scan_reading, _sensor_id, tag_id, rssi}, socket) do
|
||||
push(socket, "scan_reading", %{tag_id: tag_id, rssi: rssi})
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
def handle_info({:calibration_tag_set, _sensor_id, tag_id}, socket) do
|
||||
push(socket, "tag_set", %{tag_id: tag_id})
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
def handle_info({:stage_started, _sensor_id, distance, completed_count}, socket) do
|
||||
push(socket, "stage_started", %{distance: distance, completed_stages: completed_count})
|
||||
{:noreply, socket}
|
||||
|
||||
@@ -116,6 +116,16 @@ defmodule Localiser.Web.Controllers.SensorController do
|
||||
unauthorized: {"Unauthorized", "application/json", Schemas.Error}
|
||||
]
|
||||
|
||||
operation :calibration_set_tag,
|
||||
summary: "Commit to a specific tag for calibration stages",
|
||||
parameters: [id: [in: :path, type: :integer, required: true]],
|
||||
request_body: {"Tag params", "application/json", Schemas.CalibrationTagParams, required: true},
|
||||
responses: [
|
||||
ok: {"Tag committed", "application/json", Schemas.CalibrationBeginResponse},
|
||||
unprocessable_entity: {"Stage active or not in calibration mode", "application/json", Schemas.Error},
|
||||
unauthorized: {"Unauthorized", "application/json", Schemas.Error}
|
||||
]
|
||||
|
||||
operation :calibration_stage_start,
|
||||
summary: "Start collecting samples for a specific distance",
|
||||
parameters: [id: [in: :path, type: :integer, required: true]],
|
||||
@@ -276,6 +286,31 @@ defmodule Localiser.Web.Controllers.SensorController do
|
||||
json(conn, %{status: "calibration_mode", samples_needed: calibration_samples_needed()})
|
||||
end
|
||||
|
||||
def calibration_set_tag(conn, %{"id" => id, "tag_id" => tag_id}) when is_binary(tag_id) do
|
||||
sensor = Sensors.get_sensor!(id)
|
||||
|
||||
case SensorServer.set_calibration_tag(sensor.sensor_id, tag_id) do
|
||||
:ok ->
|
||||
json(conn, %{status: "calibration_mode", samples_needed: calibration_samples_needed()})
|
||||
|
||||
{:error, :stage_active} ->
|
||||
conn
|
||||
|> put_status(:unprocessable_entity)
|
||||
|> json(%{error: "cannot change tag while a stage is active"})
|
||||
|
||||
{:error, :not_in_calibration_mode} ->
|
||||
conn
|
||||
|> put_status(:unprocessable_entity)
|
||||
|> json(%{error: "sensor is not in calibration mode"})
|
||||
end
|
||||
end
|
||||
|
||||
def calibration_set_tag(conn, _params) do
|
||||
conn
|
||||
|> put_status(:bad_request)
|
||||
|> json(%{error: "tag_id is required"})
|
||||
end
|
||||
|
||||
def calibration_stage_start(conn, %{"id" => id, "distance" => distance})
|
||||
when is_number(distance) and distance > 0 do
|
||||
sensor = Sensors.get_sensor!(id)
|
||||
@@ -293,6 +328,11 @@ defmodule Localiser.Web.Controllers.SensorController do
|
||||
conn
|
||||
|> put_status(:unprocessable_entity)
|
||||
|> json(%{error: "sensor is not in calibration mode"})
|
||||
|
||||
{:error, :no_tag_selected} ->
|
||||
conn
|
||||
|> put_status(:unprocessable_entity)
|
||||
|> json(%{error: "no tag selected — call /calibration/tag first"})
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -102,6 +102,7 @@ defmodule Localiser.Web.Router do
|
||||
post "/sensors/:id/factory_reset", SensorController, :factory_reset
|
||||
post "/sensors/:id/reconfigure", SensorController, :reconfigure
|
||||
post "/sensors/:id/calibration/begin", SensorController, :calibration_begin
|
||||
post "/sensors/:id/calibration/tag", SensorController, :calibration_set_tag
|
||||
post "/sensors/:id/calibration/stage", SensorController, :calibration_stage_start
|
||||
post "/sensors/:id/calibration/finish", SensorController, :calibration_finish
|
||||
delete "/sensors/:id/calibration", SensorController, :calibration_cancel
|
||||
|
||||
@@ -330,6 +330,17 @@ defmodule Localiser.Web.Schemas do
|
||||
})
|
||||
end
|
||||
|
||||
defmodule CalibrationTagParams do
|
||||
require OpenApiSpex
|
||||
OpenApiSpex.schema(%{
|
||||
title: "CalibrationTagParams", type: :object,
|
||||
properties: %{
|
||||
tag_id: %Schema{type: :string, description: "Tag ID to lock in for calibration stages"}
|
||||
},
|
||||
required: [:tag_id]
|
||||
})
|
||||
end
|
||||
|
||||
defmodule CalibrationStageParams do
|
||||
require OpenApiSpex
|
||||
OpenApiSpex.schema(%{
|
||||
|
||||
Reference in New Issue
Block a user