This commit is contained in:
Josh Hawkins 2025-05-06 10:03:43 -05:00
parent 6b0134aaca
commit 3ddebbfcef

View File

@ -70,33 +70,49 @@ class OnvifController:
logger.error(f"Onvif event loop terminated unexpectedly: {e}") logger.error(f"Onvif event loop terminated unexpectedly: {e}")
async def _init_cameras(self) -> None: async def _init_cameras(self) -> None:
"""Create an ONVIF camera instance and handle failures.""" """Initialize all configured cameras."""
for cam_name, cam in self.camera_configs.items(): for cam_name in self.camera_configs:
try: await self._init_single_camera(cam_name)
self.cams[cam_name] = {
"onvif": ONVIFCamera( async def _init_single_camera(self, cam_name: str) -> bool:
cam.onvif.host, """Initialize a single camera by name.
cam.onvif.port,
cam.onvif.user, Args:
cam.onvif.password, cam_name: The name of the camera to initialize
wsdl_dir=str(Path(find_spec("onvif").origin).parent / "wsdl"),
adjust_time=cam.onvif.ignore_time_mismatch, Returns:
encrypt=not cam.onvif.tls_insecure, bool: True if initialization succeeded, False otherwise
), """
"init": False, if cam_name not in self.camera_configs:
"active": False, logger.error(f"No configuration found for camera {cam_name}")
"features": [], return False
"presets": {},
} cam = self.camera_configs[cam_name]
except (Fault, ONVIFError, Exception, TransportError) as e: try:
logger.error( self.cams[cam_name] = {
f"Failed to create ONVIF camera instance for {cam_name}: {e}" "onvif": ONVIFCamera(
) cam.onvif.host,
self.failed_cams[cam_name] = { cam.onvif.port,
"retry_attempts": 0, cam.onvif.user,
"last_error": str(e), cam.onvif.password,
"last_attempt": time.time(), wsdl_dir=str(Path(find_spec("onvif").origin).parent / "wsdl"),
} adjust_time=cam.onvif.ignore_time_mismatch,
encrypt=not cam.onvif.tls_insecure,
),
"init": False,
"active": False,
"features": [],
"presets": {},
}
return True
except (Fault, ONVIFError, TransportError, Exception) as e:
logger.error(f"Failed to create ONVIF camera instance for {cam_name}: {e}")
self.failed_cams[cam_name] = {
"retry_attempts": 0,
"last_error": str(e),
"last_attempt": time.time(),
}
return False
async def _init_onvif(self, camera_name: str) -> bool: async def _init_onvif(self, camera_name: str) -> bool:
onvif: ONVIFCamera = self.cams[camera_name]["onvif"] onvif: ONVIFCamera = self.cams[camera_name]["onvif"]
@ -654,26 +670,23 @@ class OnvifController:
) )
return {} return {}
if camera_name not in self.cams and ( if camera_name not in self.cams.keys() and (
camera_name not in self.config.cameras camera_name not in self.config.cameras
or not self.config.cameras[camera_name].onvif.host or not self.config.cameras[camera_name].onvif.host
): ):
logger.debug(f"ONVIF is not configured for {camera_name}") logger.debug(f"ONVIF is not configured for {camera_name}")
return {} return {}
if camera_name in self.cams and self.cams[camera_name]["init"]: if camera_name in self.cams.keys() and self.cams[camera_name]["init"]:
return { return {
"name": camera_name, "name": camera_name,
"features": self.cams[camera_name]["features"], "features": self.cams[camera_name]["features"],
"presets": list(self.cams[camera_name]["presets"].keys()), "presets": list(self.cams[camera_name]["presets"].keys()),
} }
if camera_name not in self.cams and camera_name in self.config.cameras: if camera_name not in self.cams.keys() and camera_name in self.config.cameras:
cam = self.config.cameras[camera_name] success = await self._init_single_camera(camera_name)
result = self._create_onvif_camera(camera_name, cam) if not success:
if result:
self.cams[camera_name] = result
else:
return {} return {}
# Reset retry count after timeout # Reset retry count after timeout