Gather client_watcher managment into connect

This commit is contained in:
atse 2023-07-05 13:13:54 +08:00
parent 0252c7b0e4
commit 9cf33abe06
1 changed files with 13 additions and 13 deletions

View File

@ -82,7 +82,6 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self.connect_btn.click()
def _on_connection_changed(self, result):
global client_watcher
self.graph_group.setEnabled(result)
self.hw_rev_lbl.setEnabled(result)
self.fan_group.setEnabled(result)
@ -95,10 +94,6 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
if not result:
self.hw_rev_lbl.setText("Thermostat vX.Y")
self.fan_group.setStyleSheet("")
if client_watcher:
client_watcher.stop_watching()
client_watcher = None
self.client_watcher_task = None
def _hw_rev(self, hw_rev_d: dict):
logging.debug(hw_rev_d)
@ -152,18 +147,23 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self._on_connection_changed(True)
self._hw_rev(await tec_client.hw_rev())
# self.fan_update(await tec_client.fan())
if client_watcher is None:
client_watcher = ClientWatcher(self.main_widget, self.report_refresh_spin.value())
client_watcher.fan_update.connect(self.fan_update)
self.report_apply_btn.clicked.connect(
lambda: client_watcher.set_update_s(self.report_refresh_spin.value())
)
QtWidgets.QApplication.instance().aboutToQuit.connect(client_watcher.stop_watching)
self.client_watcher_task = asyncio.create_task(client_watcher.run())
client_watcher = ClientWatcher(self.main_widget, self.report_refresh_spin.value())
client_watcher.fan_update.connect(self.fan_update)
self.report_apply_btn.clicked.connect(
lambda: client_watcher.set_update_s(self.report_refresh_spin.value())
)
QtWidgets.QApplication.instance().aboutToQuit.connect(client_watcher.stop_watching)
self.client_watcher_task = asyncio.create_task(client_watcher.run())
else:
await tec_client.disconnect()
tec_client = None
self._on_connection_changed(False)
client_watcher.stop_watching()
client_watcher = None
await self.client_watcher_task
self.client_watcher_task = None
except Exception as e:
logging.error(f"Failed communicating to the {ip}:{port}: {e}")
self._on_connection_changed(False)