Init client once

No none-ing
This commit is contained in:
atse 2023-07-14 16:01:53 +08:00
parent fa60439e39
commit 659d0d0835
2 changed files with 14 additions and 7 deletions

View File

@ -19,13 +19,22 @@ class Client:
await self._check_zero_limits()
def is_connecting(self):
return self._connecting_task is not None
def is_connected(self):
return self._reader is not None
async def disconnect(self):
if self._connecting_task is not None:
self._connecting_task.cancel()
self._connecting_task = None
return
self._writer.close()
await self._writer.wait_closed()
self._reader = None
self._writer = None
async def _check_zero_limits(self):
pwm_report = await self.get_pwm()

View File

@ -74,7 +74,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self.fan_pwm_recommended = False
self.tec_client: Client = None
self.tec_client = Client()
self.client_watcher: ClientWatcher = None
if args.connect:
@ -128,7 +128,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
@asyncSlot(int)
async def fan_set(self, value):
if self.tec_client is None or self.fan_auto_box.isChecked():
if not self.tec_client.is_connected() or self.fan_auto_box.isChecked():
return
await self.tec_client.set_param("fan", value)
if not self.fan_pwm_recommended:
@ -136,7 +136,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
@asyncSlot(int)
async def fan_auto_set(self, enabled):
if self.tec_client is None:
if not self.tec_client.is_connected():
return
self.fan_power_slider.setEnabled(not enabled)
if enabled:
@ -149,20 +149,19 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
async def closeEvent(self, event):
if self.client_watcher is not None:
self.client_watcher.stop_watching()
if self.tec_client is not None:
if self.tec_client.is_connecting() or self.tec_client.is_connected():
await self.tec_client.disconnect()
@asyncSlot()
async def connect(self):
ip, port = self.ip_set_line.text(), self.port_set_spin.value()
try:
if self.tec_client is None:
if not (self.tec_client.is_connecting() or self.tec_client.is_connected()):
self.status_lbl.setText("Connecting...")
self.ip_set_line.setEnabled(False)
self.port_set_spin.setEnabled(False)
self.connect_btn.setText("Stop")
self.tec_client = Client()
try:
await self.tec_client.connect(host=ip, port=port, timeout=30)
except asyncio.CancelledError:
@ -184,7 +183,6 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self.client_watcher = None
await self.tec_client.disconnect()
self.tec_client = None
self._on_connection_changed(False)
except (OSError, TimeoutError) as e: