Expose ONVIF event preparation through Edge gateway
This commit is contained in:
parent
8d3dcb8ec1
commit
fb70bfcbd1
|
|
@ -403,6 +403,12 @@ def control_onvif_camera(payload):
|
||||||
return request_onvif_client(request, {"ptz_prepare", "ptz"})
|
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():
|
def _auth_now():
|
||||||
return time.time()
|
return time.time()
|
||||||
|
|
||||||
|
|
@ -3359,6 +3365,7 @@ class Handler(BaseHTTPRequestHandler):
|
||||||
or parsed.path in {
|
or parsed.path in {
|
||||||
"/api/onvif/probe",
|
"/api/onvif/probe",
|
||||||
"/api/onvif/ptz",
|
"/api/onvif/ptz",
|
||||||
|
"/api/onvif/events",
|
||||||
"/api/camera-stream/start",
|
"/api/camera-stream/start",
|
||||||
"/api/printer/test",
|
"/api/printer/test",
|
||||||
"/api/printer/print",
|
"/api/printer/print",
|
||||||
|
|
@ -3742,6 +3749,19 @@ class Handler(BaseHTTPRequestHandler):
|
||||||
except Exception:
|
except Exception:
|
||||||
self.send_json({"ok": False, "error": "ONVIF PTZ client is unavailable"}, code=503)
|
self.send_json({"ok": False, "error": "ONVIF PTZ client is unavailable"}, code=503)
|
||||||
return
|
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":
|
if parsed.path == "/api/camera-preview/start":
|
||||||
payload = self.read_json_body()
|
payload = self.read_json_body()
|
||||||
stream_id = str((payload or {}).get("stream_id", ""))
|
stream_id = str((payload or {}).get("stream_id", ""))
|
||||||
|
|
|
||||||
|
|
@ -88,6 +88,22 @@ class OnvifProbeTest(unittest.TestCase):
|
||||||
self.assertTrue(result["ok"])
|
self.assertTrue(result["ok"])
|
||||||
self.assertEqual(json.loads(client.sent)["command"], "home")
|
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):
|
def test_rejects_public_or_named_hosts(self):
|
||||||
invalid_endpoints = (
|
invalid_endpoints = (
|
||||||
"http://example.com/onvif/device_service",
|
"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"))
|
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)
|
route_index = source.index('if parsed.path == "/api/onvif/probe":', auth_index)
|
||||||
ptz_index = source.index('if parsed.path == "/api/onvif/ptz":', route_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(auth_index, route_index)
|
||||||
self.assertLess(route_index, ptz_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/probe",', source)
|
||||||
|
self.assertIn('"/api/onvif/events",', source)
|
||||||
self.assertIn('"ONVIF client is unavailable"', source[route_index:ptz_index])
|
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__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue