Per-zone declarative safety rules, set in the zone editor and enforced on the edge from the tracking the pipeline already does (no new geometry, no new model): - min_workers (2인 1조): 0 < count < minimum sustained 30s -> lone_worker/HIGH. A solo worker in a buddy-rule zone is a compliance violation and a rescue- delay risk, so it outranks the other two. - max_workers: count > limit sustained 10s -> overcapacity/WARNING. - max_dwell_s: one track's continuous stay past the limit -> overstay/WARNING. A short detection dropout (<=5s grace) does NOT reset the dwell timer - without that, run_once's stale-track filter would quietly neuter every 10-minute rule at real-world detection rates. Edge: detectors/occupancy.py consumes Zone.rules; the engine flips the zone detector's per-track membership into per-zone headcounts (stale tracks already excluded), so counts always match what was actually detected this tick. Fired states clear on condition recovery - re-alerts need a fresh violation, not a cooldown expiry. Malformed rules_json keeps the zone's intrusion detection alive and logs the rules as inactive (never fail dark). Server: move_zones.rules_json (nullable JSON, migration included) validated strictly in ZoneConfigController (bounds, min<=max, unknown keys rejected - a silently dropped rule is one the operator believes is active). Delivered to the edge unchanged via GET /api/config. EventValidator accepts the 3 new types. UI: zone editor gains the three rule fields (dwell entered in minutes, wired in seconds) and shows each zone's rules in its list row; the events console and detail view now render every event type in Korean (wire codes fall back). Verified: pytest 216 pass / PHPUnit 123 pass + real-render harness in Chrome (editor save payload, list labels, console type labels).
85 lines
3.9 KiB
Python
85 lines
3.9 KiB
Python
import logging
|
|
|
|
from edge.zones import zones_from_config, thresholds_from_config
|
|
|
|
# Zones ship NORMALIZED (0..1 of the image); fixtures use that range.
|
|
|
|
def test_parses_zone_rows_json_string_polygon():
|
|
cfg = {"zones": [
|
|
{"zone_id": "z1", "camera_id": "cam-1", "level": "restricted",
|
|
"polygon_json": "[[0,0],[0.9,0],[0.9,0.9]]"},
|
|
], "thresholds": {"fall_prone_s": 3.0}}
|
|
zones = zones_from_config(cfg)
|
|
assert len(zones) == 1
|
|
assert zones[0].zone_id == "z1"
|
|
assert zones[0].level == "restricted"
|
|
assert zones[0].polygon == [(0.0, 0.0), (0.9, 0.0), (0.9, 0.9)]
|
|
assert thresholds_from_config(cfg)["fall_prone_s"] == 3.0
|
|
|
|
def test_skips_malformed_row_and_warns(caplog):
|
|
# A malformed row (missing polygon_json/level here) must be DROPPED and LOGGED — never silently
|
|
# swallowed, matching the pixel-space branch's "don't fail dark" principle: a dead zone with no
|
|
# signal is only discovered after a worker enters it.
|
|
cfg = {"zones": [{"zone_id": "bad"}], "thresholds": {}}
|
|
with caplog.at_level(logging.WARNING):
|
|
assert zones_from_config(cfg) == []
|
|
assert any("bad" in r.message and "malformed" in r.message for r in caplog.records)
|
|
|
|
def test_empty_config():
|
|
assert zones_from_config({}) == []
|
|
assert thresholds_from_config({}) == {}
|
|
|
|
def test_skips_short_point_without_crashing():
|
|
cfg = {"zones": [
|
|
{"zone_id": "bad", "camera_id": "c", "level": "restricted", "polygon_json": "[[0,0],[9],[0.9,0.9]]"},
|
|
{"zone_id": "ok", "camera_id": "c", "level": "restricted", "polygon_json": "[[0,0],[0.9,0],[0.9,0.9]]"},
|
|
]}
|
|
zones = zones_from_config(cfg)
|
|
# bad row skipped, good row survives -- function did not crash
|
|
assert [z.zone_id for z in zones] == ["ok"]
|
|
|
|
def test_non_iterable_zones_returns_empty():
|
|
assert zones_from_config({"zones": 5}) == []
|
|
|
|
def test_rejects_legacy_pixel_space_polygon_and_warns(caplog):
|
|
# A polygon saved by the pre-normalization dashboard is in canvas PIXELS (values in the
|
|
# hundreds). After the edge normalizes the foot to 0..1 it can never match, so such a zone
|
|
# would silently detect nothing. zones_from_config must DROP it and warn loudly (so the
|
|
# operator redraws it) rather than fail dark.
|
|
cfg = {"zones": [
|
|
{"zone_id": "legacy", "camera_id": "cam-1", "level": "restricted",
|
|
"polygon_json": "[[320,180],[600,180],[600,400]]"},
|
|
{"zone_id": "modern", "camera_id": "cam-1", "level": "restricted",
|
|
"polygon_json": "[[0.25,0.25],[0.75,0.25],[0.75,0.75]]"},
|
|
]}
|
|
with caplog.at_level(logging.WARNING):
|
|
zones = zones_from_config(cfg)
|
|
assert [z.zone_id for z in zones] == ["modern"] # legacy dropped, modern kept
|
|
assert any("legacy" in r.message and "pixel" in r.message for r in caplog.records)
|
|
|
|
|
|
def test_parses_occupancy_rules():
|
|
cfg = {"zones": [
|
|
{"zone_id": "z1", "camera_id": "c", "level": "restricted",
|
|
"polygon_json": "[[0,0],[0.9,0],[0.9,0.9]]",
|
|
"rules_json": '{"min_workers": 2, "max_workers": 4, "max_dwell_s": 600}'},
|
|
{"zone_id": "z2", "camera_id": "c", "level": "warning",
|
|
"polygon_json": "[[0,0],[0.9,0],[0.9,0.9]]"},
|
|
]}
|
|
zones = zones_from_config(cfg)
|
|
assert zones[0].rules == {"min_workers": 2, "max_workers": 4, "max_dwell_s": 600}
|
|
assert zones[1].rules is None
|
|
|
|
|
|
def test_malformed_rules_keep_zone_but_warn(caplog):
|
|
# A broken rules blob must not kill the zone's INTRUSION detection (the polygon is
|
|
# valid) -- keep the zone, drop the rules, and say so loudly.
|
|
cfg = {"zones": [
|
|
{"zone_id": "z1", "camera_id": "c", "level": "restricted",
|
|
"polygon_json": "[[0,0],[0.9,0],[0.9,0.9]]",
|
|
"rules_json": '{"min_workers": -1}'},
|
|
]}
|
|
with caplog.at_level(logging.WARNING):
|
|
zones = zones_from_config(cfg)
|
|
assert len(zones) == 1 and zones[0].rules is None
|
|
assert any("rules_json" in r.message for r in caplog.records)
|