跳转至

Serial ViewModel

controller.presentation.view_models.serial_view_model

串口视图模型 - Presentation层 纯信号转发层,不包含业务逻辑

SerialViewModel

SerialViewModel(serial_service: SerialApplicationService, command_hub_service: CommandHubService, parent=None)

Bases: BaseViewModel

串口视图模型。

纯信号转发层,不包含业务逻辑,负责 UI 与串口服务的交互。

属性:

名称 类型 描述
port_list_updated pyqtSignal

端口列表更新信号,携带端口列表。

serial_service SerialApplicationService

串口应用服务。

command_hub_service CommandHubService

命令中心服务。

初始化视图模型。

参数:

名称 类型 描述 默认
serial_service SerialApplicationService

串口应用服务。

必需
command_hub_service CommandHubService

命令中心服务。

必需
parent QObject

父对象. Defaults to None.

None
源代码位于: src/controller/controller/presentation/view_models/serial_view_model.py
24
25
26
27
28
29
30
31
32
33
34
35
36
def __init__(self, serial_service: SerialApplicationService, command_hub_service: CommandHubService, parent=None):
    """初始化视图模型。

    Args:
        serial_service (SerialApplicationService): 串口应用服务。
        command_hub_service (CommandHubService): 命令中心服务。
        parent (QObject, optional): 父对象. Defaults to None.
    """
    super().__init__(parent)

    self.serial_service = serial_service
    self.command_hub_service = command_hub_service
    self._connect_service_signals()

refresh_ports

refresh_ports() -> None

刷新端口列表命令 - 直接转发到Service。

源代码位于: src/controller/controller/presentation/view_models/serial_view_model.py
40
41
42
def refresh_ports(self) -> None:
    """刷新端口列表命令 - 直接转发到Service。"""
    self.serial_service.refresh_ports()

connect_serial

connect_serial(port: str, config: Dict[str, Any]) -> None

连接串口命令 - 直接转发到Service。

参数:

名称 类型 描述 默认
port str

端口名称。

必需
config Dict[str, Any]

配置参数。

必需
源代码位于: src/controller/controller/presentation/view_models/serial_view_model.py
44
45
46
47
48
49
50
51
def connect_serial(self, port: str, config: Dict[str, Any]) -> None:
    """连接串口命令 - 直接转发到Service。

    Args:
        port (str): 端口名称。
        config (Dict[str, Any]): 配置参数。
    """
    self.serial_service.connect_serial(port, config)

disconnect_serial

disconnect_serial() -> None

断开连接命令 - 直接转发到Service。

源代码位于: src/controller/controller/presentation/view_models/serial_view_model.py
53
54
55
def disconnect_serial(self) -> None:
    """断开连接命令 - 直接转发到Service。"""
    self.serial_service.disconnect_serial()

get_available_ports

get_available_ports() -> List[str]

获取可用端口列表 - 委托给Service。

返回:

类型 描述
List[str]

List[str]: 端口列表。

源代码位于: src/controller/controller/presentation/view_models/serial_view_model.py
59
60
61
62
63
64
65
def get_available_ports(self) -> List[str]:
    """获取可用端口列表 - 委托给Service。

    Returns:
        List[str]: 端口列表。
    """
    return self.serial_service.get_available_ports()

get_current_port

get_current_port() -> Optional[str]

获取当前连接的端口 - 委托给Service。

返回:

类型 描述
Optional[str]

Optional[str]: 端口名称。

源代码位于: src/controller/controller/presentation/view_models/serial_view_model.py
67
68
69
70
71
72
73
def get_current_port(self) -> Optional[str]:
    """获取当前连接的端口 - 委托给Service。

    Returns:
        Optional[str]: 端口名称。
    """
    return self.serial_service.get_current_port()

get_connection_status

get_connection_status() -> bool

获取连接状态 - 委托给Service。

返回:

名称 类型 描述
bool bool

是否已连接。

源代码位于: src/controller/controller/presentation/view_models/serial_view_model.py
75
76
77
78
79
80
81
def get_connection_status(self) -> bool:
    """获取连接状态 - 委托给Service。

    Returns:
        bool: 是否已连接。
    """
    return self.serial_service.is_connected()

get_port_info

get_port_info(port_name: str) -> Optional[Dict[str, str]]

获取端口信息 - 委托给Service。

参数:

名称 类型 描述 默认
port_name str

端口名称。

必需

返回:

类型 描述
Optional[Dict[str, str]]

Optional[Dict[str, str]]: 端口信息。

源代码位于: src/controller/controller/presentation/view_models/serial_view_model.py
83
84
85
86
87
88
89
90
91
92
def get_port_info(self, port_name: str) -> Optional[Dict[str, str]]:
    """获取端口信息 - 委托给Service。

    Args:
        port_name (str): 端口名称。

    Returns:
        Optional[Dict[str, str]]: 端口信息。
    """
    return self.serial_service.get_port_info(port_name)

cleanup

cleanup() -> None

清理视图模型 - 断开信号连接。

源代码位于: src/controller/controller/presentation/view_models/serial_view_model.py
110
111
112
113
def cleanup(self) -> None:
    """清理视图模型 - 断开信号连接。"""
    self._disconnect_service_signals()
    super().cleanup()