Private GIT

Skip to content
Snippets Groups Projects
Commit 89cebac5 authored by Sean Rees's avatar Sean Rees
Browse files

Make reconnects more aggressive

Previously: we would retry once. If this failed, it would require the whole
binary to be restarted. Now: we attach a timer to periodically retry and we
also reset discovery to ensure the underlying zeroconf libraries notify us
when the device reappears.
parent 7f45fd12
Branches
No related tags found
No related merge requests found
...@@ -53,11 +53,23 @@ class DeviceWrapper: ...@@ -53,11 +53,23 @@ class DeviceWrapper:
"""True if we're connected to the Dyson device.""" """True if we're connected to the Dyson device."""
return self.libdyson.is_connected return self.libdyson.is_connected
def connect(self, host: str): def connect(self, host: str, retry_on_timeout_secs: int=30):
"""Connect to the device and start the environmental monitoring """Connect to the device and start the environmental monitoring
timer.""" timer.
Args:
host: ip or hostname of Dyson device
retry_on_timeout_secs: number of seconds to wait in between retries. this will block the running thread.
"""
if self.is_connected:
logging.info('Already connected to %s (%s); no need to reconnect.', host, self.serial)
else:
try:
self.libdyson.connect(host) self.libdyson.connect(host)
self._refresh_timer() self._refresh_timer()
except libdyson.exceptions.DysonConnectTimeout:
logging.error('Timeout connecting to %s (%s); will retry', host, self.serial)
threading.Timer(retry_on_timeout_secs, self.connect, args=[host]).start()
def disconnect(self): def disconnect(self):
"""Disconnect from the Dyson device.""" """Disconnect from the Dyson device."""
...@@ -75,7 +87,7 @@ class DeviceWrapper: ...@@ -75,7 +87,7 @@ class DeviceWrapper:
self.libdyson.request_environmental_data() self.libdyson.request_environmental_data()
self._refresh_timer() self._refresh_timer()
else: else:
logging.debug('Device %s is disconnected.') logging.debug('Device %s is disconnected.', self.serial)
def _create_libdyson_device(self): def _create_libdyson_device(self):
return libdyson.get_device(self.serial, self._config_device.credentials, return libdyson.get_device(self.serial, self._config_device.credentials,
...@@ -143,8 +155,10 @@ class ConnectionManager: ...@@ -143,8 +155,10 @@ class ConnectionManager:
logging.debug('Received update from %s: %s', device.serial, message) logging.debug('Received update from %s: %s', device.serial, message)
if not device.is_connected: if not device.is_connected:
logging.info( logging.info(
'Device %s is now disconnected, clearing it and re-adding.', device.serial) 'Device %s is now disconnected, clearing it and re-adding', device.serial)
device.disconnect() device.disconnect()
self._discovery.stop_discovery()
self._discovery.start_discovery()
self._add_device(device, add_listener=False) self._add_device(device, add_listener=False)
return return
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment