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."""
import asyncio
import glob
import logging
import math
@ -110,9 +111,12 @@ def imagestream(
@router.get("/{camera_name}/ptz/info")
async def camera_ptz_info(request: Request, camera_name: str):
if camera_name in request.app.frigate_config.cameras:
return JSONResponse(
content=request.app.onvif.get_camera_info(camera_name),
# Schedule get_camera_info in the OnvifController's event loop
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:
return JSONResponse(
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.
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(
self.handle_command_async(camera_name, command, param), self.loop
)
# logger.debug(f"Scheduled handle_command_async for {camera_name}")
try:
# Wait with a timeout to prevent blocking indefinitely
@ -657,7 +652,7 @@ class OnvifController:
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
but not initialized.
@ -685,21 +680,8 @@ class OnvifController:
}
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
future = asyncio.run_coroutine_threadsafe(
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}"
)
success = await self._init_single_camera(camera_name)
if not success:
return {}
# Reset retry count after timeout
@ -717,12 +699,7 @@ class OnvifController:
f"Attempting ONVIF initialization for {camera_name} (retry {attempts + 1}/{self.max_retries})"
)
try:
# Schedule _init_onvif in the OnvifController's event loop
future = asyncio.run_coroutine_threadsafe(
self._init_onvif(camera_name), self.loop
)
success = future.result(timeout=10)
if success:
if await self._init_onvif(camera_name):
if camera_name in self.failed_cams:
del self.failed_cams[camera_name]
return {
@ -732,8 +709,6 @@ class OnvifController:
}
else:
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:
logger.error(
f"Error during ONVIF initialization for {camera_name}: {e}"