diff --git a/xair_api/util.py b/xair_api/util.py index 9a4c1b0..a26fb6d 100644 --- a/xair_api/util.py +++ b/xair_api/util.py @@ -1,6 +1,35 @@ import functools +import time from math import exp, log +from .errors import XAirRemoteConnectionTimeoutError + + +def timeout(func): + """ + Times out the login function once time elapsed exceeds remote.connect_timeout. + """ + + @functools.wraps(func) + def wrapper(*args, **kwargs): + remote, *_ = args + + err = None + start = time.time() + while time.time() < start + remote.connect_timeout: + try: + func(*args, **kwargs) + remote.logger.debug(f"login time: {round(time.time() - start, 2)}") + err = None + break + except XAirRemoteConnectionTimeoutError as e: + err = e + continue + if err: + raise err + + return wrapper + def lin_get(min, max, val): return min + (max - min) * val