Skip to content

Commit

Permalink
Add transport_timeout_s parameter for setup and adb_connect methods (#…
Browse files Browse the repository at this point in the history
…304)

* Add connect_transport_timeout param for setup and adb_connect

* Black formatting

* Remove max limit for timeout
  • Loading branch information
ollo69 committed Feb 14, 2022
1 parent 8fe056c commit 90245ed
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 20 deletions.
11 changes: 7 additions & 4 deletions androidtv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .androidtv.androidtv_sync import AndroidTVSync
from .basetv.basetv import state_detection_rules_validator
from .basetv.basetv_sync import BaseTVSync
from .constants import DEFAULT_AUTH_TIMEOUT_S
from .constants import DEFAULT_AUTH_TIMEOUT_S, DEFAULT_TRANSPORT_TIMEOUT_S
from .firetv.firetv_sync import FireTVSync


Expand All @@ -23,6 +23,7 @@ def setup(
device_class="auto",
auth_timeout_s=DEFAULT_AUTH_TIMEOUT_S,
signer=None,
transport_timeout_s=DEFAULT_TRANSPORT_TIMEOUT_S,
):
"""Connect to a device and determine whether it's an Android TV or an Amazon Fire TV.
Expand All @@ -46,6 +47,8 @@ def setup(
Authentication timeout (in seconds)
signer : PythonRSASigner, None
The signer for the ADB keys, as loaded by :meth:`androidtv.adb_manager.adb_manager_sync.ADBPythonSync.load_adbkey`
transport_timeout_s : float
Transport timeout (in seconds)
Returns
-------
Expand All @@ -55,14 +58,14 @@ def setup(
"""
if device_class == "androidtv":
atv = AndroidTVSync(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)
atv.adb_connect(auth_timeout_s=auth_timeout_s)
atv.adb_connect(auth_timeout_s=auth_timeout_s, transport_timeout_s=transport_timeout_s)
atv.get_device_properties()
atv.get_installed_apps()
return atv

if device_class == "firetv":
ftv = FireTVSync(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)
ftv.adb_connect(auth_timeout_s=auth_timeout_s)
ftv.adb_connect(auth_timeout_s=auth_timeout_s, transport_timeout_s=transport_timeout_s)
ftv.get_device_properties()
ftv.get_installed_apps()
return ftv
Expand All @@ -73,7 +76,7 @@ def setup(
aftv = BaseTVSync(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)

# establish the ADB connection
aftv.adb_connect(auth_timeout_s=auth_timeout_s)
aftv.adb_connect(auth_timeout_s=auth_timeout_s, transport_timeout_s=transport_timeout_s)

# get device properties
aftv.device_properties = aftv.get_device_properties()
Expand Down
22 changes: 18 additions & 4 deletions androidtv/adb_manager/adb_manager_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
import aiofiles
from ppadb.client import Client

from ..constants import DEFAULT_ADB_TIMEOUT_S, DEFAULT_AUTH_TIMEOUT_S, DEFAULT_LOCK_TIMEOUT_S
from ..constants import (
DEFAULT_ADB_TIMEOUT_S,
DEFAULT_AUTH_TIMEOUT_S,
DEFAULT_LOCK_TIMEOUT_S,
DEFAULT_TRANSPORT_TIMEOUT_S,
)
from ..exceptions import LockNotAcquiredException

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -224,7 +229,12 @@ async def close(self):
"""Close the ADB socket connection."""
await self._adb.close()

async def connect(self, always_log_errors=True, auth_timeout_s=DEFAULT_AUTH_TIMEOUT_S):
async def connect(
self,
always_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
Expand All @@ -233,6 +243,8 @@ async def connect(self, always_log_errors=True, auth_timeout_s=DEFAULT_AUTH_TIME
If True, errors will always be logged; otherwise, errors will only be logged on the first failed reconnect attempt
auth_timeout_s : float
Authentication timeout (in seconds)
transport_timeout_s : float
Transport timeout (in seconds)
Returns
-------
Expand All @@ -250,12 +262,14 @@ async def connect(self, always_log_errors=True, auth_timeout_s=DEFAULT_AUTH_TIME
self._signer = await self.load_adbkey(self.adbkey)

await self._adb.connect(
rsa_keys=[self._signer], transport_timeout_s=1.0, auth_timeout_s=auth_timeout_s
rsa_keys=[self._signer],
transport_timeout_s=transport_timeout_s,
auth_timeout_s=auth_timeout_s,
)

# Connect without authentication
else:
await self._adb.connect(transport_timeout_s=1.0, auth_timeout_s=auth_timeout_s)
await self._adb.connect(transport_timeout_s=transport_timeout_s, auth_timeout_s=auth_timeout_s)

# ADB connection successfully established
_LOGGER.debug("ADB connection to %s:%d successfully established", self.host, self.port)
Expand Down
22 changes: 18 additions & 4 deletions androidtv/adb_manager/adb_manager_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
from adb_shell.auth.sign_pythonrsa import PythonRSASigner
from ppadb.client import Client

from ..constants import DEFAULT_ADB_TIMEOUT_S, DEFAULT_AUTH_TIMEOUT_S, DEFAULT_LOCK_TIMEOUT_S
from ..constants import (
DEFAULT_ADB_TIMEOUT_S,
DEFAULT_AUTH_TIMEOUT_S,
DEFAULT_LOCK_TIMEOUT_S,
DEFAULT_TRANSPORT_TIMEOUT_S,
)
from ..exceptions import LockNotAcquiredException

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -108,7 +113,12 @@ def close(self):
"""Close the ADB socket connection."""
self._adb.close()

def connect(self, always_log_errors=True, auth_timeout_s=DEFAULT_AUTH_TIMEOUT_S):
def connect(
self,
always_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
Expand All @@ -117,6 +127,8 @@ def connect(self, always_log_errors=True, auth_timeout_s=DEFAULT_AUTH_TIMEOUT_S)
If True, errors will always be logged; otherwise, errors will only be logged on the first failed reconnect attempt
auth_timeout_s : float
Authentication timeout (in seconds)
transport_timeout_s : float
Transport timeout (in seconds)
Returns
-------
Expand All @@ -134,12 +146,14 @@ def connect(self, always_log_errors=True, auth_timeout_s=DEFAULT_AUTH_TIMEOUT_S)
self._signer = self.load_adbkey(self.adbkey)

self._adb.connect(
rsa_keys=[self._signer], transport_timeout_s=1.0, auth_timeout_s=auth_timeout_s
rsa_keys=[self._signer],
transport_timeout_s=transport_timeout_s,
auth_timeout_s=auth_timeout_s,
)

# Connect without authentication
else:
self._adb.connect(transport_timeout_s=1.0, auth_timeout_s=auth_timeout_s)
self._adb.connect(transport_timeout_s=transport_timeout_s, auth_timeout_s=auth_timeout_s)

# ADB connection successfully established
_LOGGER.debug("ADB connection to %s:%d successfully established", self.host, self.port)
Expand Down
11 changes: 9 additions & 2 deletions androidtv/basetv/basetv_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,12 @@ async def adb_screencap(self):
"""
return await self._adb.screencap()

async def adb_connect(self, always_log_errors=True, auth_timeout_s=constants.DEFAULT_AUTH_TIMEOUT_S):
async def adb_connect(
self,
always_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
Expand All @@ -166,6 +171,8 @@ async def adb_connect(self, always_log_errors=True, auth_timeout_s=constants.DEF
If True, errors will always be logged; otherwise, errors will only be logged on the first failed reconnect attempt
auth_timeout_s : float
Authentication timeout (in seconds)
transport_timeout_s : float
Transport timeout (in seconds)
Returns
-------
Expand All @@ -174,7 +181,7 @@ async def adb_connect(self, always_log_errors=True, auth_timeout_s=constants.DEF
"""
if isinstance(self._adb, ADBPythonAsync):
return await self._adb.connect(always_log_errors, auth_timeout_s)
return await self._adb.connect(always_log_errors, auth_timeout_s, transport_timeout_s)
return await self._adb.connect(always_log_errors)

async def adb_close(self):
Expand Down
11 changes: 9 additions & 2 deletions androidtv/basetv/basetv_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,12 @@ def adb_screencap(self):
"""
return self._adb.screencap()

def adb_connect(self, always_log_errors=True, auth_timeout_s=constants.DEFAULT_AUTH_TIMEOUT_S):
def adb_connect(
self,
always_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
Expand All @@ -166,6 +171,8 @@ def adb_connect(self, always_log_errors=True, auth_timeout_s=constants.DEFAULT_A
If True, errors will always be logged; otherwise, errors will only be logged on the first failed reconnect attempt
auth_timeout_s : float
Authentication timeout (in seconds)
transport_timeout_s : float
Transport timeout (in seconds)
Returns
-------
Expand All @@ -174,7 +181,7 @@ def adb_connect(self, always_log_errors=True, auth_timeout_s=constants.DEFAULT_A
"""
if isinstance(self._adb, ADBPythonSync):
return self._adb.connect(always_log_errors, auth_timeout_s)
return self._adb.connect(always_log_errors, auth_timeout_s, transport_timeout_s)
return self._adb.connect(always_log_errors)

def adb_close(self):
Expand Down
3 changes: 3 additions & 0 deletions androidtv/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,9 @@ class DeviceEnum(IntEnum):
#: Default authentication timeout (in s) for :meth:`adb_shell.handle.tcp_handle.TcpHandle.connect` and :meth:`adb_shell.handle.tcp_handle_async.TcpHandleAsync.connect`
DEFAULT_AUTH_TIMEOUT_S = 10.0

#: Default transport timeout (in s) for :meth:`adb_shell.handle.tcp_handle.TcpHandle.connect` and :meth:`adb_shell.handle.tcp_handle_async.TcpHandleAsync.connect`
DEFAULT_TRANSPORT_TIMEOUT_S = 1.0

#: Default timeout (in s) for :class:`adb_shell.handle.tcp_handle.TcpHandle` and :class:`adb_shell.handle.tcp_handle_async.TcpHandleAsync`
DEFAULT_ADB_TIMEOUT_S = 9.0

Expand Down
11 changes: 7 additions & 4 deletions androidtv/setup_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from .androidtv.androidtv_async import AndroidTVAsync
from .basetv.basetv_async import BaseTVAsync
from .constants import DEFAULT_AUTH_TIMEOUT_S
from .constants import DEFAULT_AUTH_TIMEOUT_S, DEFAULT_TRANSPORT_TIMEOUT_S
from .firetv.firetv_async import FireTVAsync


Expand All @@ -19,6 +19,7 @@ async def setup(
device_class="auto",
auth_timeout_s=DEFAULT_AUTH_TIMEOUT_S,
signer=None,
transport_timeout_s=DEFAULT_TRANSPORT_TIMEOUT_S,
):
"""Connect to a device and determine whether it's an Android TV or an Amazon Fire TV.
Expand All @@ -42,6 +43,8 @@ async def setup(
Authentication timeout (in seconds)
signer : PythonRSASigner, None
The signer for the ADB keys, as loaded by :meth:`androidtv.adb_manager.adb_manager_async.ADBPythonAsync.load_adbkey`
transport_timeout_s : float
Transport timeout (in seconds)
Returns
-------
Expand All @@ -51,14 +54,14 @@ async def setup(
"""
if device_class == "androidtv":
atv = AndroidTVAsync(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)
await atv.adb_connect(auth_timeout_s=auth_timeout_s)
await atv.adb_connect(auth_timeout_s=auth_timeout_s, transport_timeout_s=transport_timeout_s)
await atv.get_device_properties()
await atv.get_installed_apps()
return atv

if device_class == "firetv":
ftv = FireTVAsync(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)
await ftv.adb_connect(auth_timeout_s=auth_timeout_s)
await ftv.adb_connect(auth_timeout_s=auth_timeout_s, transport_timeout_s=transport_timeout_s)
await ftv.get_device_properties()
await ftv.get_installed_apps()
return ftv
Expand All @@ -69,7 +72,7 @@ async def setup(
aftv = BaseTVAsync(host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)

# establish the ADB connection
await aftv.adb_connect(auth_timeout_s=auth_timeout_s)
await aftv.adb_connect(auth_timeout_s=auth_timeout_s, transport_timeout_s=transport_timeout_s)

# get device properties
await aftv.get_device_properties()
Expand Down

0 comments on commit 90245ed

Please sign in to comment.