GUI: Improve abnormal disconnection handling

This commit is contained in:
linuswck 2024-04-22 13:07:18 +08:00
parent bfdb1f5066
commit ba30575406
2 changed files with 25 additions and 26 deletions

View File

@ -637,21 +637,18 @@ class Kirdy:
Decode newline delimited Json objects and return the latest json received inside the buffer. Decode newline delimited Json objects and return the latest json received inside the buffer.
- buffer_size: Integer - buffer_size: Integer
""" """
try: try:
raw_response = await asyncio.wait_for(self._reader.read(buffer_size), self.timeout) raw_response = await asyncio.wait_for(self._reader.read(buffer_size), self.timeout)
except asyncio.exceptions.TimeoutError:
return {
"msg_type": "Internal Timeout"
}
response = raw_response.decode('utf-8', errors='ignore').split("\n") response = raw_response.decode('utf-8', errors='ignore').split("\n")
for item in reversed(response): for item in reversed(response):
try: try:
return json.loads(item) return json.loads(item)
except json.decoder.JSONDecodeError: except json.decoder.JSONDecodeError as e:
pass pass
return { "msg_type": "Internal No json object found in response" } return { "msg_type": "Exception"}
except TimeoutError:
return { "msg_type": "Exception"}
async def _send_raw_cmd_handler(self, cmd, lock=True, msg_type="Acknowledge"): async def _send_raw_cmd_handler(self, cmd, lock=True, msg_type="Acknowledge"):
if lock: if lock:
@ -719,12 +716,10 @@ class Kirdy:
response = await self._read_response() response = await self._read_response()
if response["msg_type"] == msg_type: if response["msg_type"] == msg_type:
return response return response
retry += 1
await asyncio.sleep(0.1)
except asyncio.exceptions.CancelledError: except asyncio.exceptions.CancelledError:
return None return None
except Exception as e:
retry += 1
logging.error(f"_send_cmd Exception: {e}")
await asyncio.sleep(0.1)
raise NoAckRecv raise NoAckRecv
async def report_mode(self, report_interval = 0.0, buffer_size=16384): async def report_mode(self, report_interval = 0.0, buffer_size=16384):

View File

@ -30,6 +30,8 @@ from dateutil import tz
import math import math
import socket import socket
COMMON_ERROR_MSG = "Connection Timeout. Disconnecting."
def get_argparser(): def get_argparser():
parser = argparse.ArgumentParser(description="ARTIQ master") parser = argparse.ArgumentParser(description="ARTIQ master")
@ -70,12 +72,13 @@ class KirdyDataWatcher(QObject):
task = asyncio.create_task(self.signal_emitter()) task = asyncio.create_task(self.signal_emitter())
while True: while True:
if task.done(): if task.done():
_ = task.result()
task = asyncio.create_task(self.signal_emitter()) task = asyncio.create_task(self.signal_emitter())
await asyncio.sleep(self._update_s) await asyncio.sleep(self._update_s)
except asyncio.CancelledError: except asyncio.CancelledError:
pass task.cancel()
except Exception as e: except Exception as e:
logging.error(f"Encountered an error: {e}. disconnecting.", exc_info=True) logging.error(COMMON_ERROR_MSG)
self._kirdy.stop_report_mode() self._kirdy.stop_report_mode()
self.connection_error_sig.emit() self.connection_error_sig.emit()
@ -102,10 +105,11 @@ class KirdyDataWatcher(QObject):
async def report_mode(self): async def report_mode(self):
try: try:
async for status_report in self._kirdy.report_mode(): async for status_report in self._kirdy.report_mode():
if status_report["msg_type"] == "Exception":
raise TimeoutError("Connection Timeout")
self.report_update_sig.emit(status_report) self.report_update_sig.emit(status_report)
except Exception as e: except Exception as e:
logging.error(f"Encountered an error: {e}, disconnecting", exc_info=True) logging.error(f"{COMMON_ERROR_MSG}")
self._kirdy.stop_report_mode()
self.connection_error_sig.emit() self.connection_error_sig.emit()
@pyqtSlot(float) @pyqtSlot(float)
@ -216,7 +220,7 @@ class Graphs:
self.tec_i_measure_connector.cb_append_data_point(tec_i_measure, ts) self.tec_i_measure_connector.cb_append_data_point(tec_i_measure, ts)
self.tec_i_target_connector.cb_append_data_point(tec_i_set, ts) self.tec_i_target_connector.cb_append_data_point(tec_i_set, ts)
except Exception as e: except Exception as e:
logging.error(f"Graph Value cannot be updated. Error:{e}. Data:{report}") logging.error(f"Graph Value cannot be updated. Data:{report}", exc_info=True)
def clear_data_pts(self): def clear_data_pts(self):
for connector in self.connectors: for connector in self.connectors:
@ -685,7 +689,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self.params[1].child('Photodiode Monitor Config', 'Responsitivity').setValue(0) self.params[1].child('Photodiode Monitor Config', 'Responsitivity').setValue(0)
self.params[1].child('Photodiode Monitor Config', 'Dark Current').setValue(settings["pd_mon_params"]["i_dark"]) self.params[1].child('Photodiode Monitor Config', 'Dark Current').setValue(settings["pd_mon_params"]["i_dark"])
except Exception as e: except Exception as e:
logging.error(f"Params tree cannot be updated. Error:{e}. Data:{settings}") logging.error(f"Params tree cannot be updated. Data:{settings}", exc_info=True)
@asyncSlot(dict) @asyncSlot(dict)
async def update_ld_ctrl_panel_readings(self, report): async def update_ld_ctrl_panel_readings(self, report):
@ -704,7 +708,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self.params[1].child('Readings', 'PD Power').setValue(0) self.params[1].child('Readings', 'PD Power').setValue(0)
self.params[1].child('Readings', 'LF Mod Impedance').setValue(report["term_status"]) self.params[1].child('Readings', 'LF Mod Impedance').setValue(report["term_status"])
except Exception as e: except Exception as e:
logging.error(f"Params tree cannot be updated. Error:{e}. Data:{report}") logging.error(f"Params tree cannot be updated. Data:{report}", exc_info=True)
@asyncSlot(dict) @asyncSlot(dict)
async def update_thermostat_ctrl_panel_settings(self, settings): async def update_thermostat_ctrl_panel_settings(self, settings):
@ -732,7 +736,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self.graphs.set_temp_setpoint_line(temp=round(settings["temperature_setpoint"], 6)) self.graphs.set_temp_setpoint_line(temp=round(settings["temperature_setpoint"], 6))
self.graphs.set_temp_setpoint_line(visible=settings['pid_engaged']) self.graphs.set_temp_setpoint_line(visible=settings['pid_engaged'])
except Exception as e: except Exception as e:
logging.error(f"Params tree cannot be updated. Error:{e}. Data:{settings}") logging.error(f"Params tree cannot be updated. Data:{settings}", exc_info=True)
@asyncSlot(dict) @asyncSlot(dict)
async def update_thermostat_ctrl_panel_readings(self, report): async def update_thermostat_ctrl_panel_readings(self, report):
@ -745,7 +749,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self.params[3].child('Readings', 'Temperature').setValue(report["temperature"]) self.params[3].child('Readings', 'Temperature').setValue(report["temperature"])
self.params[3].child('Readings', 'Current through TEC').setValue(report["tec_i"]) self.params[3].child('Readings', 'Current through TEC').setValue(report["tec_i"])
except Exception as e: except Exception as e:
logging.error(f"Params tree cannot be updated. Error:{e}. Data:{report}") logging.error(f"Params tree cannot be updated. Data:{report}", exc_info=True)
@pyqtSlot(int) @pyqtSlot(int)
def set_max_samples(self, samples: int): def set_max_samples(self, samples: int):