添加项目文件。

This commit is contained in:
ldh
2026-11-13 09:51:38 +08:00
parent 35573a85eb
commit abb5d5d32f
123 changed files with 9453 additions and 0 deletions
+78
View File
@@ -0,0 +1,78 @@
/**
* USB 相机接口封装。
* 后端实现见 HardwareWeb/Controllers/CameraController.cs:
* GET /api/camera/status -> { connected: boolean }
* POST /api/camera/connect -> { connected: boolean }
* POST /api/camera/disconnect -> { connected: boolean }
*/
export interface CameraStatus {
connected: boolean
}
const API_BASE = (import.meta.env.VITE_API_BASE ?? '').replace(/\/$/, '')
const REQUEST_TIMEOUT = 5000
export class CameraApiError extends Error {
readonly status: number
constructor(message: string, status = 0) {
super(message)
this.name = 'CameraApiError'
this.status = status
}
}
async function request<T>(path: string, init?: RequestInit): Promise<T> {
const controller = new AbortController()
const timer = window.setTimeout(() => controller.abort(), REQUEST_TIMEOUT)
try {
const response = await fetch(`${API_BASE}${path}`, {
...init,
cache: 'no-store',
signal: controller.signal,
headers: {
Accept: 'application/json',
...(init?.headers ?? {}),
},
})
if (!response.ok) {
let detail = `HTTP ${response.status}`
try {
const body = (await response.json()) as { message?: string; error?: string }
detail = body.message ?? body.error ?? detail
} catch {
// 响应不是 JSON 时保留状态码信息
}
throw new CameraApiError(detail, response.status)
}
return (await response.json()) as T
} catch (error) {
if (error instanceof CameraApiError) throw error
if (error instanceof DOMException && error.name === 'AbortError') {
throw new CameraApiError('相机服务响应超时')
}
throw new CameraApiError(error instanceof Error ? error.message : '无法连接相机服务')
} finally {
window.clearTimeout(timer)
}
}
export function getCameraStatus(): Promise<CameraStatus> {
return request<CameraStatus>('/api/camera/status')
}
export function connectCamera(): Promise<CameraStatus> {
return request<CameraStatus>('/api/camera/connect', { method: 'POST' })
}
export function disconnectCamera(): Promise<CameraStatus> {
return request<CameraStatus>('/api/camera/disconnect', { method: 'POST' })
}
export function getCameraHubUrl(): string {
return `${API_BASE}/cameraHub`
}