Conslidate connect & disconnect actions

This commit is contained in:
atse 2023-07-18 11:01:51 +08:00
parent 5ced33594c
commit 981c28ac27
2 changed files with 9 additions and 8 deletions

View File

@ -14,10 +14,14 @@ class Client:
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._connecting_task = asyncio.create_task(asyncio.open_connection(host, port)) self._connecting_task = asyncio.create_task(asyncio.open_connection(host, port))
try:
self._reader, self._writer = await self._connecting_task self._reader, self._writer = await self._connecting_task
except asyncio.CancelledError:
return False
self._connecting_task = None self._connecting_task = None
await self._check_zero_limits() await self._check_zero_limits()
return True
def is_connecting(self): def is_connecting(self):
return self._connecting_task is not None return self._connecting_task is not None

View File

@ -104,7 +104,9 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self.status_lbl.setText("Disconnected") self.status_lbl.setText("Disconnected")
self.fan_pwm_warning.setPixmap(QtGui.QPixmap()) self.fan_pwm_warning.setPixmap(QtGui.QPixmap())
self.fan_pwm_warning.setToolTip("") self.fan_pwm_warning.setToolTip("")
self.client_watcher.stop_watching()
else: else:
self.client_watcher.start_watching()
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())
@ -171,16 +173,11 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self.ip_set_line.setEnabled(False) self.ip_set_line.setEnabled(False)
self.port_set_spin.setEnabled(False) self.port_set_spin.setEnabled(False)
try: connected = await self.tec_client.connect(host=ip, port=port, timeout=30)
await self.tec_client.connect(host=ip, port=port, timeout=30) if not connected:
except asyncio.CancelledError:
return return
await self._on_connection_changed(True) await self._on_connection_changed(True)
self.client_watcher.start_watching()
else: else:
self.client_watcher.stop_watching()
await self.tec_client.disconnect() await self.tec_client.disconnect()
await self._on_connection_changed(False) await self._on_connection_changed(False)