105 lines
2.9 KiB
TypeScript
105 lines
2.9 KiB
TypeScript
/**
|
||
* 靶样轮廓识别与采样点规划接口封装。
|
||
* 后端实现见 HardwareWeb/Controllers/SampleAnalysisController.cs:
|
||
* POST /api/sample/baseline -> { width, height, capturedAt }
|
||
* POST /api/sample/target -> { width, height, capturedAt }
|
||
* POST /api/sample/analyze -> { imageBase64, mimeType, width, height, samplingPointCount, samplingPoints }
|
||
*/
|
||
|
||
export interface SampleFrameResult {
|
||
width: number
|
||
height: number
|
||
capturedAt: string
|
||
}
|
||
|
||
export interface SamplePoint {
|
||
x: number
|
||
y: number
|
||
}
|
||
|
||
export interface SampleAnalysisResult {
|
||
imageBase64: string
|
||
mimeType: string
|
||
width: number
|
||
height: number
|
||
samplingPointCount: number
|
||
samplingPoints: SamplePoint[]
|
||
}
|
||
|
||
const API_BASE = (import.meta.env.VITE_API_BASE ?? '').replace(/\/$/, '')
|
||
const DEFAULT_TIMEOUT = 15000
|
||
const ANALYZE_TIMEOUT = 30000
|
||
|
||
export class SampleApiError extends Error {
|
||
readonly status: number
|
||
|
||
constructor(message: string, status = 0) {
|
||
super(message)
|
||
this.name = 'SampleApiError'
|
||
this.status = status
|
||
}
|
||
}
|
||
|
||
async function request<T>(
|
||
path: string,
|
||
init?: RequestInit,
|
||
timeout = DEFAULT_TIMEOUT,
|
||
): Promise<T> {
|
||
const controller = new AbortController()
|
||
const timer = window.setTimeout(() => controller.abort(), 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 SampleApiError(detail, response.status)
|
||
}
|
||
|
||
return (await response.json()) as T
|
||
} catch (error) {
|
||
if (error instanceof SampleApiError) throw error
|
||
if (error instanceof DOMException && error.name === 'AbortError') {
|
||
throw new SampleApiError('图像分析服务响应超时')
|
||
}
|
||
throw new SampleApiError(
|
||
error instanceof Error ? error.message : '无法连接图像分析服务',
|
||
)
|
||
} finally {
|
||
window.clearTimeout(timer)
|
||
}
|
||
}
|
||
|
||
/** 相机画面稳定后采集基准画面。 */
|
||
export function captureSampleBaseline(): Promise<SampleFrameResult> {
|
||
return request<SampleFrameResult>('/api/sample/baseline', { method: 'POST' })
|
||
}
|
||
|
||
/** 放置靶样并点击开始检测时采集第二张画面。 */
|
||
export function captureSampleTarget(): Promise<SampleFrameResult> {
|
||
return request<SampleFrameResult>('/api/sample/target', { method: 'POST' })
|
||
}
|
||
|
||
/** 提取靶样轮廓、绘制采样点并返回处理后的图像。 */
|
||
export function analyzeSample(): Promise<SampleAnalysisResult> {
|
||
return request<SampleAnalysisResult>(
|
||
'/api/sample/analyze',
|
||
{ method: 'POST' },
|
||
ANALYZE_TIMEOUT,
|
||
)
|
||
}
|