From fb70bfcbd1ad3564acf16b62be3b3e0bd96464c9 Mon Sep 17 00:00:00 2001 From: Andrei Smaliak Date: Thu, 8 Oct 2026 13:23:17 +0200 Subject: [PATCH] Expose ONVIF event preparation through Edge gateway --- stage_ui/console_core/backend/app.py | 20 ++++++++++++++ .../console_core/backend/test_onvif_probe.py | 26 ++++++++++++++++--- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/stage_ui/console_core/backend/app.py b/stage_ui/console_core/backend/app.py index 70b49ee..8d2331e 100644 --- a/stage_ui/console_core/backend/app.py +++ b/stage_ui/console_core/backend/app.py @@ -403,6 +403,12 @@ def control_onvif_camera(payload): return request_onvif_client(request, {"ptz_prepare", "ptz"}) +def prepare_onvif_events(payload): + request = dict(payload) if isinstance(payload, dict) else {} + request["action"] = "events_prepare" + return request_onvif_client(request, {"events_prepare"}) + + def _auth_now(): return time.time() @@ -3359,6 +3365,7 @@ class Handler(BaseHTTPRequestHandler): or parsed.path in { "/api/onvif/probe", "/api/onvif/ptz", + "/api/onvif/events", "/api/camera-stream/start", "/api/printer/test", "/api/printer/print", @@ -3742,6 +3749,19 @@ class Handler(BaseHTTPRequestHandler): except Exception: self.send_json({"ok": False, "error": "ONVIF PTZ client is unavailable"}, code=503) return + if parsed.path == "/api/onvif/events": + payload = self.read_json_body() + if payload is None: + self.send_json({"ok": False, "error": "Invalid JSON body"}, code=400) + return + try: + result = prepare_onvif_events(payload) + self.send_json(result, code=200 if result.get("ok") is True else 502) + except ValueError as exc: + self.send_json({"ok": False, "error": str(exc)}, code=400) + except Exception: + self.send_json({"ok": False, "error": "ONVIF event client is unavailable"}, code=503) + return if parsed.path == "/api/camera-preview/start": payload = self.read_json_body() stream_id = str((payload or {}).get("stream_id", "")) diff --git a/stage_ui/console_core/backend/test_onvif_probe.py b/stage_ui/console_core/backend/test_onvif_probe.py index 1deee6d..853eb3a 100644 --- a/stage_ui/console_core/backend/test_onvif_probe.py +++ b/stage_ui/console_core/backend/test_onvif_probe.py @@ -88,6 +88,22 @@ class OnvifProbeTest(unittest.TestCase): self.assertTrue(result["ok"]) self.assertEqual(json.loads(client.sent)["command"], "home") + def test_prepares_event_subscription_only_over_local_socket(self): + client = FakeSocket(json.dumps({"ok": True, "events": ["motion"]}).encode()) + with mock.patch.object(APP.socket, "socket", return_value=client): + result = APP.prepare_onvif_events({ + "module_id": 42, + "xaddr": "http://192.168.70.102:2020/onvif/device_service", + "username": "camera-user", + "password": "camera-password", + "registers": [{"id": 7, "event": "motion"}], + }) + + self.assertTrue(result["ok"]) + request = json.loads(client.sent) + self.assertEqual(request["action"], "events_prepare") + self.assertEqual(request["registers"], [{"id": 7, "event": "motion"}]) + def test_rejects_public_or_named_hosts(self): invalid_endpoints = ( "http://example.com/onvif/device_service", @@ -108,14 +124,18 @@ class OnvifProbeTest(unittest.TestCase): auth_index = source.index("if not self.require_auth(parsed):", source.index("def do_POST")) route_index = source.index('if parsed.path == "/api/onvif/probe":', auth_index) ptz_index = source.index('if parsed.path == "/api/onvif/ptz":', route_index) - preview_index = source.index('if parsed.path == "/api/camera-preview/start":', ptz_index) + events_index = source.index('if parsed.path == "/api/onvif/events":', ptz_index) + preview_index = source.index('if parsed.path == "/api/camera-preview/start":', events_index) self.assertLess(auth_index, route_index) self.assertLess(route_index, ptz_index) - self.assertLess(ptz_index, preview_index) + self.assertLess(ptz_index, events_index) + self.assertLess(events_index, preview_index) self.assertIn('"/api/onvif/probe",', source) + self.assertIn('"/api/onvif/events",', source) self.assertIn('"ONVIF client is unavailable"', source[route_index:ptz_index]) - self.assertIn('"ONVIF PTZ client is unavailable"', source[ptz_index:preview_index]) + self.assertIn('"ONVIF PTZ client is unavailable"', source[ptz_index:events_index]) + self.assertIn('"ONVIF event client is unavailable"', source[events_index:preview_index]) if __name__ == "__main__":