Finding all the Group ids

Hello,

I wrote an LUA program that finds the middle point between the first two plants in each plant group. However, I cannot seem to find a way to get all the group IDs, so in my program below, I had to manually add them.

– Define a table with group names and their corresponding IDs
local groups = {
PEPPERS = 95078,
BRUSSELS_SPROUTS = 95079,
CELERY = 95080,
CUCUMBER = 95081,
EGGPLANT = 95082,
KALE = 95083,
LETTUCE = 95084,
BLACK_SEEDED_SIMPSON_LETTUCE = 95583,
WHITE_ONION = 95584,
SPINACH = 95087,
STRAWBERRY = 95089,
ASPARAGUS = 95090,
GREEN_ZUCCHINI = 95091,
SNOW_PEA = 95092
}

– Define rectangles where plant midpoints should not be located
local rectangles = {
{x1 = 0, y1 = 1200, x2 = 5710, y2 = 1630},
{x1 = 4582, y1 = 0, x2 = 4700, y2 = 2732},
{x1 = 3321, y1 = 0, x2 = 3450, y2 = 2732},
{x1 = 2071, y1 = 0, x2 = 2200, y2 = 2732},
{x1 = 835, y1 = 0, x2 = 955, y2 = 2732}
}

– Function to check if a point is within any rectangle
local function is_in_rectangle(x, y)
for _, rect in ipairs(rectangles) do
if x >= rect.x1 and x <= rect.x2 and y >= rect.y1 and y <= rect.y2 then
return true
end
end
return false
end

– Loop over each group by name and ID
for name, id in pairs(groups) do
– Retrieve the group by ID
local group_id = id
local group_members = group(group_id) – Assuming ‘group’ function retrieves group details

-- Check if group retrieval is successful
if not group_members or #group_members < 2 then
    send_message("error", "Failed to retrieve group with ID: " .. group_id .. " (" .. name .. ") or not enough members", "toast")
else
    -- Get details of the first two plants
    local plant1 = api({ method = "get", url = "/api/points/" .. group_members[1] })
    local plant2 = api({ method = "get", url = "/api/points/" .. group_members[2] })
    
    if not plant1 or not plant2 then
        send_message("error", "Failed to retrieve plant details", "toast")
    else
        -- Calculate the midpoint between the two plants
        local midpoint_x = (plant1.x + plant2.x) / 2
        local midpoint_y = (plant1.y + plant2.y) / 2

        -- Check if the midpoint is in any of the restricted rectangles
        if is_in_rectangle(midpoint_x, midpoint_y) then
            send_message("warn", "Midpoint between " .. (plant1.name or "Unnamed Plant 1") .. " and " .. (plant2.name or "Unnamed Plant 2") .. " is within a restricted area at (" .. midpoint_x .. ", " .. midpoint_y .. ")", "toast")
        else
            send_message("info", "Midpoint between " .. (plant1.name or "Unnamed Plant 1") .. " and " .. (plant2.name or "Unnamed Plant 2") .. " is at (" .. midpoint_x .. ", " .. midpoint_y .. ")", "toast")
        end
    end
end

end

I’m not sure what’s your aim. However this might help

--- Make API-Call to get all groups
groups = api({
    method = "GET",
    url = "/api/point_groups/"
})
-- Get plants of each group
for _, ID in pairs(groups) do
Plants = group(ID.id)
...
1 Like

Thanks, I would like to understand the structure of the point_groups API call’s returned object. Is there any documentation?

JSON objects are “self-documenting” :wink:
For example, here’s part of the Point Group array returned for my account . . parsed by the JSON.parse() JS function and then pretty-printed :window:

7 May 10:31:47 - [
  {
    id: 95793,
    created_at: '2024-05-06T23:52:31.224Z',
    updated_at: '2024-05-07T00:04:23.918Z',
    name: 'Update Date Group',
    point_ids: [],
    sort_type: 'nn',
    criteria: {
      day: { op: '<', days_ago: 0 },
      string_eq: { pointer_type: [ 'Plant' ], plant_stage: [ 'sprouted' ] },
      number_eq: {},
      number_lt: {},
      number_gt: {}
    }
  },
  {
    id: 25360,
    created_at: '2021-06-16T03:30:57.923Z',
    updated_at: '2021-06-16T03:31:18.474Z',
    name: 'Weedy',
    point_ids: [],
    sort_type: 'xy_ascending',
    criteria: {
      day: { op: '<', days_ago: 0 },
      string_eq: { pointer_type: [ 'Weed' ] },
      number_eq: {},

 . . . etc.

 ]