Skip to content

Commit ebe7a24

Browse files
Merge pull request #1516 from FarmBot/staging
v15.4.9
2 parents 16e3811 + 0919bcf commit ebe7a24

21 files changed

+289
-170
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
# 15.4.9
4+
5+
* Add `tool.flow_rate_ml_per_s`.
6+
* Add `get_tool` Lua helper and use it in relevant helpers.
7+
* Fix `update_device({mounted_tool_id = 0})` functionality and validate values.
8+
* Fix `badarg` bug for plants with `planted_at` value.
9+
310
# 15.4.8
411

512
* Add support for using remote `plant` objects in the `water` Lua helper.

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
15.4.8
1+
15.4.9

lib/core/asset/tool.ex

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,29 @@ defmodule FarmbotOS.Asset.Tool do
1313
)
1414

1515
field(:name, :string)
16+
field(:flow_rate_ml_per_s, :integer)
1617
field(:monitor, :boolean, default: true)
1718
timestamps()
1819
end
1920

2021
view tool do
2122
%{
2223
id: tool.id,
23-
name: tool.name
24+
name: tool.name,
25+
flow_rate_ml_per_s: tool.flow_rate_ml_per_s
2426
}
2527
end
2628

2729
def changeset(tool, params \\ %{}) do
2830
tool
29-
|> cast(params, [:id, :name, :monitor, :created_at, :updated_at])
31+
|> cast(params, [
32+
:id,
33+
:name,
34+
:flow_rate_ml_per_s,
35+
:monitor,
36+
:created_at,
37+
:updated_at
38+
])
3039
|> validate_required([])
3140
end
3241
end

lib/os/lua.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ defmodule FarmbotOS.Lua do
138138
get_position: &Firmware.get_position/2,
139139
get_seed_tray_cell: &DataManipulation.get_seed_tray_cell/2,
140140
get_xyz: &Info.get_xyz/2,
141+
get_tool: &DataManipulation.get_tool/2,
141142
go_to_home: &Firmware.go_to_home/2,
142143
grid: &DataManipulation.grid/2,
143144
group: &DataManipulation.group/2,

lib/os/lua/data_manipulation.ex

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule FarmbotOS.Lua.DataManipulation do
44
"""
55

66
alias FarmbotOS.{Asset, JSON}
7-
alias FarmbotOS.Asset.{Device, FbosConfig, FirmwareConfig}
7+
alias FarmbotOS.Asset.{Repo, Tool, Device, FbosConfig, FirmwareConfig}
88
alias FarmbotOS.Lua.Util
99
alias FarmbotOS.Lua
1010
alias FarmbotOS.SysCalls.ResourceUpdate
@@ -100,7 +100,28 @@ defmodule FarmbotOS.Lua.DataManipulation do
100100

101101
def update_device([table], lua) do
102102
params = Map.new(table)
103-
_ = ResourceUpdate.update_resource("Device", nil, params)
103+
104+
new_params =
105+
if Map.has_key?(params, "mounted_tool_id") do
106+
if params["mounted_tool_id"] == 0 do
107+
Map.put(params, "mounted_tool_id", nil)
108+
else
109+
tool_ids = Repo.all(Tool) |> Enum.map(fn tool -> tool.id end)
110+
111+
if not Enum.member?(tool_ids, params["mounted_tool_id"]) do
112+
FarmbotOS.Logger.error(3, "Tool ID not found.")
113+
114+
Enum.filter(params, fn {key, _value} -> key != "mounted_tool_id" end)
115+
|> Map.new()
116+
else
117+
params
118+
end
119+
end
120+
else
121+
params
122+
end
123+
124+
_ = ResourceUpdate.update_resource("Device", nil, new_params)
104125
{[true], lua}
105126
end
106127

@@ -158,6 +179,43 @@ defmodule FarmbotOS.Lua.DataManipulation do
158179
{[Util.map_to_table(firmware_config)], lua}
159180
end
160181

182+
def get_tool([params], lua) do
183+
map = Util.lua_to_elixir(params)
184+
185+
tool_id = Map.get(map, "id")
186+
tool_name = Map.get(map, "name")
187+
tool_params = %{}
188+
189+
tool_params =
190+
if tool_id do
191+
Map.put(tool_params, :id, tool_id)
192+
else
193+
tool_params
194+
end
195+
196+
tool_params =
197+
if tool_name do
198+
Map.put(tool_params, :name, tool_name)
199+
else
200+
tool_params
201+
end
202+
203+
tool = Asset.get_tool(tool_params)
204+
205+
tool_result =
206+
if tool do
207+
%{
208+
id: tool.id,
209+
name: tool.name,
210+
flow_rate_ml_per_s: tool.flow_rate_ml_per_s
211+
}
212+
else
213+
nil
214+
end
215+
216+
{[tool_result], lua}
217+
end
218+
161219
def new_sensor_reading([table], lua) do
162220
table
163221
|> Enum.map(fn

lib/os/lua/result.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ defmodule FarmbotOS.Lua.Result do
3737

3838
def parse_error(error, _) do
3939
show(error)
40+
41+
if Mix.env() == :test do
42+
IO.puts(inspect(error))
43+
end
44+
4045
{:error, "Lua failure"}
4146
end
4247

lib/os/sys_calls/point_lookup.ex

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,18 @@ defmodule FarmbotOS.SysCalls.PointLookup do
4848
)
4949
end
5050

51-
%{resource_type: type, resource_id: id}
52-
|> Map.merge(s)
53-
|> Map.take(@relevant_keys)
54-
|> Map.put(:age, age)
51+
p =
52+
%{resource_type: type, resource_id: id}
53+
|> Map.merge(s)
54+
|> Map.take(@relevant_keys)
55+
|> Map.put(:age, age)
56+
57+
if p.planted_at do
58+
p
59+
|> Map.put(:planted_at, DateTime.to_iso8601(s.planted_at))
60+
else
61+
p
62+
end
5563

5664
other ->
5765
Logger.debug("Point error: Please notify support #{inspect(other)}")

priv/lua/dismount_tool.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ return function()
33
local start_time = os.time() * 1000
44

55
-- Checks
6+
if not tool_id then
7+
toast("No tool is mounted to the UTM", "error")
8+
return
9+
end
610
if not verify_tool() then
711
return
812
end
@@ -25,15 +29,11 @@ return function()
2529
end
2630

2731
-- Get tool name
28-
local tool = api({ url = "/api/tools/" .. tool_id })
29-
if not tool then
30-
toast("API error", "error")
31-
return
32-
end
32+
local tool_name = get_tool{id = tool_id}.name
3333

3434
-- Checks
3535
if not slot then
36-
toast("No slot found for the currently mounted tool (" .. tool.name .. ") - check the Tools panel", "error")
36+
toast("No slot found for the currently mounted tool (" .. tool_name .. ") - check the Tools panel", "error")
3737
return
3838
elseif slot_dir == 0 then
3939
toast("Tool slot must have a direction", "error")
@@ -46,7 +46,7 @@ return function()
4646
-- Job progress tracking
4747
function job(percent, status)
4848
set_job_progress(
49-
"Dismounting " .. tool.name,
49+
"Dismounting " .. tool_name,
5050
{ percent = percent, status = status, time = start_time }
5151
)
5252
end
@@ -85,6 +85,6 @@ return function()
8585
else
8686
job(100, "Complete")
8787
update_device({mounted_tool_id = 0})
88-
toast(tool.name .. " dismounted", "success")
88+
toast(tool_name .. " dismounted", "success")
8989
end
9090
end

priv/lua/dispense.lua

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,13 @@ return function(ml, params)
33
local tool_name = params.tool_name or "Watering Nozzle"
44
local pin_number = params.pin or 8
55

6-
-- Get all tools
7-
local tools = api({ url = "/api/tools/" })
8-
if not tools then
9-
toast("API error", "error")
6+
-- Get flow_rate
7+
local tool = get_tool{name = tool_name}
8+
if not tool then
9+
toast('Tool "' .. tool_name .. '" not found', 'error')
1010
return
1111
end
12-
13-
-- Pluck the nozzle
14-
local nozzle, flow_rate
15-
for key, tool in pairs(tools) do
16-
if tool.name == tool_name then
17-
nozzle = tool
18-
flow_rate = nozzle.flow_rate_ml_per_s
19-
end
20-
end
12+
local flow_rate = tool.flow_rate_ml_per_s
2113

2214
-- Checks
2315
if not flow_rate then

priv/lua/mount_tool.lua

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,7 @@ return function(input)
33
if type(input) == "string" then
44
local prelim_tool
55
local tool_name = input
6-
local tools = api({ url = "/api/tools/" })
7-
if not tools then
8-
toast("API error", "error")
9-
return
10-
end
11-
for key, tool in pairs(tools) do
12-
if tool.name == tool_name then
13-
prelim_tool = tool
14-
end
15-
end
6+
prelim_tool = get_tool{name = tool_name}
167
if not prelim_tool then
178
toast("'" .. tool_name .. "' tool not found", "error")
189
return
@@ -58,13 +49,9 @@ return function(input)
5849
return
5950
end
6051

61-
-- Get tool name
62-
local tool = api({
63-
url = "/api/tools/" .. slot.tool_id
64-
})
65-
52+
local tool = get_tool{id = slot.tool_id}
6653
if not tool then
67-
toast("API error", "error")
54+
toast("Tool slot must have a tool", "error")
6855
return
6956
end
7057

0 commit comments

Comments
 (0)