feat: accept calibration readings only from a chosen tag
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user