mirror of
https://github.com/kerberos-io/agent.git
synced 2026-11-27 23:50:54 +00:00
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>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
5b134731fa
commit
59f43d2877
@@ -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). | "" |
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user