Преглед изворни кода

feat(photo): 添加拍摄过程中的连接状态检测和图像处理通道

- 新增 LINK_ERRORS 数组用于检测相机和 MCU 连接错误
- 添加 imageProcess 通信通道用于图像处理流程
- 实现 failIfLinkLost 函数在连接丢失时立即返回失败
- 新增 linkErrorMessage 函数统一处理连接错误消息
- 在文档中更新了拍摄接口的连接错误码说明
- 完善了拍摄过程中相机和 MCU 断开连接的错误处理机制
panqiuyao пре 2 недеља
родитељ
комит
3488b11c6d
2 измењених фајлова са 36 додато и 1 уклоњено
  1. 3 1
      frontend/docs/takePhoto.API.md
  2. 33 0
      frontend/src/utils/takePhotoApi.ts

+ 3 - 1
frontend/docs/takePhoto.API.md

@@ -108,7 +108,9 @@ POST /take_photo
 | 拍摄程序正在运行,请稍候 | 已有拍摄在进行,不要并发调用 |
 | 拍照接口仅在客户端内可用 | 客户端未启动,或不是在客户端里访问 |
 | 设备服务未连接 | 8 秒内连不上本机拍摄服务 |
-| mcu设备未连接,请先连接设备 | 拍摄设备未连接 |
+| mcu设备未连接,请先连接设备 | 拍摄开始前 MCU 未连接 |
+| MCU 连接失败 / MCU串口未连接,请检查 | 拍摄过程中 MCU 断开,接口立即返回失败 |
+| 请检查相机是否连接 / 相机未连接 | 拍摄过程中相机断开或相机软件未打开,接口立即返回失败 |
 | mcu设备未初始化 | 设备未完成初始化 |
 | 当前有未完成的任务,请稍后再试 | 设备上还有未结束的动作 |
 | 货号[xxx]已存在,请勿重复拍摄~ | 该货号已有拍摄记录,需先在客户端删除后再拍 |

+ 33 - 0
frontend/src/utils/takePhotoApi.ts

@@ -24,6 +24,17 @@ let shooting = false
 
 const TAKE_PHOTO_TIMEOUT = 10 * 60 * 1000
 const MCU_ERRORS = ['mcu设备未连接', 'mcu设备未初始化', '当前有未完成的任务']
+const LINK_ERRORS = [
+  '相机未连接',
+  '请检查相机',
+  '请先连接相机',
+  '相机是否连接',
+  'MCU 连接失败',
+  'MCU连接失败',
+  'MCU串口未连接',
+  'mcu设备未连接',
+  'MCU硬件连接失败',
+]
 
 type IpcLike = {
   on: (channel: string, listener: (...args: any[]) => void) => void
@@ -148,6 +159,7 @@ function waitForShoot(ipc: IpcLike) {
     runMcu: icpList.socket.message + '_run_mcu',
     mcu: icpList.socket.message + '_mcu',
     photoTake: icpList.socket.message + '_photo_take',
+    imageProcess: icpList.socket.message + '_image_process',
     finish: icpList.socket.message + '_photo_take_finish',
     stop: icpList.socket.message + '_run_mcu_stop',
   }
@@ -164,20 +176,32 @@ function waitForShoot(ipc: IpcLike) {
     resolveFn(result)
   }
 
+  const failIfLinkLost = (result: any) => {
+    const msg = linkErrorMessage(result)
+    if (!msg) return false
+    done({ code: 1, msg }, true)
+    return true
+  }
   handlers[channels.runMcu] = (_event, result) => {
+    if (failIfLinkLost(result)) return
     if (result && result.code !== 0) {
       done({ code: 1, msg: result.msg || '拍照失败' })
     }
   }
   handlers[channels.mcu] = (_event, result) => {
+    if (failIfLinkLost(result)) return
     const msg = String(result?.msg || '')
     if (result?.code === 1 && MCU_ERRORS.some((item) => msg.includes(item))) {
       done({ code: 1, msg }, true)
     }
   }
   handlers[channels.photoTake] = (_event, result) => {
+    if (failIfLinkLost(result)) return
     if (result?.code !== 0 && result?.msg) stepError = result.msg
   }
+  handlers[channels.imageProcess] = (_event, result) => {
+    failIfLinkLost(result)
+  }
   handlers[channels.stop] = (_event, result) => {
     stopped = true
     stepError = result?.msg || '拍摄已终止'
@@ -217,6 +241,15 @@ function waitForShoot(ipc: IpcLike) {
   }
 }
 
+function linkErrorMessage(result: any): string | null {
+  const msg = String(result?.msg || '')
+  if (!msg) return null
+  if (LINK_ERRORS.some((item) => msg.includes(item))) return msg
+  const status = result?.status ?? result?.device_status
+  if (status === -1 && (msg.includes('相机') || msg.includes('MCU') || msg.includes('mcu'))) return msg
+  return null
+}
+
 function keepOn(ipc: IpcLike, channel: string, handler: (...args: any[]) => void) {
   let set = keptListeners.get(channel)
   if (!set) {