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
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

View File

@ -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())

View File

@ -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