Skip to content
Permalink
Browse files

Add `mounted_tool_id` to Device schema and worker

  • Loading branch information
ConnorRigby committed Nov 22, 2019
1 parent b6602c5 commit 2168e719e2ee7fc0eb355b133524dfd5afcc1983
@@ -20,6 +20,7 @@ defmodule FarmbotCore.Asset.Device do
field(:last_ota, :utc_datetime)
field(:last_ota_checkup, :utc_datetime)
field(:ota_hour, :integer)
field(:mounted_tool_id, :integer)
field(:monitor, :boolean, default: true)
timestamps()
end
@@ -31,7 +32,8 @@ defmodule FarmbotCore.Asset.Device do
timezone: device.timezone,
last_ota: device.last_ota,
last_ota_checkup: device.last_ota_checkup,
ota_hour: device.ota_hour
ota_hour: device.ota_hour,
mounted_tool_id: device.mounted_tool_id
}
end

@@ -43,6 +45,7 @@ defmodule FarmbotCore.Asset.Device do
:last_ota,
:last_ota_checkup,
:ota_hour,
:mounted_tool_id,
:monitor,
:created_at,
:updated_at
@@ -1,5 +1,5 @@
defimpl FarmbotCore.AssetWorker, for: FarmbotCore.Asset.Device do
alias FarmbotCore.Asset.Device
alias FarmbotCore.{Asset, Asset.Device}
use GenServer
require FarmbotCore.Logger

@@ -26,17 +26,38 @@ defimpl FarmbotCore.AssetWorker, for: FarmbotCore.Asset.Device do

def log_changes(new_device, old_device) do
interesting_params = [
:ota_hour
:ota_hour,
:mounted_tool_id
]
new_interesting_device = Map.take(new_device, interesting_params) |> MapSet.new()
old_interesting_device = Map.take(old_device, interesting_params) |> MapSet.new()
difference = MapSet.difference(new_interesting_device, old_interesting_device)
Enum.each(difference, fn
{:ota_hour, nil} ->
FarmbotCore.Logger.success 1, "Farmbot will apply updates as soon as possible"

{:ota_hour, hour} ->
FarmbotCore.Logger.success 1, "Farmbot will apply updates during the hour of #{hour}:00"

{:mounted_tool_id, nil} ->
if old_device.mounted_tool_id do
if tool = Asset.get_tool(id: old_device.mounted_tool_id) do
FarmbotCore.Logger.info 2, "Farmbot dismounted #{tool.name}"
else
FarmbotCore.Logger.info 2, "Farmbot dismounted unknown tool"
end
else
# no previously mounted tool
:ok
end

{:mounted_tool_id, id} ->
if tool = Asset.get_tool(id: id) do
FarmbotCore.Logger.info 2, "Farmbot mounted #{tool.name}"
else
FarmbotCore.Logger.info 2, "Farmbot mounted unknown tool"
end

{_key, _value} ->
:noop
end)

0 comments on commit 2168e71

Please sign in to comment.
You can’t perform that action at this time.