Enhance time synchronization checks, update storage paths, and improve camera recording management

This commit is contained in:
Alireza Vaezi
2025-07-25 22:38:33 -04:00
parent 69966519b0
commit 731d8cd9ff
13 changed files with 283 additions and 60 deletions

View File

@@ -90,6 +90,7 @@ class APIServer:
self.server_start_time = datetime.now()
self.running = False
self._server_thread: Optional[threading.Thread] = None
self._event_loop: Optional[asyncio.AbstractEventLoop] = None
# Setup CORS
self.app.add_middleware(
@@ -349,8 +350,15 @@ class APIServer:
"timestamp": event.timestamp.isoformat()
}
# Use asyncio to broadcast (need to handle thread safety)
asyncio.create_task(self.websocket_manager.broadcast(message))
# Schedule the broadcast in the event loop thread-safely
if self._event_loop and not self._event_loop.is_closed():
# Use call_soon_threadsafe to schedule the coroutine from another thread
asyncio.run_coroutine_threadsafe(
self.websocket_manager.broadcast(message),
self._event_loop
)
else:
self.logger.debug("Event loop not available for broadcasting")
except Exception as e:
self.logger.error(f"Error broadcasting event: {e}")
@@ -399,6 +407,10 @@ class APIServer:
def _run_server(self) -> None:
"""Run the uvicorn server"""
try:
# Capture the event loop for thread-safe event broadcasting
self._event_loop = asyncio.new_event_loop()
asyncio.set_event_loop(self._event_loop)
uvicorn.run(
self.app,
host=self.config.system.api_host,
@@ -409,6 +421,7 @@ class APIServer:
self.logger.error(f"Error running API server: {e}")
finally:
self.running = False
self._event_loop = None
def is_running(self) -> bool:
"""Check if API server is running"""