Skip to content

Commit

Permalink
Merge pull request #1516 from FarmBot/staging
Browse files Browse the repository at this point in the history
v15.4.9
  • Loading branch information
gabrielburnworth authored Jun 13, 2024
2 parents 16e3811 + 0919bcf commit ebe7a24
Show file tree
Hide file tree
Showing 21 changed files with 289 additions and 170 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

# 15.4.9

* Add `tool.flow_rate_ml_per_s`.
* Add `get_tool` Lua helper and use it in relevant helpers.
* Fix `update_device({mounted_tool_id = 0})` functionality and validate values.
* Fix `badarg` bug for plants with `planted_at` value.

# 15.4.8

* Add support for using remote `plant` objects in the `water` Lua helper.
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15.4.8
15.4.9
13 changes: 11 additions & 2 deletions lib/core/asset/tool.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,29 @@ defmodule FarmbotOS.Asset.Tool do
)

field(:name, :string)
field(:flow_rate_ml_per_s, :integer)
field(:monitor, :boolean, default: true)
timestamps()
end

view tool do
%{
id: tool.id,
name: tool.name
name: tool.name,
flow_rate_ml_per_s: tool.flow_rate_ml_per_s
}
end

def changeset(tool, params \\ %{}) do
tool
|> cast(params, [:id, :name, :monitor, :created_at, :updated_at])
|> cast(params, [
:id,
:name,
:flow_rate_ml_per_s,
:monitor,
:created_at,
:updated_at
])
|> validate_required([])
end
end
1 change: 1 addition & 0 deletions lib/os/lua.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ defmodule FarmbotOS.Lua do
get_position: &Firmware.get_position/2,
get_seed_tray_cell: &DataManipulation.get_seed_tray_cell/2,
get_xyz: &Info.get_xyz/2,
get_tool: &DataManipulation.get_tool/2,
go_to_home: &Firmware.go_to_home/2,
grid: &DataManipulation.grid/2,
group: &DataManipulation.group/2,
Expand Down
62 changes: 60 additions & 2 deletions lib/os/lua/data_manipulation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule FarmbotOS.Lua.DataManipulation do
"""

alias FarmbotOS.{Asset, JSON}
alias FarmbotOS.Asset.{Device, FbosConfig, FirmwareConfig}
alias FarmbotOS.Asset.{Repo, Tool, Device, FbosConfig, FirmwareConfig}
alias FarmbotOS.Lua.Util
alias FarmbotOS.Lua
alias FarmbotOS.SysCalls.ResourceUpdate
Expand Down Expand Up @@ -100,7 +100,28 @@ defmodule FarmbotOS.Lua.DataManipulation do

def update_device([table], lua) do
params = Map.new(table)
_ = ResourceUpdate.update_resource("Device", nil, params)

new_params =
if Map.has_key?(params, "mounted_tool_id") do
if params["mounted_tool_id"] == 0 do
Map.put(params, "mounted_tool_id", nil)
else
tool_ids = Repo.all(Tool) |> Enum.map(fn tool -> tool.id end)

if not Enum.member?(tool_ids, params["mounted_tool_id"]) do
FarmbotOS.Logger.error(3, "Tool ID not found.")

Enum.filter(params, fn {key, _value} -> key != "mounted_tool_id" end)
|> Map.new()
else
params
end
end
else
params
end

_ = ResourceUpdate.update_resource("Device", nil, new_params)
{[true], lua}
end

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

def get_tool([params], lua) do
map = Util.lua_to_elixir(params)

tool_id = Map.get(map, "id")
tool_name = Map.get(map, "name")
tool_params = %{}

tool_params =
if tool_id do
Map.put(tool_params, :id, tool_id)
else
tool_params
end

tool_params =
if tool_name do
Map.put(tool_params, :name, tool_name)
else
tool_params
end

tool = Asset.get_tool(tool_params)

tool_result =
if tool do
%{
id: tool.id,
name: tool.name,
flow_rate_ml_per_s: tool.flow_rate_ml_per_s
}
else
nil
end

{[tool_result], lua}
end

def new_sensor_reading([table], lua) do
table
|> Enum.map(fn
Expand Down
5 changes: 5 additions & 0 deletions lib/os/lua/result.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ defmodule FarmbotOS.Lua.Result do

def parse_error(error, _) do
show(error)

if Mix.env() == :test do
IO.puts(inspect(error))
end

{:error, "Lua failure"}
end

Expand Down
16 changes: 12 additions & 4 deletions lib/os/sys_calls/point_lookup.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,18 @@ defmodule FarmbotOS.SysCalls.PointLookup do
)
end

%{resource_type: type, resource_id: id}
|> Map.merge(s)
|> Map.take(@relevant_keys)
|> Map.put(:age, age)
p =
%{resource_type: type, resource_id: id}
|> Map.merge(s)
|> Map.take(@relevant_keys)
|> Map.put(:age, age)

if p.planted_at do
p
|> Map.put(:planted_at, DateTime.to_iso8601(s.planted_at))
else
p
end

other ->
Logger.debug("Point error: Please notify support #{inspect(other)}")
Expand Down
16 changes: 8 additions & 8 deletions priv/lua/dismount_tool.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ return function()
local start_time = os.time() * 1000

-- Checks
if not tool_id then
toast("No tool is mounted to the UTM", "error")
return
end
if not verify_tool() then
return
end
Expand All @@ -25,15 +29,11 @@ return function()
end

-- Get tool name
local tool = api({ url = "/api/tools/" .. tool_id })
if not tool then
toast("API error", "error")
return
end
local tool_name = get_tool{id = tool_id}.name

-- Checks
if not slot then
toast("No slot found for the currently mounted tool (" .. tool.name .. ") - check the Tools panel", "error")
toast("No slot found for the currently mounted tool (" .. tool_name .. ") - check the Tools panel", "error")
return
elseif slot_dir == 0 then
toast("Tool slot must have a direction", "error")
Expand All @@ -46,7 +46,7 @@ return function()
-- Job progress tracking
function job(percent, status)
set_job_progress(
"Dismounting " .. tool.name,
"Dismounting " .. tool_name,
{ percent = percent, status = status, time = start_time }
)
end
Expand Down Expand Up @@ -85,6 +85,6 @@ return function()
else
job(100, "Complete")
update_device({mounted_tool_id = 0})
toast(tool.name .. " dismounted", "success")
toast(tool_name .. " dismounted", "success")
end
end
18 changes: 5 additions & 13 deletions priv/lua/dispense.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,13 @@ return function(ml, params)
local tool_name = params.tool_name or "Watering Nozzle"
local pin_number = params.pin or 8

-- Get all tools
local tools = api({ url = "/api/tools/" })
if not tools then
toast("API error", "error")
-- Get flow_rate
local tool = get_tool{name = tool_name}
if not tool then
toast('Tool "' .. tool_name .. '" not found', 'error')
return
end

-- Pluck the nozzle
local nozzle, flow_rate
for key, tool in pairs(tools) do
if tool.name == tool_name then
nozzle = tool
flow_rate = nozzle.flow_rate_ml_per_s
end
end
local flow_rate = tool.flow_rate_ml_per_s

-- Checks
if not flow_rate then
Expand Down
19 changes: 3 additions & 16 deletions priv/lua/mount_tool.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,7 @@ return function(input)
if type(input) == "string" then
local prelim_tool
local tool_name = input
local tools = api({ url = "/api/tools/" })
if not tools then
toast("API error", "error")
return
end
for key, tool in pairs(tools) do
if tool.name == tool_name then
prelim_tool = tool
end
end
prelim_tool = get_tool{name = tool_name}
if not prelim_tool then
toast("'" .. tool_name .. "' tool not found", "error")
return
Expand Down Expand Up @@ -58,13 +49,9 @@ return function(input)
return
end

-- Get tool name
local tool = api({
url = "/api/tools/" .. slot.tool_id
})

local tool = get_tool{id = slot.tool_id}
if not tool then
toast("API error", "error")
toast("Tool slot must have a tool", "error")
return
end

Expand Down
Loading

0 comments on commit ebe7a24

Please sign in to comment.