Skip to content

Commit

Permalink
add new job lua helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielburnworth committed Apr 6, 2023
1 parent 710a876 commit a5fa2ef
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# 15.4.1

* Add new `dispense`, `water`, and `grid` lua helpers.
* Add new `dispense`, `water`, `grid`, `set_job`, `complete_job`, and `get_job` lua helpers.
* Add job progress tracking to `wait` lua helper.
* Add `z` to `garden_size` and add `0` fallback values.
* Fix `photo_grid` lua helper calculation bug.
Expand Down
3 changes: 3 additions & 0 deletions lib/os/lua.ex
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ defmodule FarmbotOS.Lua do
],
calibrate_camera: execute_script("camera-calibration"),
check_position: &Firmware.check_position/2,
complete_job: &Info.complete_job/2,
coordinate: &Firmware.coordinate/2,
cs_eval: &FarmbotOS.Celery.execute_from_lua/2,
current_hour: &Info.current_hour/2,
Expand All @@ -132,6 +133,7 @@ defmodule FarmbotOS.Lua do
get_device: &DataManipulation.get_device/2,
get_fbos_config: &DataManipulation.get_fbos_config/2,
get_firmware_config: &DataManipulation.get_firmware_config/2,
get_job: &Info.get_job/2,
get_job_progress: &Info.get_job_progress/2,
get_position: &Firmware.get_position/2,
get_seed_tray_cell: &DataManipulation.get_seed_tray_cell/2,
Expand Down Expand Up @@ -160,6 +162,7 @@ defmodule FarmbotOS.Lua do
debug: &Info.debug/2,
toast: &Info.toast/2,
safe_z: &DataManipulation.safe_z/2,
set_job: &Info.set_job/2,
set_job_progress: &Info.set_job_progress/2,
set_pin_io_mode: &Firmware.set_pin_io_mode/2,
soft_stop: &Firmware.soft_stop/2,
Expand Down
44 changes: 43 additions & 1 deletion lib/os/lua/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,16 @@ defmodule FarmbotOS.Lua.Info do
{[job], lua}
end

def get_job([name], lua) do
get_job_progress([name], lua)
end

def set_job_progress([name, args], lua) do
map = FarmbotOS.Lua.Util.lua_to_elixir(args)

job = %FarmbotOS.BotState.JobProgress.Percent{
type: Map.get(map, "type") || "unknown",
status: Map.get(map, "status") || "working",
status: Map.get(map, "status") || "Working",
percent: Map.get(map, "percent") || 0,
time: Map.get(map, "time") || nil
}
Expand All @@ -122,6 +126,44 @@ defmodule FarmbotOS.Lua.Info do
{[], lua}
end

def set_job([name], lua) do
set_job([name, []], lua)
end

def set_job([name, args], lua) do
map = FarmbotOS.Lua.Util.lua_to_elixir(args)
{[existing_job], _} = get_job_progress([name], lua)
existing = FarmbotOS.Lua.Util.lua_to_elixir(existing_job) || %{}

existing_map =
if Map.get(existing, :status) == "Complete" do
%{}
else
existing
end

now = DateTime.to_unix(DateTime.utc_now()) * 1000
time = Map.get(map, "time") || Map.get(existing_map, :time) || now

job = %{
type: Map.get(map, "type") || Map.get(existing_map, :type),
status: Map.get(map, "status") || Map.get(existing_map, :status),
percent: Map.get(map, "percent") || Map.get(existing_map, :percent),
time: time
}

set_job_progress([name, FarmbotOS.Lua.Util.map_to_table(job)], lua)
end

def complete_job([name], lua) do
job = %{
status: "Complete",
percent: 100
}

set_job([name, FarmbotOS.Lua.Util.map_to_table(job)], lua)
end

defp do_send_message(kind, message, channels, lua) do
result = SysCallGlue.send_message(kind, "#{message}", channels)

Expand Down
18 changes: 18 additions & 0 deletions test/os/lua_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,30 @@ defmodule FarmbotOS.LuaTest do
end

test "job setters/getters" do
expect(FarmbotOS.BotState, :set_job_progress, 4, fn
"foo", _ -> {:ok, []}
end)

expect(FarmbotOS.BotState, :fetch, 2, fn -> %{:jobs => %{"foo" => %{}}} end)

expect(FarmbotOS.BotState, :fetch, 3, fn ->
%{:jobs => %{"foo" => %{:unit => "percent", :status => "Complete"}}}
end)

fns = Lua.builtins()
name = "foo"
args = [type: "bar", status: "baz", percent: 42.0]
lua = %{}
result = fns.set_job_progress.([name, args], lua)
assert result == {[], %{}}
result = fns.set_job.([name], lua)
assert result == {[], %{}}
result = fns.complete_job.([name], lua)
assert result == {[], %{}}
result = fns.set_job.([name, args], lua)
assert result == {[], %{}}
{[result], %{}} = fns.get_job.([name], lua)
assert result.unit == "percent"
{[result2], %{}} = fns.get_job_progress.([name], lua)
assert result2.unit == "percent"
end
Expand Down

0 comments on commit a5fa2ef

Please sign in to comment.