跳转至

Camera ViewModel

controller.presentation.view_models.camera_view_model

摄像头视图模型 - Presentation层 只依赖Application层,不直接访问Domain层

CameraViewModel

CameraViewModel(app_service: CameraApplicationService, parent=None)

Bases: BaseViewModel

摄像头视图模型。

职责: - 连接 UI 和 Application Service - 管理图像显示状态(定时刷新) - 管理检测状态 - 通过 Application Service 获取图像和检测结果

架构原则: - 只依赖 Application 层,不直接访问 Domain 层 - 所有 Domain 层调用通过 Application Service 转发

属性:

名称 类型 描述
image_display_requested pyqtSignal

图像显示请求信号,携带 (image, image_type)。

status_updated pyqtSignal

状态文本更新信号。

image_info_updated pyqtSignal

图像信息更新信号。

connection_status_changed pyqtSignal

摄像头连接状态信号。

button_states_changed pyqtSignal

按钮状态变更信号,携带 (color_active, depth_active)。

clear_display_requested pyqtSignal

清除显示请求信号。

detection_status_changed pyqtSignal

检测状态变更信号。

detection_result_updated pyqtSignal

检测结果更新信号。

app_service CameraApplicationService

摄像头应用服务。

display_timer QTimer

图像刷新定时器。

初始化摄像头视图模型。

参数:

名称 类型 描述 默认
app_service CameraApplicationService

摄像头应用服务。

必需
parent QObject

父对象. Defaults to None.

None
源代码位于: src/controller/controller/presentation/view_models/camera_view_model.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def __init__(
    self,
    app_service: CameraApplicationService,
    parent=None
):
    """初始化摄像头视图模型。

    Args:
        app_service (CameraApplicationService): 摄像头应用服务。
        parent (QObject, optional): 父对象. Defaults to None.
    """
    super().__init__(parent)
    self.app_service = app_service

    # 显示状态
    self.current_display_mode = None  # None, 'color', 'depth'

    # 定时器(30FPS刷新图像)
    self.display_timer = QTimer()
    self.display_timer.timeout.connect(self._update_display)

    # 连接信号
    self._connect_signals()

connect_camera

connect_camera()

连接摄像头。

源代码位于: src/controller/controller/presentation/view_models/camera_view_model.py
74
75
76
def connect_camera(self):
    """连接摄像头。"""
    self.app_service.connect_camera()

disconnect_camera

disconnect_camera()

断开摄像头。

源代码位于: src/controller/controller/presentation/view_models/camera_view_model.py
78
79
80
81
82
83
84
85
def disconnect_camera(self):
    """断开摄像头。"""
    # 先停止显示
    if self.current_display_mode is not None:
        self.stop_display()

    # 断开摄像头
    self.app_service.disconnect_camera()

start_color_display

start_color_display()

开始显示彩色图。

源代码位于: src/controller/controller/presentation/view_models/camera_view_model.py
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def start_color_display(self):
    """开始显示彩色图。"""
    # ✅ 通过Application层检查摄像头是否连接
    if not self.app_service.is_camera_connected():
        self.status_updated.emit("请先连接摄像头")
        return

    # 设置显示模式
    self.current_display_mode = 'color'

    # 启动定时器(33ms = ~30FPS)
    self.display_timer.start(33)

    # 更新状态
    self.status_updated.emit("显示彩色图像")
    self.button_states_changed.emit(True, False)

start_depth_display

start_depth_display()

开始显示深度图。

源代码位于: src/controller/controller/presentation/view_models/camera_view_model.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def start_depth_display(self):
    """开始显示深度图。"""
    # ✅ 通过Application层检查摄像头是否连接
    if not self.app_service.is_camera_connected():
        self.status_updated.emit("请先连接摄像头")
        return

    # 设置显示模式
    self.current_display_mode = 'depth'

    # 启动定时器
    self.display_timer.start(33)

    # 更新状态
    self.status_updated.emit("显示深度图像")
    self.button_states_changed.emit(False, True)

stop_display

stop_display()

停止显示。

源代码位于: src/controller/controller/presentation/view_models/camera_view_model.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
def stop_display(self):
    """停止显示。"""
    # 停止定时器
    self.display_timer.stop()

    # 清除显示模式
    self.current_display_mode = None

    # 清除显示
    self.clear_display_requested.emit()

    # 更新状态
    self.status_updated.emit("已停止显示")
    self.button_states_changed.emit(False, False)
    self.image_info_updated.emit({})

start_detection

start_detection()

开始检测。

源代码位于: src/controller/controller/presentation/view_models/camera_view_model.py
137
138
139
def start_detection(self):
    """开始检测。"""
    self.app_service.start_detection()

stop_detection

stop_detection()

停止检测。

源代码位于: src/controller/controller/presentation/view_models/camera_view_model.py
141
142
143
def stop_detection(self):
    """停止检测。"""
    self.app_service.stop_detection()

move_to_detected_part

move_to_detected_part()

运动到检测到的零件位置。

职责:仅转发到 Application 层。

源代码位于: src/controller/controller/presentation/view_models/camera_view_model.py
145
146
147
148
149
150
def move_to_detected_part(self):
    """运动到检测到的零件位置。

    职责:仅转发到 Application 层。
    """
    self.app_service.move_to_detected_part()

cleanup

cleanup()

清理资源。

源代码位于: src/controller/controller/presentation/view_models/camera_view_model.py
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
def cleanup(self):
    """清理资源。"""
    try:
        # 停止检测(✅ 通过Application层)
        if self.app_service.is_detection_running():
            self.app_service.stop_detection()

        # 停止显示
        self.stop_display()

        # 断开摄像头(✅ 通过Application层)
        if self.app_service.is_camera_connected():
            self.app_service.disconnect_camera()

    except Exception as e:
        pass