forked from M-Labs/thermostat
Simplify on_connect_btn_clicked
Raise if OSError
This commit is contained in:
parent
ed34eb9e03
commit
7e0dd61f79
@ -63,6 +63,7 @@ class MainWindow(QtWidgets.QMainWindow):
|
|||||||
self.info_box = InfoBox()
|
self.info_box = InfoBox()
|
||||||
|
|
||||||
self.thermostat = Thermostat(self, self.report_refresh_spin.value())
|
self.thermostat = Thermostat(self, self.report_refresh_spin.value())
|
||||||
|
self._connecting_task = None
|
||||||
|
|
||||||
def handle_connection_error():
|
def handle_connection_error():
|
||||||
self.info_box.display_info_box(
|
self.info_box.display_info_box(
|
||||||
@ -231,38 +232,37 @@ class MainWindow(QtWidgets.QMainWindow):
|
|||||||
|
|
||||||
@asyncSlot()
|
@asyncSlot()
|
||||||
async def on_connect_btn_clicked(self):
|
async def on_connect_btn_clicked(self):
|
||||||
host, port = (
|
if (self._connecting_task is None) and (not self.thermostat.connected()):
|
||||||
self.conn_menu.host_set_line.text(),
|
|
||||||
self.conn_menu.port_set_spin.value(),
|
|
||||||
)
|
|
||||||
|
|
||||||
self._connecting_task = None
|
|
||||||
try:
|
|
||||||
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:
|
self._connecting_task = asyncio.create_task(
|
||||||
self._connecting_task = asyncio.wait_for(
|
asyncio.wait_for(
|
||||||
self.thermostat.start_session(host=host, port=port), timeout=5
|
self.thermostat.start_session(
|
||||||
|
host=self.conn_menu.host_set_line.text(),
|
||||||
|
port=self.conn_menu.port_set_spin.value(),
|
||||||
|
),
|
||||||
|
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
|
|
||||||
except (OSError, asyncio.TimeoutError):
|
|
||||||
try:
|
try:
|
||||||
|
await self._connecting_task
|
||||||
|
except (OSError, asyncio.TimeoutError, asyncio.CancelledError) as exc:
|
||||||
|
await self.bail()
|
||||||
|
if isinstance(exc, asyncio.CancelledError):
|
||||||
|
return
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
await self._on_connection_changed(True)
|
||||||
|
finally:
|
||||||
|
self._connecting_task = None
|
||||||
|
|
||||||
|
elif self._connecting_task is not None:
|
||||||
|
self._connecting_task.cancel()
|
||||||
|
else:
|
||||||
await self.bail()
|
await self.bail()
|
||||||
except ConnectionResetError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
@asyncSlot()
|
@asyncSlot()
|
||||||
async def bail(self):
|
async def bail(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user