Compare commits

..
Author SHA1 Message Date
Cédric Verstraeten 3d65b42f32 Merge pull request #368 from EpicMouse123/feature/region-multi-polygon
Support multiple polygons in AGENT_REGION_POLYGON
2026-11-27 11:54:50 +01:00
adam.witwickiandClaude Opus 5.5 59f43d2877 Support multiple polygons in AGENT_REGION_POLYGON
AGENT_REGION_POLYGON could only describe one motion region, although
motion detection already evaluates several regions independently. This
left agents configured through environment variables (ConfigMap mode)
unable to use multiple regions.

Polygons can now be separated by "|":

  AGENT_REGION_POLYGON="64,36;576,36;608,324;32,324|700,40;900,40;900,300"

Each part becomes its own polygon with IDs "0", "1", ... assigned in
order. Empty parts and parts with fewer than 3 valid points are skipped.
A value without "|" parses as before.

Closes #366

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 10:15:00 +00:00
4 changed files with 151 additions and 55 deletions
+24 -25
View File
@@ -8,29 +8,21 @@ on:
workflow_dispatch:
jobs:
# Build one image per architecture. Only architectures we also ship in
# releases (amd64 + arm64): the golang/debian trixie base images don't
# provide arm/v6, which made every nightly fail since February.
nightly-build:
# Nightlies always build the latest master: schedules run on the default
# branch, and manual runs from any other branch are skipped.
if: github.ref == 'refs/heads/master'
nightly-build-amd64:
runs-on: ubuntu-latest
strategy:
# Let every architecture finish, so one broken build doesn't hide the others.
fail-fast: false
matrix:
architecture: [amd64, arm64]
architecture: [amd64]
steps:
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
# Check out the triggering commit (master HEAD) so the code matches the
# $GITHUB_SHA used in the image tags.
- name: Checkout
uses: actions/checkout@v4
with:
ref: master
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
@@ -39,25 +31,32 @@ jobs:
- name: Available platforms
run: echo ${{ steps.buildx.outputs.platforms }}
- name: Run Buildx
run: docker buildx build --build-arg VERSION=$(echo $GITHUB_SHA | cut -c1-7) --platform linux/${{matrix.architecture}} -t kerberos/agent-nightly:arch-${{matrix.architecture}}-$(echo $GITHUB_SHA | cut -c1-7) --push .
# Create the multi-arch manifest once, after all architectures are pushed.
# Previously each build job created/appended to it in parallel, so the
# result depended on which job finished last.
nightly-manifest:
run: docker buildx build --build-arg VERSION=$(echo $GITHUB_SHA | cut -c1-7) --platform linux/${{matrix.architecture}} -t kerberos/agent-nightly:arch-$(echo ${{matrix.architecture}} | tr / -)-$(echo $GITHUB_SHA | cut -c1-7) --push .
- name: Create new and append to manifest
run: docker buildx imagetools create -t kerberos/agent-nightly:$(echo $GITHUB_SHA | cut -c1-7) kerberos/agent-nightly:arch-$(echo ${{matrix.architecture}} | tr / -)-$(echo $GITHUB_SHA | cut -c1-7)
nightly-build-other:
runs-on: ubuntu-latest
needs: nightly-build
strategy:
matrix:
architecture: [arm64, arm/v7, arm/v6]
steps:
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Checkout
uses: actions/checkout@v4
with:
ref: master
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v3
- name: Create manifest
run: |
SHORT_SHA=$(echo $GITHUB_SHA | cut -c1-7)
docker buildx imagetools create -t kerberos/agent-nightly:$SHORT_SHA \
kerberos/agent-nightly:arch-amd64-$SHORT_SHA \
kerberos/agent-nightly:arch-arm64-$SHORT_SHA
- name: Available platforms
run: echo ${{ steps.buildx.outputs.platforms }}
- name: Run Buildx
run: docker buildx build --build-arg VERSION=$(echo $GITHUB_SHA | cut -c1-7) --platform linux/${{matrix.architecture}} -t kerberos/agent-nightly:arch-$(echo ${{matrix.architecture}} | tr / -)-$(echo $GITHUB_SHA | cut -c1-7) --push .
- name: Create new and append to manifest
run: docker buildx imagetools create --append -t kerberos/agent-nightly:$(echo $GITHUB_SHA | cut -c1-7) kerberos/agent-nightly:arch-$(echo ${{matrix.architecture}} | tr / -)-$(echo $GITHUB_SHA | cut -c1-7)
+1 -1
View File
@@ -336,7 +336,7 @@ Notes:
| `AGENT_AUTO_CLEAN_MIN_FREE_SPACE` | When `AUTO_CLEAN` is enabled and no `MAX_SIZE` is set, keep at least this much free space (in MB) on the recordings disk before deleting the oldest (already-uploaded first) recordings. Defaults to 5% of the disk. | "" |
| `AGENT_TIME` | Enable the timetable for Kerberos Agent | "false" |
| `AGENT_TIMETABLE` | A (weekly) time table to specify when to make recordings "start1,end1,start2,end2;start1.. | "" |
| `AGENT_REGION_POLYGON` | A single polygon set for motion detection: "x1,y1;x2,y2;x3,y3;... | "" |
| `AGENT_REGION_POLYGON` | One or more polygons for motion detection. Points are "x,y" separated by ";", polygons are separated by "\|": "x1,y1;x2,y2;x3,y3\|x1,y1;x2,y2;x3,y3". Each polygon needs at least 3 points. | "" |
| `AGENT_CAPTURE_IPCAMERA_RTSP` | Full-HD RTSP or RTSPS endpoint for the target camera. | "" |
| `AGENT_CAPTURE_IPCAMERA_RTSP_TRANSPORT` | Transport for the RTSP streams: "tcp", "udp" or "auto" (UDP first, falls back to TCP). | "tcp" |
| `AGENT_CAPTURE_IPCAMERA_SUB_RTSP` | RTSP or RTSPS sub-stream endpoint used for livestreaming (WebRTC). | "" |
+35 -29
View File
@@ -308,6 +308,39 @@ func initConfigPointers(config *models.Config) {
}
}
// parseRegionPolygons parses the AGENT_REGION_POLYGON value into motion
// regions. Points are "x,y" pairs separated by ";", and several polygons can
// be given separated by "|", e.g. "0,0;10,0;10,10|20,20;30,20;30,30". Each
// part becomes its own polygon, with IDs "0", "1", ... assigned in order.
// Invalid points are ignored, and empty parts or parts with fewer than 3 valid
// points are skipped.
func parseRegionPolygons(value string) []models.Polygon {
polygons := []models.Polygon{}
for _, part := range strings.Split(value, "|") {
var coordinates []models.Coordinate
for _, pair := range strings.Split(part, ";") {
xy := strings.Split(strings.TrimSpace(pair), ",")
if len(xy) != 2 {
continue
}
x, errX := strconv.ParseFloat(strings.TrimSpace(xy[0]), 64)
y, errY := strconv.ParseFloat(strings.TrimSpace(xy[1]), 64)
if errX != nil || errY != nil {
continue
}
coordinates = append(coordinates, models.Coordinate{X: x, Y: y})
}
if len(coordinates) < 3 {
continue
}
polygons = append(polygons, models.Polygon{
ID: strconv.Itoa(len(polygons)),
Coordinates: coordinates,
})
}
return polygons
}
// applyAgentEnvVars applies the AGENT_* environment variables (optionally
// carrying the given prefix, e.g. "GLOBAL_") onto configuration.Config. When
// applyDefaults is true, defaults (such as the signing key) are applied after
@@ -503,35 +536,8 @@ func applyAgentEnvVars(configuration *models.Configuration, prefix string, apply
break
case "AGENT_REGION_POLYGON":
var coordinates []models.Coordinate
// Convert value to coordinates array
// 0,0;1,1;2,2;3,3
coordinatesString := strings.Split(value, ";")
for _, coordinateString := range coordinatesString {
coordinate := strings.Split(coordinateString, ",")
if len(coordinate) == 2 {
x, err := strconv.ParseFloat(coordinate[0], 64)
if err != nil {
continue
}
y, err := strconv.ParseFloat(coordinate[1], 64)
if err != nil {
continue
}
coordinates = append(coordinates, models.Coordinate{
X: x,
Y: y,
})
}
}
configuration.Config.Region.Polygon = []models.Polygon{
{
Coordinates: coordinates,
ID: "0",
},
}
// One or more polygons: "x,y;x,y;x,y|x,y;x,y;x,y"
configuration.Config.Region.Polygon = parseRegionPolygons(value)
break
/* MQTT settings for bi-directional communication */
+91
View File
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"os"
"reflect"
"testing"
"time"
@@ -152,3 +153,93 @@ func TestFactoryConfigRetryableErrors(t *testing.T) {
t.Fatal("decode errors should not retry")
}
}
func TestParseRegionPolygons(t *testing.T) {
square := []models.Coordinate{{X: 0, Y: 0}, {X: 10, Y: 0}, {X: 10, Y: 10}, {X: 0, Y: 10}}
triangle := []models.Coordinate{{X: 20, Y: 20}, {X: 30, Y: 20}, {X: 30, Y: 30}}
tests := []struct {
name string
value string
want []models.Polygon
}{
{
name: "single polygon parses as before",
value: "0,0;10,0;10,10;0,10",
want: []models.Polygon{{ID: "0", Coordinates: square}},
},
{
name: "multiple polygons separated by pipe",
value: "0,0;10,0;10,10;0,10|20,20;30,20;30,30",
want: []models.Polygon{{ID: "0", Coordinates: square}, {ID: "1", Coordinates: triangle}},
},
{
name: "issue example",
value: "64,36;576,36;608,324;32,324|700,40;900,40;900,300",
want: []models.Polygon{
{ID: "0", Coordinates: []models.Coordinate{{X: 64, Y: 36}, {X: 576, Y: 36}, {X: 608, Y: 324}, {X: 32, Y: 324}}},
{ID: "1", Coordinates: []models.Coordinate{{X: 700, Y: 40}, {X: 900, Y: 40}, {X: 900, Y: 300}}},
},
},
{
name: "empty parts are skipped and ids stay sequential",
value: "|0,0;10,0;10,10;0,10||20,20;30,20;30,30|",
want: []models.Polygon{{ID: "0", Coordinates: square}, {ID: "1", Coordinates: triangle}},
},
{
name: "parts with fewer than 3 points are skipped",
value: "1,1;2,2|0,0;10,0;10,10;0,10|5,5",
want: []models.Polygon{{ID: "0", Coordinates: square}},
},
{
name: "invalid points are ignored before counting",
value: "0,0;x,1;10,0;10;10,10;0,10;",
want: []models.Polygon{{ID: "0", Coordinates: square}},
},
{
name: "whitespace is trimmed",
value: " 0, 0 ; 10,0;10 ,10; 0,10 | 20,20; 30,20 ;30,30 ",
want: []models.Polygon{{ID: "0", Coordinates: square}, {ID: "1", Coordinates: triangle}},
},
{
name: "decimal coordinates",
value: "0.5,0.5;10.25,0;10,10.75",
want: []models.Polygon{{ID: "0", Coordinates: []models.Coordinate{{X: 0.5, Y: 0.5}, {X: 10.25, Y: 0}, {X: 10, Y: 10.75}}}},
},
{name: "empty value", value: "", want: []models.Polygon{}},
{name: "only separators", value: "||", want: []models.Polygon{}},
{name: "single polygon with too few points", value: "0,0;10,10", want: []models.Polygon{}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := parseRegionPolygons(test.value)
if !reflect.DeepEqual(got, test.want) {
t.Fatalf("parseRegionPolygons(%q) = %+v, want %+v", test.value, got, test.want)
}
})
}
}
func TestApplyAgentEnvVarsRegionPolygon(t *testing.T) {
for _, prefix := range []string{"", "GLOBAL_"} {
t.Run("prefix="+prefix, func(t *testing.T) {
t.Setenv(prefix+"AGENT_REGION_POLYGON", "0,0;10,0;10,10;0,10|20,20;30,20;30,30")
configuration := &models.Configuration{}
initConfigPointers(&configuration.Config)
applyAgentEnvVars(configuration, prefix, false)
polygons := configuration.Config.Region.Polygon
if len(polygons) != 2 {
t.Fatalf("got %d polygons, want 2: %+v", len(polygons), polygons)
}
if polygons[0].ID != "0" || polygons[1].ID != "1" {
t.Fatalf("got ids %q, %q, want \"0\", \"1\"", polygons[0].ID, polygons[1].ID)
}
if len(polygons[0].Coordinates) != 4 || len(polygons[1].Coordinates) != 3 {
t.Fatalf("got %d and %d points, want 4 and 3", len(polygons[0].Coordinates), len(polygons[1].Coordinates))
}
})
}
}