feat: support firmware uploads, OTA pushes

This commit is contained in:
2026-05-17 14:39:29 +02:00
parent e4e4065c2b
commit 2601484fad
7 changed files with 198 additions and 3 deletions
@@ -2,7 +2,7 @@ defmodule Localiser.Web.Controllers.SensorController do
use Phoenix.Controller, formats: [:json]
use OpenApiSpex.ControllerSpecs
alias Localiser.Domain.Sensors
alias Localiser.Domain.{Sensors, Firmware}
alias Localiser.Localisation.Sensor.Server, as: SensorServer
alias Localiser.Web.Schemas
@@ -77,6 +77,20 @@ defmodule Localiser.Web.Controllers.SensorController do
unauthorized: {"Unauthorized", "application/json", Schemas.Error}
]
operation :ota,
summary: "Send OTA update command to a single sensor",
parameters: [id: [in: :path, type: :integer, required: true]],
request_body: {"OTA params", "application/json", %OpenApiSpex.Schema{
type: :object,
required: [:version],
properties: %{version: %OpenApiSpex.Schema{type: :string}}
}, required: true},
responses: [
ok: {"Command sent", "application/json", Schemas.CommandSent},
not_found: {"Firmware version not found", "application/json", Schemas.Error},
unauthorized: {"Unauthorized", "application/json", Schemas.Error}
]
operation :factory_reset,
summary: "Send factory reset command to sensor",
parameters: [id: [in: :path, type: :integer, required: true]],
@@ -198,6 +212,27 @@ defmodule Localiser.Web.Controllers.SensorController do
end
end
def ota(conn, %{"id" => id, "version" => version}) do
sensor = Sensors.get_sensor!(id)
case Firmware.get(version) do
{:ok, _path} ->
url = "#{conn.scheme}://#{conn.host}:#{conn.port}/api/firmware/#{version}"
{:ok, _} = Sensors.send_ota_update(sensor, url, version)
json(conn, %{status: "ok", url: url, version: version})
:not_found ->
conn
|> put_status(:not_found)
|> json(%{error: "Firmware version not found"})
end
end
def ota(conn, _params) do
conn
|> put_status(:bad_request)
|> json(%{error: "version is required"})
end
def factory_reset(conn, %{"id" => id}) do
sensor = Sensors.get_sensor!(id)
{:ok, _} = Sensors.factory_reset(sensor)