From 1f4fcf6b86e3de911c4bd95aa8470cd018e644d1 Mon Sep 17 00:00:00 2001 From: gabrielburnworth Date: Thu, 23 May 2024 13:37:19 -0700 Subject: [PATCH 1/2] [v15.4.8-rc0] add lua plant age calculation --- CHANGELOG.md | 6 ++++++ VERSION | 2 +- lib/os/lua.ex | 1 + lib/os/lua/info.ex | 5 +++++ lib/os/sys_calls/point_lookup.ex | 1 + priv/lua/spec/water_spec.lua | 25 ++++++++++++++++++++++++- priv/lua/water.lua | 12 +++++++++--- test/os/lua/ext/info_test.exs | 23 +++++++++++++++++++++++ 8 files changed, 70 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68946ddf3..c5ca4cd64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +# 15.4.8 + + * Add support for using remote `plant` objects in the `water` Lua helper. + * Add `to_unix` Lua helper. + * Add `planted_at` to local `plant` objects. + # 15.4.7 * Firmware update to fix calibration deadzone settings. diff --git a/VERSION b/VERSION index b085e61eb..ac09a8466 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -15.4.7 \ No newline at end of file +15.4.8-rc0 \ No newline at end of file diff --git a/lib/os/lua.ex b/lib/os/lua.ex index 8ee41e9c0..de23aa6ab 100644 --- a/lib/os/lua.ex +++ b/lib/os/lua.ex @@ -180,6 +180,7 @@ defmodule FarmbotOS.Lua do update_firmware_config: &DataManipulation.update_firmware_config/2, utc: &Info.utc/2, local_time: &Info.local_time/2, + to_unix: &Info.to_unix/2, verify_tool: &DataManipulation.verify_tool/2, wait_ms: &Wait.wait/2, wait: &DataManipulation.wait/2, diff --git a/lib/os/lua/info.ex b/lib/os/lua/info.ex index 95c3b3ea0..4a959b6fe 100644 --- a/lib/os/lua/info.ex +++ b/lib/os/lua/info.ex @@ -104,6 +104,11 @@ defmodule FarmbotOS.Lua.Info do Timex.Timezone.convert(datetime, tz) end + def to_unix([datetime_string], lua) do + {:ok, datetime, _} = DateTime.from_iso8601(datetime_string) + {[DateTime.to_unix(datetime)], lua} + end + @doc "Returns the current month" def current_month(_args, lua) do {[DateTime.utc_now().month], lua} diff --git a/lib/os/sys_calls/point_lookup.ex b/lib/os/sys_calls/point_lookup.ex index 6f04edc43..899774a9d 100644 --- a/lib/os/sys_calls/point_lookup.ex +++ b/lib/os/sys_calls/point_lookup.ex @@ -14,6 +14,7 @@ defmodule FarmbotOS.SysCalls.PointLookup do :name, :openfarm_slug, :plant_stage, + :planted_at, :depth, :water_curve_id, :spread_curve_id, diff --git a/priv/lua/spec/water_spec.lua b/priv/lua/spec/water_spec.lua index 63da843e8..6ad673aa8 100644 --- a/priv/lua/spec/water_spec.lua +++ b/priv/lua/spec/water_spec.lua @@ -1,5 +1,7 @@ local water = require("water") +_G.os.time = spy.new(function() return 100 end) +_G.to_unix = spy.new(function() return 100 - 10 * 86400 end) _G.dispense = spy.new(function() end) _G.get_curve = spy.new(function() return { day = function() return 100 end } @@ -22,7 +24,7 @@ describe("water()", function() _G.complete_job:clear() end) - it("gets water amount and calls dispense()", function() + it("gets water amount from age and calls dispense()", function() local plant = { name = "Plant", age = 10, @@ -43,6 +45,27 @@ describe("water()", function() assert.spy(complete_job).was.called_with("Watering Plant at (1, 2)") end) + it("gets water amount from planted_at and calls dispense()", function() + local plant = { + name = "Plant", + planted_at = "2021-03-31T19:42:18.173Z", + water_curve_id = 1, + x = 1, + y = 2, + z = 3, + } + water(plant) + + assert.spy(get_curve).was.called_with(1) + assert.spy(toast).was_not_called() + assert.spy(set_job).was.called_with("Watering Plant at (1, 2)", { status = "Moving" }) + assert.spy(move).was.called_with({x = 1, y = 2, z = 0}) + assert.spy(set_job).was.called_with("Watering Plant at (1, 2)", { status = "Watering", percent = 50 }) + assert.spy(send_message).was.called_with("info", "Watering 10 day old Plant at (1, 2) 100mL") + assert.spy(dispense).was.called_with(100, nil) + assert.spy(complete_job).was.called_with("Watering Plant at (1, 2)") + end) + it("passes params", function() local plant = { name = "Plant", diff --git a/priv/lua/water.lua b/priv/lua/water.lua index d9ba8e827..b35777cd1 100644 --- a/priv/lua/water.lua +++ b/priv/lua/water.lua @@ -2,16 +2,22 @@ return function(plant, params) local plant_name_xy = plant.name .. " at (" .. plant.x .. ", " .. plant.y .. ")" local job_name = "Watering " .. plant_name_xy - if not plant.age then + if not plant.age and not plant.planted_at then toast(plant_name_xy .. " has not been planted yet. Skipping.", "warn") return end + if plant.age then + plant_age = plant.age + else + plant_age = math.ceil((os.time() - to_unix(plant.planted_at)) / 86400) + end + -- Get water curve and water amount in mL local water_curve, water_ml if plant.water_curve_id then water_curve = get_curve(plant.water_curve_id) - water_ml = water_curve.day(plant.age) + water_ml = water_curve.day(plant_age) else toast(plant_name_xy .. " has no assigned water curve. Skipping.", "warn") return @@ -23,7 +29,7 @@ return function(plant, params) -- Water the plant set_job(job_name, { status = "Watering", percent = 50 }) - send_message("info", "Watering " .. plant.age .. " day old " .. plant_name_xy .. " " .. water_ml .. "mL") + send_message("info", "Watering " .. plant_age .. " day old " .. plant_name_xy .. " " .. water_ml .. "mL") dispense(water_ml, params) complete_job(job_name) end diff --git a/test/os/lua/ext/info_test.exs b/test/os/lua/ext/info_test.exs index cbf965fbc..7422f7599 100644 --- a/test/os/lua/ext/info_test.exs +++ b/test/os/lua/ext/info_test.exs @@ -154,6 +154,13 @@ defmodule FarmbotOS.Lua.InfoTest do assert actual == day end + test "utc(\"hour\")" do + hour = DateTime.utc_now().hour + lua_code = "return utc(\"hour\")" + {:ok, [actual]} = lua(lua_code, lua_code) + assert actual == hour + end + test "utc(\"minute\")" do minute = DateTime.utc_now().minute lua_code = "return utc(\"minute\")" @@ -204,6 +211,15 @@ defmodule FarmbotOS.Lua.InfoTest do assert actual == day end + test "local_time(\"hour\")" do + tz = "America/Chicago" + local = Timex.Timezone.convert(DateTime.utc_now(), tz) + hour = local.hour + lua_code = "return local_time(\"hour\")" + {:ok, [actual]} = lua(lua_code, lua_code) + assert actual == hour + end + test "local_time(\"minute\")" do tz = "America/Chicago" local = Timex.Timezone.convert(DateTime.utc_now(), tz) @@ -222,6 +238,13 @@ defmodule FarmbotOS.Lua.InfoTest do assert actual == second end + test "to_unix()" do + now = DateTime.to_unix(DateTime.utc_now()) + lua_code = "return to_unix(utc())" + {:ok, [actual]} = lua(lua_code, lua_code) + assert actual == now + end + test "current_month()" do month = DateTime.utc_now().month lua_code = "return current_month()" From 4d1d7d202062caa5921fd179c36ffc66092e42f6 Mon Sep 17 00:00:00 2001 From: gabrielburnworth Date: Tue, 28 May 2024 12:25:23 -0700 Subject: [PATCH 2/2] v15.4.8 --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index ac09a8466..11f8064c9 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -15.4.8-rc0 \ No newline at end of file +15.4.8 \ No newline at end of file