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).
77 lines
2.6 KiB
Python
77 lines
2.6 KiB
Python
import uuid as _uuid
|
|
from dataclasses import dataclass, field
|
|
from enum import Enum
|
|
|
|
|
|
class EventType(str, Enum):
|
|
FALL = "fall"
|
|
NO_MOTION = "no_motion"
|
|
IMMOBILITY = "immobility"
|
|
GHOST = "ghost"
|
|
ZONE_WARNING = "zone_warning"
|
|
ZONE_RESTRICTED = "zone_restricted"
|
|
PINCH = "pinch"
|
|
MONITORING_DEGRADED = "monitoring_degraded"
|
|
DEVICE_FAULT = "device_fault"
|
|
LONE_WORKER = "lone_worker" # 2인 1조 존에 단독 작업 지속
|
|
OVERCAPACITY = "overcapacity" # 존 정원 초과 지속
|
|
OVERSTAY = "overstay" # 존 체류 상한 초과 (트랙별)
|
|
|
|
|
|
class Severity(str, Enum):
|
|
CRITICAL = "critical"
|
|
HIGH = "high"
|
|
WARNING = "warning"
|
|
INFO = "info"
|
|
|
|
|
|
_SEVERITY = {
|
|
EventType.NO_MOTION: Severity.CRITICAL,
|
|
EventType.GHOST: Severity.CRITICAL,
|
|
EventType.FALL: Severity.HIGH,
|
|
EventType.ZONE_RESTRICTED: Severity.HIGH,
|
|
EventType.PINCH: Severity.HIGH,
|
|
EventType.IMMOBILITY: Severity.WARNING,
|
|
EventType.ZONE_WARNING: Severity.WARNING,
|
|
EventType.MONITORING_DEGRADED: Severity.WARNING,
|
|
EventType.DEVICE_FAULT: Severity.WARNING,
|
|
# 단독 작업은 규정 위반이자 사고 시 구조 지연 요인 — 정원/체류보다 한 단계 높게.
|
|
EventType.LONE_WORKER: Severity.HIGH,
|
|
EventType.OVERCAPACITY: Severity.WARNING,
|
|
EventType.OVERSTAY: Severity.WARNING,
|
|
}
|
|
|
|
|
|
def severity_for(event_type: EventType) -> Severity:
|
|
return _SEVERITY.get(event_type, Severity.INFO)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Event:
|
|
event_type: EventType
|
|
severity: Severity
|
|
camera_id: str
|
|
track_id: int | None
|
|
zone_id: str | None
|
|
occurred_at: float
|
|
duration_s: float
|
|
metrics: dict
|
|
spatial: tuple | None # (x, y) 유령사고 등 공간 기준
|
|
# Stable idempotency key: generated ONCE at event creation and carried unchanged through the
|
|
# offline queue and every retry, so a POST that succeeded server-side but timed out
|
|
# client-side is deduplicated by the server (see server IngestController) instead of stored twice.
|
|
event_uuid: str = field(default_factory=lambda: _uuid.uuid4().hex)
|
|
|
|
def to_payload(self, device_id: str) -> dict:
|
|
return {
|
|
"device_id": device_id,
|
|
"event_uuid": self.event_uuid,
|
|
"camera_id": self.camera_id,
|
|
"event_type": self.event_type.value,
|
|
"severity": self.severity.value,
|
|
"track_id": self.track_id,
|
|
"zone_id": self.zone_id,
|
|
"occurred_at": self.occurred_at,
|
|
"duration_s": round(self.duration_s, 2),
|
|
"metrics": self.metrics,
|
|
}
|