camera.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { get, post } from '@/utils/request'
  2. import type {
  3. IBaseResponse,
  4. IListResponse,
  5. IPageResponse,
  6. BaseResponse,
  7. CameraDTO,
  8. ChannelDTO,
  9. CameraInfoDTO,
  10. CameraAddRequest,
  11. CameraUpdateRequest,
  12. CameraListRequest,
  13. SwitchChannelRequest,
  14. PTZAction,
  15. PTZControlRequest,
  16. AdminPTZRequest
  17. } from '@/types'
  18. // ==================== Controller APIs (MVP) ====================
  19. // 获取摄像头列表 (POST)
  20. export function listCameras(machineId?: string): Promise<IListResponse<CameraDTO>> {
  21. return post('/camera/list', machineId ? { machineId } : {})
  22. }
  23. // 获取摄像头信息
  24. export function getCamera(cameraId: string): Promise<IBaseResponse<CameraDTO>> {
  25. return get(`/camera/${cameraId}`)
  26. }
  27. // 切换摄像头通道 (MVP核心)
  28. export function switchChannel(data: SwitchChannelRequest): Promise<IBaseResponse<ChannelDTO>> {
  29. return post('/camera/switch', data)
  30. }
  31. // 获取当前活动通道
  32. export function getCurrentChannel(machineId: string): Promise<IBaseResponse<ChannelDTO>> {
  33. return get('/camera/current', { machineId })
  34. }
  35. // 开始PTZ控制 (后台专用)
  36. export function ptzStart(cameraId: string, action: PTZAction, speed: number = 50): Promise<BaseResponse> {
  37. return post(`/camera/${cameraId}/ptz/start`, undefined, {
  38. params: { action, speed }
  39. })
  40. }
  41. // 停止PTZ控制 (后台专用)
  42. export function ptzStop(cameraId: string): Promise<BaseResponse> {
  43. return post(`/camera/${cameraId}/ptz/stop`)
  44. }
  45. // PTZ 直接控制 (pan/tilt/zoom 方式)
  46. export function ptzDirectControl(cameraId: string, data: PTZControlRequest): Promise<BaseResponse> {
  47. return post(`/camera/${cameraId}/ptz/control`, data)
  48. }
  49. // ==================== Admin APIs ====================
  50. // 获取摄像头列表 (管理后台,分页)
  51. export function adminListCameras(params?: CameraListRequest): Promise<IPageResponse<CameraInfoDTO>> {
  52. return post('/admin/cameras/list', params || {})
  53. }
  54. // 获取摄像头列表 (全部,不分页)
  55. export function adminListAllCameras(machineId?: string): Promise<IListResponse<CameraInfoDTO>> {
  56. return get('/admin/cameras/listAll', machineId ? { machineId } : undefined)
  57. }
  58. // 获取摄像头详情
  59. export function adminGetCamera(id: number): Promise<IBaseResponse<CameraInfoDTO>> {
  60. return get('/admin/cameras/detail', { id })
  61. }
  62. // 添加摄像头
  63. export function adminAddCamera(data: CameraAddRequest): Promise<IBaseResponse<CameraInfoDTO>> {
  64. return post('/admin/cameras/add', data)
  65. }
  66. // 更新摄像头
  67. export function adminUpdateCamera(data: CameraUpdateRequest): Promise<IBaseResponse<CameraInfoDTO>> {
  68. return post('/admin/cameras/update', data)
  69. }
  70. // 删除摄像头
  71. export function adminDeleteCamera(id: number): Promise<BaseResponse> {
  72. return post('/admin/cameras/delete', undefined, {
  73. params: { id }
  74. })
  75. }
  76. // 检测摄像头连通性
  77. export function adminCheckCamera(id: number): Promise<IBaseResponse<boolean>> {
  78. return post('/admin/cameras/check', undefined, {
  79. params: { id }
  80. })
  81. }
  82. // 获取摄像头快照
  83. export function adminGetSnapshot(id: number): Promise<IBaseResponse<Blob>> {
  84. return get('/admin/cameras/snapshot', { id }, { responseType: 'blob' })
  85. }
  86. // PTZ 控制 (Admin)
  87. export function adminPTZControl(data: AdminPTZRequest): Promise<IBaseResponse<boolean>> {
  88. return post('/admin/cameras/ptz', data)
  89. }
  90. // ==================== 兼容旧代码的别名 ====================
  91. // 获取设备列表 (兼容 - 使用不分页接口)
  92. export const listDevice = adminListAllCameras
  93. // 获取设备详情 (兼容)
  94. export const getDevice = (deviceId: string) => getCamera(deviceId)
  95. // 添加设备 (兼容)
  96. export const addDevice = adminAddCamera
  97. // 修改设备 (兼容)
  98. export const updateDevice = adminUpdateCamera
  99. // 删除设备 (兼容)
  100. export const delDevice = (id: number) => adminDeleteCamera(id)
  101. // PTZ控制 (兼容旧API)
  102. export function ptzControl(
  103. _deviceId: string,
  104. channelId: string,
  105. command: string,
  106. horizonSpeed?: number,
  107. _verticalSpeed?: number,
  108. _zoomSpeed?: number
  109. ): Promise<BaseResponse> {
  110. // 映射旧的命令到新的 action
  111. const actionMap: Record<string, PTZAction> = {
  112. up: 'up',
  113. down: 'down',
  114. left: 'left',
  115. right: 'right',
  116. zoomin: 'zoom_in',
  117. zoomout: 'zoom_out',
  118. stop: 'stop'
  119. }
  120. const action = actionMap[command.toLowerCase()] || 'stop'
  121. const speed = horizonSpeed || 50
  122. // 使用 channelId 关联的 cameraId 进行 PTZ 控制
  123. return ptzStart(channelId, action, speed)
  124. }