forked from M-Labs/thermostat
Put the connecting task in aioclient
This commit is contained in:
parent
7e56f2d879
commit
fa60439e39
|
@ -9,13 +9,21 @@ class Client:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._reader = None
|
self._reader = None
|
||||||
self._writer = None
|
self._writer = None
|
||||||
|
self._connecting_task = None
|
||||||
self._command_lock = asyncio.Lock()
|
self._command_lock = asyncio.Lock()
|
||||||
|
|
||||||
async def connect(self, host='192.168.1.26', port=23, timeout=None):
|
async def connect(self, host='192.168.1.26', port=23, timeout=None):
|
||||||
self._reader, self._writer = await asyncio.open_connection(host, port)
|
self._connecting_task = asyncio.create_task(asyncio.open_connection(host, port))
|
||||||
|
self._reader, self._writer = await self._connecting_task
|
||||||
|
self._connecting_task = None
|
||||||
|
|
||||||
await self._check_zero_limits()
|
await self._check_zero_limits()
|
||||||
|
|
||||||
async def disconnect(self):
|
async def disconnect(self):
|
||||||
|
if self._connecting_task is not None:
|
||||||
|
self._connecting_task.cancel()
|
||||||
|
return
|
||||||
|
|
||||||
self._writer.close()
|
self._writer.close()
|
||||||
await self._writer.wait_closed()
|
await self._writer.wait_closed()
|
||||||
|
|
||||||
|
|
|
@ -163,13 +163,11 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
|
||||||
self.connect_btn.setText("Stop")
|
self.connect_btn.setText("Stop")
|
||||||
|
|
||||||
self.tec_client = Client()
|
self.tec_client = Client()
|
||||||
self.connect_task = asyncio.create_task(self.tec_client.connect(host=ip, port=port, timeout=30))
|
|
||||||
try:
|
try:
|
||||||
await self.connect_task
|
await self.tec_client.connect(host=ip, port=port, timeout=30)
|
||||||
except asyncio.exceptions.CancelledError:
|
except asyncio.CancelledError:
|
||||||
return
|
return
|
||||||
|
|
||||||
self.connect_btn.setEnabled(True)
|
|
||||||
self._on_connection_changed(True)
|
self._on_connection_changed(True)
|
||||||
self._status(await self.tec_client.hw_rev())
|
self._status(await self.tec_client.hw_rev())
|
||||||
self.fan_update(await self.tec_client.fan())
|
self.fan_update(await self.tec_client.fan())
|
||||||
|
@ -181,12 +179,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
|
||||||
)
|
)
|
||||||
self.client_watcher.start_watching()
|
self.client_watcher.start_watching()
|
||||||
else:
|
else:
|
||||||
if self.client_watcher is None:
|
if self.client_watcher is not None:
|
||||||
self.connect_task.cancel()
|
|
||||||
self.tec_client = None
|
|
||||||
self.on_connection_changed(False)
|
|
||||||
return
|
|
||||||
else:
|
|
||||||
self.client_watcher.stop_watching()
|
self.client_watcher.stop_watching()
|
||||||
self.client_watcher = None
|
self.client_watcher = None
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue