diff options
-rw-r--r-- | src/polaris/kettle.py | 9 | ||||
-rw-r--r-- | src/polaris/protocol.py | 8 | ||||
-rwxr-xr-x | src/polaris_kettle_bot.py | 3 |
3 files changed, 14 insertions, 6 deletions
diff --git a/src/polaris/kettle.py b/src/polaris/kettle.py index 6f76279..d6e0dd6 100644 --- a/src/polaris/kettle.py +++ b/src/polaris/kettle.py @@ -149,16 +149,18 @@ class Kettle(DeviceListener, ConnectionStatusListener): device_token: str conn: Optional[UDPConnection] conn_status: Optional[ConnectionStatus] + _read_timeout: Optional[int] _logger: logging.Logger _find_evt: threading.Event - def __init__(self, mac: str, device_token: str): + def __init__(self, mac: str, device_token: str, read_timeout: Optional[int] = None): super().__init__() self.mac = mac self.device = None self.device_token = device_token self.conn = None self.conn_status = None + self._read_timeout = read_timeout self._find_evt = threading.Event() self._logger = logging.getLogger(f'{__name__}.{self.__class__.__name__}') @@ -205,10 +207,13 @@ class Kettle(DeviceListener, ConnectionStatusListener): assert self.device.curve == 29, f'curve type {self.device.curve} is not implemented' assert self.device.protocol == 2, f'protocol {self.device.protocol} is not supported' + kw = {} + if self._read_timeout is not None: + kw['read_timeout'] = self._read_timeout self.conn = UDPConnection(addr=self.device.addr, port=self.device.port, device_pubkey=self.device.pubkey, - device_token=bytes.fromhex(self.device_token)) + device_token=bytes.fromhex(self.device_token), **kw) if incoming_message_listener: self.conn.add_incoming_message_listener(incoming_message_listener) diff --git a/src/polaris/protocol.py b/src/polaris/protocol.py index 2a78c47..41f0776 100644 --- a/src/polaris/protocol.py +++ b/src/polaris/protocol.py @@ -28,7 +28,6 @@ _logger = logging.getLogger(__name__) PING_FREQUENCY = 3 RESEND_ATTEMPTS = 5 -READ_TIMEOUT = 1 ERROR_TIMEOUT = 15 MESSAGE_QUEUE_REMOVE_DELAY = 13 # after what time to delete (and pass False to handlers, if needed) messages with phase=DONE from queue DISCONNECT_TIMEOUT = 15 @@ -739,6 +738,7 @@ class UDPConnection(threading.Thread, ConnectionStatusListener): incoming_time: float status: ConnectionStatus reconnect_tries: int + read_timeout: int _addr_lock: threading.Lock _iml_lock: threading.Lock @@ -749,7 +749,8 @@ class UDPConnection(threading.Thread, ConnectionStatusListener): addr: Union[IPv4Address, IPv6Address], port: int, device_pubkey: bytes, - device_token: bytes): + device_token: bytes, + read_timeout: int = 1): super().__init__() self._logger = logging.getLogger(f'{__name__}.{self.__class__.__name__} <{hex(id(self))}>') self.setName(self.__class__.__name__) @@ -771,6 +772,7 @@ class UDPConnection(threading.Thread, ConnectionStatusListener): self.conn_listeners = [self] self.status = ConnectionStatus.NOT_CONNECTED self.reconnect_tries = 0 + self.read_timeout = read_timeout self._iml_lock = threading.Lock() self._csl_lock = threading.Lock() @@ -880,7 +882,7 @@ class UDPConnection(threading.Thread, ConnectionStatusListener): sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind(('0.0.0.0', self.source_port)) - sock.settimeout(READ_TIMEOUT) + sock.settimeout(self.read_timeout) while not self.interrupted: with self._st_lock: diff --git a/src/polaris_kettle_bot.py b/src/polaris_kettle_bot.py index 3ce9688..c031cb1 100755 --- a/src/polaris_kettle_bot.py +++ b/src/polaris_kettle_bot.py @@ -163,7 +163,8 @@ class KettleController(threading.Thread, self._logger = logging.getLogger(self.__class__.__name__) self.kettle = Kettle(mac=config['kettle']['mac'], - device_token=config['kettle']['token']) + device_token=config['kettle']['token'], + read_timeout=config['kettle']['read_timeout']) self.kettle_reconnect() # info |