diff --git a/pytec/pytec/aioclient.py b/pytec/pytec/aioclient.py index e423e89..7b568e9 100644 --- a/pytec/pytec/aioclient.py +++ b/pytec/pytec/aioclient.py @@ -7,56 +7,30 @@ class CommandError(Exception): pass -class StoppedConnecting(Exception): - pass - - class AsyncioClient: def __init__(self): self._reader = None self._writer = None - self._connecting_task = None self._command_lock = asyncio.Lock() self._report_mode_on = False self.timeout = None - async def start_session(self, host="192.168.1.26", port=23, timeout=None): + async def start_session(self, host="192.168.1.26", port=23): """Start session to Thermostat at specified host and port. - Throws StoppedConnecting if disconnect was called while connecting. - Throws asyncio.TimeoutError if timeout was exceeded. Example:: client = AsyncioClient() - try: - await client.start_session() - except StoppedConnecting: - print("Stopped connecting") + await client.start_session() """ - self._connecting_task = asyncio.create_task( - asyncio.wait_for(asyncio.open_connection(host, port), timeout) - ) - self.timeout = timeout - try: - self._reader, self._writer = await self._connecting_task - except asyncio.CancelledError: - raise StoppedConnecting - finally: - self._connecting_task = None - + self._reader, self._writer = await asyncio.open_connection(host, port) await self._check_zero_limits() - def connecting(self): - """Returns True if client is connecting""" - return self._connecting_task is not None - def connected(self): """Returns True if client is connected""" return self._writer is not None async def end_session(self): - """End session to Thermostat if connected, cancel connection if connecting""" - if self._connecting_task is not None: - self._connecting_task.cancel() + """End session to Thermostat""" if self._writer is None: return diff --git a/pytec/pytec/gui/model/thermostat.py b/pytec/pytec/gui/model/thermostat.py index 8dc8e52..061039f 100644 --- a/pytec/pytec/gui/model/thermostat.py +++ b/pytec/pytec/gui/model/thermostat.py @@ -29,7 +29,7 @@ class Thermostat(QObject, metaclass=PropertyMeta): super().__init__(parent) async def start_session(self, host, port): - await self._client.start_session(host, port, timeout=5) + await self._client.start_session(host, port) async def run(self): self._update_params_task = asyncio.create_task(self.update_params()) @@ -70,9 +70,6 @@ class Thermostat(QObject, metaclass=PropertyMeta): def connected(self): return self._client.connected() - def connecting(self): - return self._client.connecting() - def start_watching(self): self._watch_task = asyncio.create_task(self.run()) diff --git a/pytec/tec_qt.py b/pytec/tec_qt.py index 3fa7646..3d45c15 100755 --- a/pytec/tec_qt.py +++ b/pytec/tec_qt.py @@ -12,7 +12,6 @@ import json from autotune import PIDAutotuneState from qasync import asyncSlot, asyncClose import qasync -from pytec.aioclient import StoppedConnecting import asyncio import logging import argparse @@ -236,19 +235,26 @@ class MainWindow(QtWidgets.QMainWindow): self.conn_menu.host_set_line.text(), self.conn_menu.port_set_spin.value(), ) + + self._connecting_task = None try: - if not (self.thermostat.connecting() or self.thermostat.connected()): + if (self._connecting_task is None) or (not self.thermostat.connected()): self.status_lbl.setText("Connecting...") self.connect_btn.setText("Stop") self.conn_menu.host_set_line.setEnabled(False) self.conn_menu.port_set_spin.setEnabled(False) try: - await self.thermostat.start_session(host=host, port=port) - except StoppedConnecting: + self._connecting_task = asyncio.wait_for( + self.thermostat.start_session(host=host, port=port), timeout=5 + ) + await self._connecting_task + except asyncio.TimeoutError: return await self._on_connection_changed(True) else: + if self._connecting_task is not None: + self._connecting_task.cancel() await self.bail() # TODO: Remove asyncio.TimeoutError in Python 3.11