Skip to content

Commit 16e3811

Browse files
Merge pull request #1515 from FarmBot/staging
v15.4.8
2 parents eade856 + 4d1d7d2 commit 16e3811

File tree

8 files changed

+70
-5
lines changed

8 files changed

+70
-5
lines changed

CHANGELOG.md

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

3+
# 15.4.8
4+
5+
* Add support for using remote `plant` objects in the `water` Lua helper.
6+
* Add `to_unix` Lua helper.
7+
* Add `planted_at` to local `plant` objects.
8+
39
# 15.4.7
410

511
* Firmware update to fix calibration deadzone settings.

VERSION

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

lib/os/lua.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ defmodule FarmbotOS.Lua do
180180
update_firmware_config: &DataManipulation.update_firmware_config/2,
181181
utc: &Info.utc/2,
182182
local_time: &Info.local_time/2,
183+
to_unix: &Info.to_unix/2,
183184
verify_tool: &DataManipulation.verify_tool/2,
184185
wait_ms: &Wait.wait/2,
185186
wait: &DataManipulation.wait/2,

lib/os/lua/info.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ defmodule FarmbotOS.Lua.Info do
104104
Timex.Timezone.convert(datetime, tz)
105105
end
106106

107+
def to_unix([datetime_string], lua) do
108+
{:ok, datetime, _} = DateTime.from_iso8601(datetime_string)
109+
{[DateTime.to_unix(datetime)], lua}
110+
end
111+
107112
@doc "Returns the current month"
108113
def current_month(_args, lua) do
109114
{[DateTime.utc_now().month], lua}

lib/os/sys_calls/point_lookup.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ defmodule FarmbotOS.SysCalls.PointLookup do
1414
:name,
1515
:openfarm_slug,
1616
:plant_stage,
17+
:planted_at,
1718
:depth,
1819
:water_curve_id,
1920
:spread_curve_id,

priv/lua/spec/water_spec.lua

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
local water = require("water")
22

3+
_G.os.time = spy.new(function() return 100 end)
4+
_G.to_unix = spy.new(function() return 100 - 10 * 86400 end)
35
_G.dispense = spy.new(function() end)
46
_G.get_curve = spy.new(function()
57
return { day = function() return 100 end }
@@ -22,7 +24,7 @@ describe("water()", function()
2224
_G.complete_job:clear()
2325
end)
2426

25-
it("gets water amount and calls dispense()", function()
27+
it("gets water amount from age and calls dispense()", function()
2628
local plant = {
2729
name = "Plant",
2830
age = 10,
@@ -43,6 +45,27 @@ describe("water()", function()
4345
assert.spy(complete_job).was.called_with("Watering Plant at (1, 2)")
4446
end)
4547

48+
it("gets water amount from planted_at and calls dispense()", function()
49+
local plant = {
50+
name = "Plant",
51+
planted_at = "2021-03-31T19:42:18.173Z",
52+
water_curve_id = 1,
53+
x = 1,
54+
y = 2,
55+
z = 3,
56+
}
57+
water(plant)
58+
59+
assert.spy(get_curve).was.called_with(1)
60+
assert.spy(toast).was_not_called()
61+
assert.spy(set_job).was.called_with("Watering Plant at (1, 2)", { status = "Moving" })
62+
assert.spy(move).was.called_with({x = 1, y = 2, z = 0})
63+
assert.spy(set_job).was.called_with("Watering Plant at (1, 2)", { status = "Watering", percent = 50 })
64+
assert.spy(send_message).was.called_with("info", "Watering 10 day old Plant at (1, 2) 100mL")
65+
assert.spy(dispense).was.called_with(100, nil)
66+
assert.spy(complete_job).was.called_with("Watering Plant at (1, 2)")
67+
end)
68+
4669
it("passes params", function()
4770
local plant = {
4871
name = "Plant",

priv/lua/water.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@ return function(plant, params)
22
local plant_name_xy = plant.name .. " at (" .. plant.x .. ", " .. plant.y .. ")"
33
local job_name = "Watering " .. plant_name_xy
44

5-
if not plant.age then
5+
if not plant.age and not plant.planted_at then
66
toast(plant_name_xy .. " has not been planted yet. Skipping.", "warn")
77
return
88
end
99

10+
if plant.age then
11+
plant_age = plant.age
12+
else
13+
plant_age = math.ceil((os.time() - to_unix(plant.planted_at)) / 86400)
14+
end
15+
1016
-- Get water curve and water amount in mL
1117
local water_curve, water_ml
1218
if plant.water_curve_id then
1319
water_curve = get_curve(plant.water_curve_id)
14-
water_ml = water_curve.day(plant.age)
20+
water_ml = water_curve.day(plant_age)
1521
else
1622
toast(plant_name_xy .. " has no assigned water curve. Skipping.", "warn")
1723
return
@@ -23,7 +29,7 @@ return function(plant, params)
2329

2430
-- Water the plant
2531
set_job(job_name, { status = "Watering", percent = 50 })
26-
send_message("info", "Watering " .. plant.age .. " day old " .. plant_name_xy .. " " .. water_ml .. "mL")
32+
send_message("info", "Watering " .. plant_age .. " day old " .. plant_name_xy .. " " .. water_ml .. "mL")
2733
dispense(water_ml, params)
2834
complete_job(job_name)
2935
end

test/os/lua/ext/info_test.exs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ defmodule FarmbotOS.Lua.InfoTest do
154154
assert actual == day
155155
end
156156

157+
test "utc(\"hour\")" do
158+
hour = DateTime.utc_now().hour
159+
lua_code = "return utc(\"hour\")"
160+
{:ok, [actual]} = lua(lua_code, lua_code)
161+
assert actual == hour
162+
end
163+
157164
test "utc(\"minute\")" do
158165
minute = DateTime.utc_now().minute
159166
lua_code = "return utc(\"minute\")"
@@ -204,6 +211,15 @@ defmodule FarmbotOS.Lua.InfoTest do
204211
assert actual == day
205212
end
206213

214+
test "local_time(\"hour\")" do
215+
tz = "America/Chicago"
216+
local = Timex.Timezone.convert(DateTime.utc_now(), tz)
217+
hour = local.hour
218+
lua_code = "return local_time(\"hour\")"
219+
{:ok, [actual]} = lua(lua_code, lua_code)
220+
assert actual == hour
221+
end
222+
207223
test "local_time(\"minute\")" do
208224
tz = "America/Chicago"
209225
local = Timex.Timezone.convert(DateTime.utc_now(), tz)
@@ -222,6 +238,13 @@ defmodule FarmbotOS.Lua.InfoTest do
222238
assert actual == second
223239
end
224240

241+
test "to_unix()" do
242+
now = DateTime.to_unix(DateTime.utc_now())
243+
lua_code = "return to_unix(utc())"
244+
{:ok, [actual]} = lua(lua_code, lua_code)
245+
assert actual == now
246+
end
247+
225248
test "current_month()" do
226249
month = DateTime.utc_now().month
227250
lua_code = "return current_month()"

0 commit comments

Comments
 (0)