feat: add factory_Reset, reconfigure_settings endpoints for sensors

This commit is contained in:
2026-05-15 15:23:58 +02:00
parent b434896e71
commit ed96e044cf
4 changed files with 77 additions and 2 deletions
@@ -77,6 +77,23 @@ defmodule Localiser.Web.Controllers.SensorController do
unauthorized: {"Unauthorized", "application/json", Schemas.Error}
]
operation :factory_reset,
summary: "Send factory reset command to sensor",
parameters: [id: [in: :path, type: :integer, required: true]],
responses: [
ok: {"Command sent", "application/json", Schemas.CommandSent},
unauthorized: {"Unauthorized", "application/json", Schemas.Error}
]
operation :reconfigure,
summary: "Push new network settings to sensor",
parameters: [id: [in: :path, type: :integer, required: true]],
request_body: {"Reconfigure params", "application/json", Schemas.SensorReconfigureParams, required: true},
responses: [
ok: {"Command sent", "application/json", Schemas.CommandSent},
unauthorized: {"Unauthorized", "application/json", Schemas.Error}
]
operation :calibration_start,
summary: "Begin RSSI calibration",
parameters: [id: [in: :path, type: :integer, required: true]],
@@ -181,6 +198,19 @@ defmodule Localiser.Web.Controllers.SensorController do
end
end
def factory_reset(conn, %{"id" => id}) do
sensor = Sensors.get_sensor!(id)
{:ok, _} = Sensors.factory_reset(sensor)
json(conn, %{status: "ok"})
end
def reconfigure(conn, %{"id" => id} = params) do
sensor = Sensors.get_sensor!(id)
config = Map.take(params, ["ssid", "password", "mqtt_broker", "mqtt_port"])
{:ok, _} = Sensors.reconfigure_settings(sensor, config)
json(conn, %{status: "ok"})
end
def calibration_start(conn, %{"id" => id, "reference_distance" => ref_dist}) do
sensor = Sensors.get_sensor!(id)
:ok = SensorServer.begin_calibration(sensor.sensor_id, ref_dist)
+2
View File
@@ -82,6 +82,8 @@ defmodule Localiser.Web.Router do
delete "/sensors/:id", SensorController, :delete
put "/sensors/:id/place", SensorController, :place
delete "/sensors/:id/place", SensorController, :unplace
post "/sensors/:id/factory_reset", SensorController, :factory_reset
post "/sensors/:id/reconfigure", SensorController, :reconfigure
post "/sensors/:id/calibration/start", SensorController, :calibration_start
post "/sensors/:id/calibration/stop", SensorController, :calibration_stop
end
+22
View File
@@ -124,6 +124,15 @@ defmodule Localiser.Web.Schemas do
})
end
defmodule CommandSent do
require OpenApiSpex
OpenApiSpex.schema(%{
title: "CommandSent", type: :object,
properties: %{status: %Schema{type: :string, enum: ["ok"]}},
required: [:status]
})
end
defmodule CalibrationStatus do
require OpenApiSpex
OpenApiSpex.schema(%{
@@ -246,6 +255,19 @@ defmodule Localiser.Web.Schemas do
})
end
defmodule SensorReconfigureParams do
require OpenApiSpex
OpenApiSpex.schema(%{
title: "SensorReconfigureParams", type: :object,
properties: %{
ssid: %Schema{type: :string},
password: %Schema{type: :string, format: :password},
mqtt_broker: %Schema{type: :string, required: false},
mqtt_port: %Schema{type: :integer, required: false}
}
})
end
defmodule SensorUpdateParams do
require OpenApiSpex
OpenApiSpex.schema(%{