@jsimmonds Hi John
I have re-written the water LUA code to incorporate a verification loop to check 3 times over a minute (Every 20 seconds)
– New code
theNumberOfAllowableNetworkTries = 3
theSecondsDelayBetweenRetries = 20
failedToGetPoints = 1
while( theNumberOfAllowableNetworkTries > 0 )
do
theNumberOfAllowableNetworkTries = theNumberOfAllowableNetworkTries -1
local points = api({method = "GET", url = "/api/points"})
if result then
toast("Plant list obtained", "info")
theNumberOfAllowableNetworkTries = 0
failedToGetPoints = 0
else
toast("Error - Unable to obtain list of plants (Retrying)" , "warn")
wait (theSecondsDelayBetweenRetries * 1000)
theNumberOfAllowableNetworkTries = theNumberOfAllowableNetworkTries -1
end
end
if failedToGetPoints then
toast("Fatal Error - Unable to obtain list of plants (EXIT)", "error")
exit (1)
end
-- New End code
local plants = {}
for k, v in pairs(points) do
if v.pointer_type == "Plant" then
table.insert(plants, {name = v.name, x = v.x, y = v.y})
end
end
table.sort(plants, function(l, r)
-- "close enough" approximation.
if math.abs(l.x - r.x) < 150 then
return l.y < r.y
else
return l.x < r.x
end
end)
count = 0
total = #plants
job = "Watering all " .. total .. " plants"
send_message(
"info",
"Watering all " .. total .. " plants for " .. watering_time .. " seconds each",
"toast")
for k, v in pairs(plants) do
coordinates = "(" .. v.x .. ", " .. v.y .. ")"
set_job_progress(job, {
percent = 100 * (count) / total,
status = "Moving to " .. (v.name or "plant") .. " at " .. coordinates,
time = start_time
})
move_absolute(v.x, v.y, 0)
set_job_progress(job, {
percent = 100 * (count + 0.5) / total,
status = "Watering " .. (v.name or "plant") .. " for " .. watering_time .. " seconds",
time = start_time
})
write_pin(8, "digital", 1)
wait(watering_time * 1000)
write_pin(8, "digital", 0)
count = count + 1
end
set_job_progress(job, {
percent = 100,
status = "Complete",
time = start_time
})
@jsimmonds
It always failed to detect if the API has returned the correct data. Can you look at where I have gone wrong? This is my first attempt at coding LUA and I pinched the template from this
I would have thought that the API read would also have the same detection as the documentation says *
Returns nil
if there was an error.
– Create a new point at (200, 200, 0) with a radius of 100
result = api({
method = “post”,
url = “/api/points”,
body = {
x = 200,
y = 200,
z = 0,
radius = 100,
pointer_type = “GenericPointer”
}
})
if result then
toast(“Point creation ok”, “debug”)
else
toast(“Error - See logs for details”, “error”)
end