Skip to content

Commit

Permalink
Merge pull request #1515 from FarmBot/staging
Browse files Browse the repository at this point in the history
v15.4.8
  • Loading branch information
gabrielburnworth authored May 28, 2024
2 parents eade856 + 4d1d7d2 commit 16e3811
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15.4.7
15.4.8
1 change: 1 addition & 0 deletions lib/os/lua.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions lib/os/lua/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
1 change: 1 addition & 0 deletions lib/os/sys_calls/point_lookup.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defmodule FarmbotOS.SysCalls.PointLookup do
:name,
:openfarm_slug,
:plant_stage,
:planted_at,
:depth,
:water_curve_id,
:spread_curve_id,
Expand Down
25 changes: 24 additions & 1 deletion priv/lua/spec/water_spec.lua
Original file line number Diff line number Diff line change
@@ -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 }
Expand All @@ -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,
Expand All @@ -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",
Expand Down
12 changes: 9 additions & 3 deletions priv/lua/water.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
23 changes: 23 additions & 0 deletions test/os/lua/ext/info_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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\")"
Expand Down Expand Up @@ -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)
Expand All @@ -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()"
Expand Down

0 comments on commit 16e3811

Please sign in to comment.