Skip to content

Commit

Permalink
Simplify logging of connection attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffLIrion committed Apr 9, 2022
1 parent bcc83db commit 6146014
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 78 deletions.
34 changes: 11 additions & 23 deletions androidtv/adb_manager/adb_manager_async.py
Expand Up @@ -207,9 +207,6 @@ def __init__(self, host, port, adbkey="", signer=None):

self._signer = signer

# keep track of whether the ADB connection is intact
self._available = False

# use a lock to make sure that ADB commands don't overlap
self._adb_lock = asyncio.Lock()

Expand All @@ -231,16 +228,16 @@ async def close(self):

async def connect(
self,
always_log_errors=True,
log_errors=True,
auth_timeout_s=DEFAULT_AUTH_TIMEOUT_S,
transport_timeout_s=DEFAULT_TRANSPORT_TIMEOUT_S,
):
"""Connect to an Android TV / Fire TV device.
Parameters
----------
always_log_errors : bool
If True, errors will always be logged; otherwise, errors will only be logged on the first failed reconnect attempt
log_errors : bool
Whether errors should be logged
auth_timeout_s : float
Authentication timeout (in seconds)
transport_timeout_s : float
Expand Down Expand Up @@ -273,11 +270,10 @@ async def connect(

# ADB connection successfully established
_LOGGER.debug("ADB connection to %s:%d successfully established", self.host, self.port)
self._available = True
return True

except OSError as exc:
if self._available or always_log_errors:
if log_errors:
if exc.strerror is None:
exc.strerror = "Timed out trying to connect to ADB device."
_LOGGER.warning(
Expand All @@ -290,24 +286,21 @@ async def connect(

# ADB connection attempt failed
await self.close()
self._available = False
return False

except Exception as exc: # pylint: disable=broad-except
if self._available or always_log_errors:
if log_errors:
_LOGGER.warning(
"Couldn't connect to %s:%d. %s: %s", self.host, self.port, exc.__class__.__name__, exc
)

# ADB connection attempt failed
await self.close()
self._available = False
return False

except LockNotAcquiredException:
_LOGGER.warning("Couldn't connect to %s:%d because adb-shell lock not acquired.", self.host, self.port)
await self.close()
self._available = False
return False

@staticmethod
Expand Down Expand Up @@ -470,9 +463,8 @@ def __init__(self, host, port=5555, adb_server_ip="", adb_server_port=5037):
self._adb_client = None
self._adb_device = None

# keep track of whether the ADB connection is/was intact
# keep track of whether the ADB connection is intact
self._available = False
self._was_available = False

# use a lock to make sure that ADB commands don't overlap
self._adb_lock = asyncio.Lock()
Expand Down Expand Up @@ -500,13 +492,13 @@ async def close(self):
"""
self._available = False

async def connect(self, always_log_errors=True):
async def connect(self, log_errors=True):
"""Connect to an Android TV / Fire TV device.
Parameters
----------
always_log_errors : bool
If True, errors will always be logged; otherwise, errors will only be logged on the first failed reconnect attempt
log_errors : bool
Whether errors should be logged
Returns
-------
Expand All @@ -531,11 +523,10 @@ async def connect(self, always_log_errors=True):
self.adb_server_port,
)
self._available = True
self._was_available = True
return True

# ADB connection attempt failed (without an exception)
if self._was_available or always_log_errors:
if log_errors:
_LOGGER.warning(
"Couldn't connect to %s:%d via ADB server %s:%d because the server is not connected to the device",
self.host,
Expand All @@ -546,12 +537,11 @@ async def connect(self, always_log_errors=True):

await self.close()
self._available = False
self._was_available = False
return False

# ADB connection attempt failed
except Exception as exc: # noqa pylint: disable=broad-except
if self._was_available or always_log_errors:
if log_errors:
_LOGGER.warning(
"Couldn't connect to %s:%d via ADB server %s:%d, error: %s",
self.host,
Expand All @@ -563,7 +553,6 @@ async def connect(self, always_log_errors=True):

await self.close()
self._available = False
self._was_available = False
return False

except LockNotAcquiredException:
Expand All @@ -576,7 +565,6 @@ async def connect(self, always_log_errors=True):
)
await self.close()
self._available = False
self._was_available = False
return False

async def pull(self, local_path, device_path):
Expand Down
34 changes: 11 additions & 23 deletions androidtv/adb_manager/adb_manager_sync.py
Expand Up @@ -91,9 +91,6 @@ def __init__(self, host, port, adbkey="", signer=None):

self._signer = signer

# keep track of whether the ADB connection is intact
self._available = False

# use a lock to make sure that ADB commands don't overlap
self._adb_lock = threading.Lock()

Expand All @@ -115,16 +112,16 @@ def close(self):

def connect(
self,
always_log_errors=True,
log_errors=True,
auth_timeout_s=DEFAULT_AUTH_TIMEOUT_S,
transport_timeout_s=DEFAULT_TRANSPORT_TIMEOUT_S,
):
"""Connect to an Android TV / Fire TV device.
Parameters
----------
always_log_errors : bool
If True, errors will always be logged; otherwise, errors will only be logged on the first failed reconnect attempt
log_errors : bool
Whether errors should be logged
auth_timeout_s : float
Authentication timeout (in seconds)
transport_timeout_s : float
Expand Down Expand Up @@ -157,11 +154,10 @@ def connect(

# ADB connection successfully established
_LOGGER.debug("ADB connection to %s:%d successfully established", self.host, self.port)
self._available = True
return True

except OSError as exc:
if self._available or always_log_errors:
if log_errors:
if exc.strerror is None:
exc.strerror = "Timed out trying to connect to ADB device."
_LOGGER.warning(
Expand All @@ -174,24 +170,21 @@ def connect(

# ADB connection attempt failed
self.close()
self._available = False
return False

except Exception as exc: # pylint: disable=broad-except
if self._available or always_log_errors:
if log_errors:
_LOGGER.warning(
"Couldn't connect to %s:%d. %s: %s", self.host, self.port, exc.__class__.__name__, exc
)

# ADB connection attempt failed
self.close()
self._available = False
return False

except LockNotAcquiredException:
_LOGGER.warning("Couldn't connect to %s:%d because adb-shell lock not acquired.", self.host, self.port)
self.close()
self._available = False
return False

@staticmethod
Expand Down Expand Up @@ -354,9 +347,8 @@ def __init__(self, host, port=5555, adb_server_ip="", adb_server_port=5037):
self._adb_client = None
self._adb_device = None

# keep track of whether the ADB connection is/was intact
# keep track of whether the ADB connection is intact
self._available = False
self._was_available = False

# use a lock to make sure that ADB commands don't overlap
self._adb_lock = threading.Lock()
Expand Down Expand Up @@ -384,13 +376,13 @@ def close(self):
"""
self._available = False

def connect(self, always_log_errors=True):
def connect(self, log_errors=True):
"""Connect to an Android TV / Fire TV device.
Parameters
----------
always_log_errors : bool
If True, errors will always be logged; otherwise, errors will only be logged on the first failed reconnect attempt
log_errors : bool
Whether errors should be logged
Returns
-------
Expand All @@ -415,11 +407,10 @@ def connect(self, always_log_errors=True):
self.adb_server_port,
)
self._available = True
self._was_available = True
return True

# ADB connection attempt failed (without an exception)
if self._was_available or always_log_errors:
if log_errors:
_LOGGER.warning(
"Couldn't connect to %s:%d via ADB server %s:%d because the server is not connected to the device",
self.host,
Expand All @@ -430,12 +421,11 @@ def connect(self, always_log_errors=True):

self.close()
self._available = False
self._was_available = False
return False

# ADB connection attempt failed
except Exception as exc: # noqa pylint: disable=broad-except
if self._was_available or always_log_errors:
if log_errors:
_LOGGER.warning(
"Couldn't connect to %s:%d via ADB server %s:%d, error: %s",
self.host,
Expand All @@ -447,7 +437,6 @@ def connect(self, always_log_errors=True):

self.close()
self._available = False
self._was_available = False
return False

except LockNotAcquiredException:
Expand All @@ -460,7 +449,6 @@ def connect(self, always_log_errors=True):
)
self.close()
self._available = False
self._was_available = False
return False

def pull(self, local_path, device_path):
Expand Down
10 changes: 5 additions & 5 deletions androidtv/basetv/basetv_async.py
Expand Up @@ -159,16 +159,16 @@ async def adb_screencap(self):

async def adb_connect(
self,
always_log_errors=True,
log_errors=True,
auth_timeout_s=constants.DEFAULT_AUTH_TIMEOUT_S,
transport_timeout_s=constants.DEFAULT_TRANSPORT_TIMEOUT_S,
):
"""Connect to an Android TV / Fire TV device.
Parameters
----------
always_log_errors : bool
If True, errors will always be logged; otherwise, errors will only be logged on the first failed reconnect attempt
log_errors : bool
Whether errors should be logged
auth_timeout_s : float
Authentication timeout (in seconds)
transport_timeout_s : float
Expand All @@ -181,8 +181,8 @@ async def adb_connect(
"""
if isinstance(self._adb, ADBPythonAsync):
return await self._adb.connect(always_log_errors, auth_timeout_s, transport_timeout_s)
return await self._adb.connect(always_log_errors)
return await self._adb.connect(log_errors, auth_timeout_s, transport_timeout_s)
return await self._adb.connect(log_errors)

async def adb_close(self):
"""Close the ADB connection.
Expand Down
10 changes: 5 additions & 5 deletions androidtv/basetv/basetv_sync.py
Expand Up @@ -159,16 +159,16 @@ def adb_screencap(self):

def adb_connect(
self,
always_log_errors=True,
log_errors=True,
auth_timeout_s=constants.DEFAULT_AUTH_TIMEOUT_S,
transport_timeout_s=constants.DEFAULT_TRANSPORT_TIMEOUT_S,
):
"""Connect to an Android TV / Fire TV device.
Parameters
----------
always_log_errors : bool
If True, errors will always be logged; otherwise, errors will only be logged on the first failed reconnect attempt
log_errors : bool
Whether errors should be logged
auth_timeout_s : float
Authentication timeout (in seconds)
transport_timeout_s : float
Expand All @@ -181,8 +181,8 @@ def adb_connect(
"""
if isinstance(self._adb, ADBPythonSync):
return self._adb.connect(always_log_errors, auth_timeout_s, transport_timeout_s)
return self._adb.connect(always_log_errors)
return self._adb.connect(log_errors, auth_timeout_s, transport_timeout_s)
return self._adb.connect(log_errors)

def adb_close(self):
"""Close the ADB connection.
Expand Down

0 comments on commit 6146014

Please sign in to comment.