You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
2.5 KiB
Python
70 lines
2.5 KiB
Python
"""One common veto for alarm entry, buffered sends, retries and heartbeat.
|
|
Bind only after real producer/model/owner integration. No binding means no ON in
|
|
UNKNOWN mode. Context provider must return None unless the active V2 Session also
|
|
allows alarm; this guard is an additional veto, never a replacement for Session.
|
|
"""
|
|
from copy import deepcopy
|
|
import time
|
|
from . import config
|
|
from .alarm_eligibility import AlarmEligibility, Context
|
|
|
|
_gate = None
|
|
_context_provider = None
|
|
|
|
|
|
def enabled():
|
|
return bool(getattr(config, 'UNKNOWN_INSPECTION_ENABLE', False))
|
|
|
|
|
|
def bind_guard(gate, context_provider):
|
|
global _gate, _context_provider
|
|
if not isinstance(gate, AlarmEligibility) or not callable(context_provider):
|
|
raise ValueError('An explicit guard and active-session context provider are required')
|
|
_gate, _context_provider = gate, context_provider
|
|
|
|
|
|
def capture_contexts(camera_ids):
|
|
if not enabled():
|
|
return None
|
|
result = {}
|
|
for camera in camera_ids:
|
|
try:
|
|
ctx = _context_provider(camera) if _context_provider else None
|
|
if (isinstance(ctx, Context) and ctx.camera == camera
|
|
and _gate and _gate.eligible(ctx, time.monotonic())):
|
|
result[camera] = ctx
|
|
except Exception:
|
|
pass
|
|
return result
|
|
|
|
|
|
def arm_allowed(camera, requested=True, contexts=None):
|
|
if not requested:
|
|
return False
|
|
if not enabled():
|
|
return True
|
|
current = capture_contexts((camera,)).get(camera)
|
|
return current is not None and (contexts is None or contexts.get(camera) == current)
|
|
|
|
|
|
def filter_ws(payload):
|
|
if not enabled():
|
|
return payload
|
|
# Every buffered ON must have an enqueue-time provenance token. Old queued
|
|
# booleans, strings and startup/smoke writes cannot acquire a token later.
|
|
if not isinstance(payload, dict):
|
|
return {'type': 'video_alarm', 'data': False}
|
|
contexts = payload.get('_cc10_contexts')
|
|
allowed = isinstance(contexts, dict) and any(arm_allowed(c, True, contexts) for c in contexts)
|
|
return {'type': payload.get('type', 'video_alarm'), 'data': bool(payload.get('data')) and allowed}
|
|
|
|
|
|
def filter_http(body, contexts, mac_to_camera):
|
|
if not enabled():
|
|
return body
|
|
out = deepcopy(body)
|
|
for camera in out.get('cameras') or []:
|
|
cid = mac_to_camera(camera.get('mac'))
|
|
camera['alarm'] = bool(camera.get('alarm')) and isinstance(contexts, dict) and cid is not None and arm_allowed(cid, True, contexts)
|
|
return out
|