Fix camera onboarding and consolidate device management for v0.1.33

This commit is contained in:
Codex
2026-10-05 18:23:25 +08:00
parent eef46f6ec0
commit 675d31c01f
11 changed files with 136 additions and 14 deletions
+22 -3
View File
@@ -20,6 +20,16 @@ class NoRedirect(urllib.request.HTTPRedirectHandler):
return None
def check_fault(root):
fault = root.find('.//{http://www.w3.org/2003/05/soap-envelope}Fault')
if fault is None:
return
codes = [el.text or '' for el in fault.iter() if el.tag.rsplit('}', 1)[-1] in ('Value', 'faultcode')]
if any(code.rsplit(':', 1)[-1] in ('NotAuthorized', 'FailedAuthentication') for code in codes):
raise PermissionError('ONVIF authentication failed')
raise ValueError('ONVIF returned a fault; check device service and request support')
def request(host, port, endpoint, namespace, operation, content, username, password):
created = dt.datetime.now(dt.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
nonce = secrets.token_bytes(16)
@@ -44,10 +54,19 @@ def request(host, port, endpoint, namespace, operation, content, username, passw
with opener.open(req, timeout=5) as response:
root = ET.fromstring(response.read(1024 * 1024))
except urllib.error.HTTPError as exc:
exc.close()
try:
if exc.code in (401, 403):
raise PermissionError('ONVIF authentication failed') from None
try:
fault = ET.fromstring(exc.read(1024 * 1024))
except ET.ParseError:
fault = None
if fault is not None:
check_fault(fault)
finally:
exc.close()
raise
if root.find('.//{http://www.w3.org/2003/05/soap-envelope}Fault') is not None:
raise ValueError('ONVIF returned a fault; check device credentials and permissions')
check_fault(root)
return root