Use start and end session nomenclature

Helps when we also inherit from QObject, which already has connect and
disconnect methods.
This commit is contained in:
atse 2023-08-09 11:13:43 +08:00
parent 05bc5d8809
commit c6815950d2
3 changed files with 11 additions and 10 deletions

View File

@ -3,7 +3,7 @@ from pytec.aioclient import Client
async def main(): async def main():
tec = Client() tec = Client()
await tec.connect() #(host="192.168.1.26", port=23) await tec.start_session() #(host="192.168.1.26", port=23)
await tec.set_param("s-h", 1, "t0", 20) await tec.set_param("s-h", 1, "t0", 20)
print(await tec.get_pwm()) print(await tec.get_pwm())
print(await tec.get_pid()) print(await tec.get_pid())

View File

@ -17,15 +17,15 @@ class Client:
self._report_mode_on = False self._report_mode_on = False
self.timeout = None self.timeout = None
async def connect(self, host='192.168.1.26', port=23, timeout=None): async def start_session(self, host='192.168.1.26', port=23, timeout=None):
"""Connect to the Thermostat with host and port. """Start session to Thermostat at specified host and port.
Throws StoppedConnecting if disconnect was called while connecting. Throws StoppedConnecting if disconnect was called while connecting.
Throws asyncio.TimeoutError if timeout was exceeded. Throws asyncio.TimeoutError if timeout was exceeded.
Example:: Example::
client = Client() client = Client()
try: try:
await client.connect() await client.start_session()
except StoppedConnecting: except StoppedConnecting:
print("Stopped connecting") print("Stopped connecting")
""" """
@ -50,8 +50,8 @@ class Client:
"""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 disconnect(self): async def end_session(self):
"""Disconnect the client if connected, cancel connection if connecting""" """End session to Thermostat if connected, cancel connection if connecting"""
if self._connecting_task is not None: if self._connecting_task is not None:
self._connecting_task.cancel() self._connecting_task.cancel()
@ -249,7 +249,7 @@ class Client:
self._writer.write("reset\n".encode('utf-8')) self._writer.write("reset\n".encode('utf-8'))
await self._writer.drain() await self._writer.drain()
await self.disconnect() await self.end_session()
async def dfu(self): async def dfu(self):
"""Put the Thermostat in DFU update mode """Put the Thermostat in DFU update mode
@ -262,7 +262,7 @@ class Client:
self._writer.write("dfu\n".encode('utf-8')) self._writer.write("dfu\n".encode('utf-8'))
await self._writer.drain() await self._writer.drain()
await self.disconnect() await self.end_session()
async def ipv4(self): async def ipv4(self):
"""Get the IPv4 settings of the Thermostat""" """Get the IPv4 settings of the Thermostat"""

View File

@ -570,7 +570,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self.port_set_spin.setEnabled(False) self.port_set_spin.setEnabled(False)
try: try:
await self.client.connect(host=host, port=port, timeout=30) await self.client.start_session(host=host, port=port, timeout=30)
except StoppedConnecting: except StoppedConnecting:
return return
await self._on_connection_changed(True) await self._on_connection_changed(True)
@ -581,9 +581,10 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
logging.error(f"Failed communicating to {host}:{port}: {e}") logging.error(f"Failed communicating to {host}:{port}: {e}")
await self.bail() await self.bail()
@asyncSlot()
async def bail(self): async def bail(self):
await self._on_connection_changed(False) await self._on_connection_changed(False)
await self.client.disconnect() await self.client.end_session()
@pyqtSlot(list) @pyqtSlot(list)
def plot(self, report): def plot(self, report):