Connecting task moved?

This commit is contained in:
atse 2024-07-09 16:39:11 +08:00
parent 371ddcdc5b
commit af53926b01
3 changed files with 15 additions and 38 deletions

View File

@ -7,56 +7,30 @@ class CommandError(Exception):
pass pass
class StoppedConnecting(Exception):
pass
class AsyncioClient: class AsyncioClient:
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()
self._report_mode_on = False self._report_mode_on = False
self.timeout = None 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. """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:: Example::
client = AsyncioClient() client = AsyncioClient()
try: await client.start_session()
await client.start_session()
except StoppedConnecting:
print("Stopped connecting")
""" """
self._connecting_task = asyncio.create_task( self._reader, self._writer = await asyncio.open_connection(host, port)
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
await self._check_zero_limits() await self._check_zero_limits()
def connecting(self):
"""Returns True if client is connecting"""
return self._connecting_task is not None
def connected(self): def connected(self):
"""Returns True if client is connected""" """Returns True if client is connected"""
return self._writer is not None return self._writer is not None
async def end_session(self): async def end_session(self):
"""End session to Thermostat if connected, cancel connection if connecting""" """End session to Thermostat"""
if self._connecting_task is not None:
self._connecting_task.cancel()
if self._writer is None: if self._writer is None:
return return

View File

@ -29,7 +29,7 @@ class Thermostat(QObject, metaclass=PropertyMeta):
super().__init__(parent) super().__init__(parent)
async def start_session(self, host, port): 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): async def run(self):
self._update_params_task = asyncio.create_task(self.update_params()) self._update_params_task = asyncio.create_task(self.update_params())
@ -70,9 +70,6 @@ class Thermostat(QObject, metaclass=PropertyMeta):
def connected(self): def connected(self):
return self._client.connected() return self._client.connected()
def connecting(self):
return self._client.connecting()
def start_watching(self): def start_watching(self):
self._watch_task = asyncio.create_task(self.run()) self._watch_task = asyncio.create_task(self.run())

View File

@ -12,7 +12,6 @@ import json
from autotune import PIDAutotuneState from autotune import PIDAutotuneState
from qasync import asyncSlot, asyncClose from qasync import asyncSlot, asyncClose
import qasync import qasync
from pytec.aioclient import StoppedConnecting
import asyncio import asyncio
import logging import logging
import argparse import argparse
@ -236,19 +235,26 @@ class MainWindow(QtWidgets.QMainWindow):
self.conn_menu.host_set_line.text(), self.conn_menu.host_set_line.text(),
self.conn_menu.port_set_spin.value(), self.conn_menu.port_set_spin.value(),
) )
self._connecting_task = None
try: 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.status_lbl.setText("Connecting...")
self.connect_btn.setText("Stop") self.connect_btn.setText("Stop")
self.conn_menu.host_set_line.setEnabled(False) self.conn_menu.host_set_line.setEnabled(False)
self.conn_menu.port_set_spin.setEnabled(False) self.conn_menu.port_set_spin.setEnabled(False)
try: try:
await self.thermostat.start_session(host=host, port=port) self._connecting_task = asyncio.wait_for(
except StoppedConnecting: self.thermostat.start_session(host=host, port=port), timeout=5
)
await self._connecting_task
except asyncio.TimeoutError:
return return
await self._on_connection_changed(True) await self._on_connection_changed(True)
else: else:
if self._connecting_task is not None:
self._connecting_task.cancel()
await self.bail() await self.bail()
# TODO: Remove asyncio.TimeoutError in Python 3.11 # TODO: Remove asyncio.TimeoutError in Python 3.11