fix: actually include sensor firmware version string in repo, /version response
This commit is contained in:
@@ -59,9 +59,29 @@ defmodule Localiser.Domain.Sensors do
|
||||
{:ok, sensor}
|
||||
end
|
||||
|
||||
@version_timeout_ms 5_000
|
||||
|
||||
def get_version(%Sensor{} = sensor) do
|
||||
Localiser.MQTT.Connection.publish(cmd_topic(sensor), Jason.encode!(%{action: "version"}))
|
||||
{:ok, sensor}
|
||||
:ok = Phoenix.PubSub.subscribe(Localiser.PubSub, "sensors")
|
||||
|
||||
case Localiser.MQTT.Connection.publish(cmd_topic(sensor), Jason.encode!(%{action: "version"})) do
|
||||
:ok ->
|
||||
result =
|
||||
receive do
|
||||
{:sensor_announced, %Sensor{sensor_id: sid, firmware_version: v}}
|
||||
when sid == sensor.sensor_id and not is_nil(v) ->
|
||||
{:ok, v}
|
||||
after
|
||||
@version_timeout_ms -> {:error, :timeout}
|
||||
end
|
||||
|
||||
Phoenix.PubSub.unsubscribe(Localiser.PubSub, "sensors")
|
||||
result
|
||||
|
||||
error ->
|
||||
Phoenix.PubSub.unsubscribe(Localiser.PubSub, "sensors")
|
||||
error
|
||||
end
|
||||
end
|
||||
|
||||
def send_ota_update(%Sensor{} = sensor, url, version) do
|
||||
@@ -169,12 +189,22 @@ defmodule Localiser.Domain.Sensors do
|
||||
|
||||
# Called when an ESP board self-announces on MQTT. Inserts a new unplaced sensor
|
||||
# record, or marks it confirmed if the sensor_id already exists.
|
||||
def upsert_announced(sensor_id) do
|
||||
def upsert_announced(sensor_id, firmware_version \\ nil) do
|
||||
attrs = %{sensor_id: sensor_id, confirmed: true}
|
||||
attrs = if firmware_version, do: Map.put(attrs, :firmware_version, firmware_version), else: attrs
|
||||
|
||||
on_conflict =
|
||||
if firmware_version do
|
||||
[set: [confirmed: true, firmware_version: firmware_version, updated_at: DateTime.utc_now()]]
|
||||
else
|
||||
[set: [confirmed: true, updated_at: DateTime.utc_now()]]
|
||||
end
|
||||
|
||||
result =
|
||||
%Sensor{}
|
||||
|> Sensor.changeset(%{sensor_id: sensor_id, confirmed: true})
|
||||
|> Sensor.changeset(attrs)
|
||||
|> Repo.insert(
|
||||
on_conflict: [set: [confirmed: true, updated_at: DateTime.utc_now()]],
|
||||
on_conflict: on_conflict,
|
||||
conflict_target: :sensor_id,
|
||||
returning: true
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user