Block all calls to protect client when the connection is

dropped and we are awaiting a reconnect
This commit is contained in:
Sebastian Goscik
2023-07-08 16:30:09 +01:00
parent 6c719c0162
commit c4e9a42c1a
5 changed files with 16 additions and 0 deletions

View File

@@ -82,6 +82,9 @@ class VideoDownloader:
self.logger.info("Starting Downloader") self.logger.info("Starting Downloader")
while True: while True:
try: try:
# Wait for unifi protect to be connected
await self._protect.connect_event.wait()
event = await self.download_queue.get() event = await self.download_queue.get()
self.current_event = event self.current_event = event
self.logger = logging.LoggerAdapter(self.base_logger, {'event': f' [{event.id}]'}) self.logger = logging.LoggerAdapter(self.base_logger, {'event': f' [{event.id}]'})

View File

@@ -100,6 +100,7 @@ class EventListener:
if self._protect.check_ws(): if self._protect.check_ws():
logger.extra_debug("Websocket is connected.") logger.extra_debug("Websocket is connected.")
else: else:
self._protect.connect_event.clear()
logger.warning("Lost connection to Unifi Protect.") logger.warning("Lost connection to Unifi Protect.")
# Unsubscribe, close the session. # Unsubscribe, close the session.
@@ -125,4 +126,5 @@ class EventListener:
# Back off for a little while # Back off for a little while
await asyncio.sleep(10) await asyncio.sleep(10)
self._protect.connect_event.set()
logger.info("Re-established connection to Unifi Protect and to the websocket.") logger.info("Re-established connection to Unifi Protect and to the websocket.")

View File

@@ -123,6 +123,9 @@ class MissingEventChecker:
logger.info("Starting Missing Event Checker") logger.info("Starting Missing Event Checker")
while True: while True:
try: try:
# Wait for unifi protect to be connected
await self._protect.connect_event.wait()
logger.extra_debug("Running check for missing events...") logger.extra_debug("Running check for missing events...")
wanted_events = await self._get_missing_events() wanted_events = await self._get_missing_events()

View File

@@ -193,6 +193,11 @@ class UnifiProtectBackup:
else: else:
raise ConnectionError("Failed to connect to UniFi Protect after 10 attempts") raise ConnectionError("Failed to connect to UniFi Protect after 10 attempts")
# Add a lock to the protect client that can be used to prevent code accessing the client when it has
# lost connection
self._protect.connect_event = asyncio.Event()
self._protect.connect_event.set()
# Get a mapping of camera ids -> names # Get a mapping of camera ids -> names
logger.info("Found cameras:") logger.info("Found cameras:")
for camera in self._protect.bootstrap.cameras.values(): for camera in self._protect.bootstrap.cameras.values():

View File

@@ -286,6 +286,9 @@ async def get_camera_name(protect: ProtectApiClient, id: str):
If the camera ID is not know, it tries refreshing the cached data If the camera ID is not know, it tries refreshing the cached data
""" """
# Wait for unifi protect to be connected
await protect.connect_event.wait()
try: try:
return protect.bootstrap.cameras[id].name return protect.bootstrap.cameras[id].name
except KeyError: except KeyError: