134 lines
4.5 KiB
Python
134 lines
4.5 KiB
Python
"""设置页的事件绑定和当前客户端设备号生成。
|
|
|
|
本文件把界面操作转换为轻量信号,不直接执行 ADB 或 SQLite。
|
|
后续应用服务应在后台线程处理阻塞操作,再通过页面更新方法返回结果。
|
|
"""
|
|
|
|
import hashlib
|
|
import platform
|
|
import uuid
|
|
|
|
from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot
|
|
|
|
|
|
DEVICE_ID_PLACEHOLDER = "待生成"
|
|
|
|
|
|
def generate_client_device_id() -> str:
|
|
"""根据本机特征生成设备号,不暴露原始系统和硬件信息。"""
|
|
|
|
fingerprint = "|".join(
|
|
(
|
|
platform.system(),
|
|
platform.node(),
|
|
platform.machine(),
|
|
f"{uuid.getnode():012x}",
|
|
)
|
|
)
|
|
digest = hashlib.sha256(fingerprint.encode("utf-8")).hexdigest()[:16].upper()
|
|
return f"CLIENT-{digest}"
|
|
|
|
|
|
class SettingsPageEventBinder(QObject):
|
|
"""绑定设备管理控件,并向应用层发出稳定事件。"""
|
|
|
|
searchRequested = pyqtSignal()
|
|
connectRequested = pyqtSignal(str)
|
|
currentDeviceSaveRequested = pyqtSignal(str, str)
|
|
saveRequested = pyqtSignal(str, str)
|
|
deleteRequested = pyqtSignal(str)
|
|
|
|
def __init__(self, page):
|
|
super().__init__(page)
|
|
self._page = page
|
|
self._busy = False
|
|
|
|
page.currentDeviceSaveButton.clicked.connect(
|
|
self._request_save_current_device
|
|
)
|
|
page.searchButton.clicked.connect(self._request_search)
|
|
page.connectButton.clicked.connect(self._request_connect)
|
|
page.saveButton.clicked.connect(self._request_save)
|
|
page.deleteButton.clicked.connect(self._request_delete)
|
|
page.addressInput.textChanged.connect(self._sync_button_state)
|
|
page.deviceTableModel.checkedDeviceChanged.connect(self._sync_button_state)
|
|
self._sync_button_state()
|
|
|
|
@pyqtSlot()
|
|
def _request_save_current_device(self) -> None:
|
|
if self._busy:
|
|
return
|
|
|
|
device_id = self._page.deviceIdInput.text().strip()
|
|
if not device_id or device_id == DEVICE_ID_PLACEHOLDER:
|
|
device_id = generate_client_device_id()
|
|
self._page.deviceIdInput.setText(device_id)
|
|
|
|
device_name = self._page.deviceNameInput.text().strip()
|
|
self._page.deviceNameInput.setText(device_name)
|
|
self.currentDeviceSaveRequested.emit(device_id, device_name)
|
|
|
|
@pyqtSlot()
|
|
def _request_search(self) -> None:
|
|
if self._busy:
|
|
return
|
|
self.searchRequested.emit()
|
|
|
|
@pyqtSlot()
|
|
def _request_connect(self) -> None:
|
|
if self._busy:
|
|
return
|
|
|
|
address = self._page.addressInput.text().strip()
|
|
if not address:
|
|
self._page.deviceStatusLabel.setText(
|
|
"连接失败:请先填写无线调试地址,例如 192.168.0.173:5555"
|
|
)
|
|
self._page.addressInput.setFocus()
|
|
return
|
|
self.connectRequested.emit(address)
|
|
|
|
@pyqtSlot()
|
|
def _request_save(self) -> None:
|
|
if self._busy:
|
|
return
|
|
|
|
serial = self._page.deviceTableModel.checked_serial
|
|
if not serial:
|
|
self._page.deviceStatusLabel.setText("保存失败:请先勾选一台已连接设备")
|
|
self._page.deviceTable.setFocus()
|
|
return
|
|
|
|
device_name = self._page.deviceNameInput.text().strip()
|
|
self.saveRequested.emit(device_name, serial)
|
|
|
|
@pyqtSlot()
|
|
def _request_delete(self) -> None:
|
|
if self._busy:
|
|
return
|
|
|
|
serial = self._page.deviceTableModel.checked_serial
|
|
if not serial:
|
|
self._page.deviceStatusLabel.setText("删除失败:请先勾选一台设备")
|
|
self._page.deviceTable.setFocus()
|
|
return
|
|
self.deleteRequested.emit(serial)
|
|
|
|
@pyqtSlot()
|
|
def _sync_button_state(self, *_args) -> None:
|
|
serial = self._page.deviceTableModel.checked_serial
|
|
has_address = bool(self._page.addressInput.text().strip())
|
|
self._page.currentDeviceSaveButton.setEnabled(not self._busy)
|
|
self._page.searchButton.setEnabled(not self._busy)
|
|
self._page.connectButton.setEnabled(not self._busy and has_address)
|
|
self._page.saveButton.setEnabled(not self._busy and bool(serial))
|
|
self._page.deleteButton.setEnabled(not self._busy and bool(serial))
|
|
|
|
def set_busy(self, busy: bool, message: str = "") -> None:
|
|
"""切换界面忙碌状态,防止用户重复提交设备命令。"""
|
|
|
|
self._busy = busy
|
|
self._sync_button_state()
|
|
if message:
|
|
self._page.deviceStatusLabel.setText(message)
|