修改UCD连接需要重启错误

This commit is contained in:
xinzhu.yin
2026-06-16 14:31:00 +08:00
parent dc096a9805
commit 3dd383b982
5 changed files with 81 additions and 22 deletions

View File

@@ -78,8 +78,27 @@ class ConnectionController:
# -- UCD 连接 ------------------------------------------------
def connect_ucd(self, display: str) -> bool:
def _refresh_ucd_selection(self, display: str) -> tuple[list[str], str | None]:
"""Rescan devices and return (device list, valid selection or None)."""
ucd_list = self.list_ucd_devices()
if display in ucd_list:
return ucd_list, display
return ucd_list, (ucd_list[0] if ucd_list else None)
def _sync_ucd_combo(self, ucd_list: list[str], selected: str) -> None:
app = self._app
app.ucd_list_combo.config(values=ucd_list)
app.ucd_list_var.set(selected)
app.update_config()
def connect_ucd(self, display: str, *, rescan: bool = True) -> bool:
"""打开指定 UCD 设备。成功返回 True。"""
if rescan:
_, display = self._refresh_ucd_selection(display)
if not display:
self._log("未检测到 UCD 设备,请确认电源已打开后重试", level="error")
return False
if self._device.state.name != "CLOSED":
try:
self._device.close()
@@ -158,7 +177,11 @@ class ConnectionController:
def worker():
try:
ucd_ok = self.connect_ucd(app.ucd_list_var.get())
ucd_list, selected = self._refresh_ucd_selection(app.ucd_list_var.get())
app._dispatch_ui(self._sync_ucd_combo, ucd_list, selected or "")
ucd_ok = False
if selected:
ucd_ok = self.connect_ucd(selected, rescan=False)
app._dispatch_ui(
app.update_connection_indicator,
app.ucd_status_indicator, ucd_ok,
@@ -185,7 +208,19 @@ class ConnectionController:
def worker():
try:
ucd_ok = self.connect_ucd(app.ucd_list_var.get())
ucd_list, selected = self._refresh_ucd_selection(app.ucd_list_var.get())
app._dispatch_ui(self._sync_ucd_combo, ucd_list, selected or "")
if not selected:
app._dispatch_ui(
app.update_connection_indicator,
app.ucd_status_indicator,
False,
)
app._dispatch_ui(app.status_var.set, "未检测到 UCD 设备")
app._dispatch_ui(self._enable_widgets)
return
ucd_ok = self.connect_ucd(selected, rescan=False)
app._dispatch_ui(
app.update_connection_indicator,
app.ucd_status_indicator,