mirror of
https://github.com/ep1cman/unifi-protect-backup.git
synced 2025-12-05 23:53:30 +00:00
Simplified websocket reconnection logic
This is now handled automatically by uiprotect internally. We do not need to worry about this, greatly simplifying the logic here to just logging messages
This commit is contained in:
@@ -38,7 +38,6 @@ class EventListener:
|
|||||||
self._unsub_websocketstate = None
|
self._unsub_websocketstate = None
|
||||||
self.detection_types: List[str] = detection_types
|
self.detection_types: List[str] = detection_types
|
||||||
self.ignore_cameras: List[str] = ignore_cameras
|
self.ignore_cameras: List[str] = ignore_cameras
|
||||||
self._websocket_state: WebsocketState = WebsocketState.DISCONNECTED
|
|
||||||
|
|
||||||
async def start(self):
|
async def start(self):
|
||||||
"""Main Loop."""
|
"""Main Loop."""
|
||||||
@@ -46,10 +45,6 @@ class EventListener:
|
|||||||
self._unsub_websocket_state = self._protect.subscribe_websocket_state(self._websocket_state_callback)
|
self._unsub_websocket_state = self._protect.subscribe_websocket_state(self._websocket_state_callback)
|
||||||
self._unsub = self._protect.subscribe_websocket(self._websocket_callback)
|
self._unsub = self._protect.subscribe_websocket(self._websocket_callback)
|
||||||
|
|
||||||
while True:
|
|
||||||
await asyncio.sleep(60)
|
|
||||||
await self._check_websocket_and_reconnect()
|
|
||||||
|
|
||||||
def _websocket_callback(self, msg: WSSubscriptionMessage) -> None:
|
def _websocket_callback(self, msg: WSSubscriptionMessage) -> None:
|
||||||
"""Callback for "EVENT" websocket messages.
|
"""Callback for "EVENT" websocket messages.
|
||||||
|
|
||||||
@@ -67,7 +62,11 @@ class EventListener:
|
|||||||
return
|
return
|
||||||
if "end" not in msg.changed_data:
|
if "end" not in msg.changed_data:
|
||||||
return
|
return
|
||||||
if msg.new_obj.type not in [EventType.MOTION, EventType.SMART_DETECT, EventType.RING]:
|
if msg.new_obj.type not in [
|
||||||
|
EventType.MOTION,
|
||||||
|
EventType.SMART_DETECT,
|
||||||
|
EventType.RING,
|
||||||
|
]:
|
||||||
return
|
return
|
||||||
if msg.new_obj.type is EventType.MOTION and "motion" not in self.detection_types:
|
if msg.new_obj.type is EventType.MOTION and "motion" not in self.detection_types:
|
||||||
logger.extra_debug(f"Skipping unwanted motion detection event: {msg.new_obj.id}") # type: ignore
|
logger.extra_debug(f"Skipping unwanted motion detection event: {msg.new_obj.id}") # type: ignore
|
||||||
@@ -106,43 +105,7 @@ class EventListener:
|
|||||||
Args:
|
Args:
|
||||||
msg (WebsocketState): new state of the websocket
|
msg (WebsocketState): new state of the websocket
|
||||||
"""
|
"""
|
||||||
logger.websocket_data(state) # type: ignore
|
if state == WebsocketState.DISCONNECTED:
|
||||||
|
logger.error("Unifi Protect Websocket lost connection. Reconnecting...")
|
||||||
self._websocket_state = state
|
elif state == WebsocketState.CONNECTED:
|
||||||
|
logger.info("Unifi Protect Websocket connection restored")
|
||||||
logger.debug(f"Change of Websocket State to {state}")
|
|
||||||
|
|
||||||
async def _check_websocket_and_reconnect(self):
|
|
||||||
"""Checks for websocket disconnect and triggers a reconnect."""
|
|
||||||
logger.extra_debug("Checking the status of the websocket...")
|
|
||||||
if self._websocket_state:
|
|
||||||
logger.extra_debug("Websocket is connected.")
|
|
||||||
else:
|
|
||||||
self._protect.connect_event.clear()
|
|
||||||
logger.warning("Lost connection to Unifi Protect.")
|
|
||||||
|
|
||||||
# Unsubscribe, close the session.
|
|
||||||
self._unsub()
|
|
||||||
await self._protect.close_session()
|
|
||||||
|
|
||||||
while True:
|
|
||||||
logger.warning("Attempting reconnect...")
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Start the uiprotect connection by calling `update`
|
|
||||||
await self._protect.close_session()
|
|
||||||
self._protect._bootstrap = None
|
|
||||||
await self._protect.update(force=True)
|
|
||||||
if self._websocket_state:
|
|
||||||
self._unsub = self._protect.subscribe_websocket(self._websocket_callback)
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
logger.error("Unable to establish connection to Unifi Protect")
|
|
||||||
except Exception as e:
|
|
||||||
logger.error("Unexpected exception occurred while trying to reconnect:", exc_info=e)
|
|
||||||
|
|
||||||
# Back off for a little while
|
|
||||||
await asyncio.sleep(10)
|
|
||||||
|
|
||||||
self._protect.connect_event.set()
|
|
||||||
logger.info("Re-established connection to Unifi Protect and to the websocket.")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user