move coroutine call with loop to api

This commit is contained in:
Josh Hawkins 2025-05-06 16:33:04 -05:00
parent 1b02887b45
commit 20647ce891
2 changed files with 10 additions and 31 deletions

View File

@ -1,5 +1,6 @@
"""Image and video apis.""" """Image and video apis."""
import asyncio
import glob import glob
import logging import logging
import math import math
@ -110,9 +111,12 @@ def imagestream(
@router.get("/{camera_name}/ptz/info") @router.get("/{camera_name}/ptz/info")
async def camera_ptz_info(request: Request, camera_name: str): async def camera_ptz_info(request: Request, camera_name: str):
if camera_name in request.app.frigate_config.cameras: if camera_name in request.app.frigate_config.cameras:
return JSONResponse( # Schedule get_camera_info in the OnvifController's event loop
content=request.app.onvif.get_camera_info(camera_name), future = asyncio.run_coroutine_threadsafe(
request.app.onvif.get_camera_info(camera_name), request.app.onvif.loop
) )
result = future.result()
return JSONResponse(content=result)
else: else:
return JSONResponse( return JSONResponse(
content={"success": False, "message": "Camera not found"}, content={"success": False, "message": "Camera not found"},

View File

@ -638,14 +638,9 @@ class OnvifController:
Handle ONVIF commands by scheduling them in the event loop. Handle ONVIF commands by scheduling them in the event loop.
This is the synchronous interface that schedules async work. This is the synchronous interface that schedules async work.
""" """
# Run the async command in the event loop
# logger.debug(
# f"Scheduling handle_command_async for {camera_name} with command {command}. {self.loop}"
# )
future = asyncio.run_coroutine_threadsafe( future = asyncio.run_coroutine_threadsafe(
self.handle_command_async(camera_name, command, param), self.loop self.handle_command_async(camera_name, command, param), self.loop
) )
# logger.debug(f"Scheduled handle_command_async for {camera_name}")
try: try:
# Wait with a timeout to prevent blocking indefinitely # Wait with a timeout to prevent blocking indefinitely
@ -657,7 +652,7 @@ class OnvifController:
f"Error executing command {command} for camera {camera_name}: {e}" f"Error executing command {command} for camera {camera_name}: {e}"
) )
def get_camera_info(self, camera_name: str) -> dict[str, any]: async def get_camera_info(self, camera_name: str) -> dict[str, any]:
""" """
Get ptz capabilities and presets, attempting to reconnect if ONVIF is configured Get ptz capabilities and presets, attempting to reconnect if ONVIF is configured
but not initialized. but not initialized.
@ -685,21 +680,8 @@ class OnvifController:
} }
if camera_name not in self.cams.keys() and camera_name in self.config.cameras: if camera_name not in self.cams.keys() and camera_name in self.config.cameras:
# Schedule _init_single_camera in the OnvifController's event loop success = await self._init_single_camera(camera_name)
future = asyncio.run_coroutine_threadsafe( if not success:
self._init_single_camera(camera_name), self.loop
)
try:
success = future.result(timeout=10)
if not success:
return {}
except asyncio.TimeoutError:
logger.error(f"_init_single_camera timed out for camera {camera_name}")
return {}
except Exception as e:
logger.error(
f"Error in _init_single_camera for camera {camera_name}: {e}"
)
return {} return {}
# Reset retry count after timeout # Reset retry count after timeout
@ -717,12 +699,7 @@ class OnvifController:
f"Attempting ONVIF initialization for {camera_name} (retry {attempts + 1}/{self.max_retries})" f"Attempting ONVIF initialization for {camera_name} (retry {attempts + 1}/{self.max_retries})"
) )
try: try:
# Schedule _init_onvif in the OnvifController's event loop if await self._init_onvif(camera_name):
future = asyncio.run_coroutine_threadsafe(
self._init_onvif(camera_name), self.loop
)
success = future.result(timeout=10)
if success:
if camera_name in self.failed_cams: if camera_name in self.failed_cams:
del self.failed_cams[camera_name] del self.failed_cams[camera_name]
return { return {
@ -732,8 +709,6 @@ class OnvifController:
} }
else: else:
logger.warning(f"ONVIF initialization failed for {camera_name}") logger.warning(f"ONVIF initialization failed for {camera_name}")
except asyncio.TimeoutError:
logger.error(f"_init_onvif timed out for camera {camera_name}")
except Exception as e: except Exception as e:
logger.error( logger.error(
f"Error during ONVIF initialization for {camera_name}: {e}" f"Error during ONVIF initialization for {camera_name}: {e}"