提交代码
This commit is contained in:
@@ -1,13 +1,19 @@
|
||||
import { onBeforeUnmount, onMounted, ref } from 'vue'
|
||||
|
||||
/** 秒级倒计时,归零时只触发一次回调。 */
|
||||
export function useCountdown(seconds: number, onComplete: () => void) {
|
||||
/** 秒级倒计时,归零时只触发一次回调;enabled 为 false 时不启动计时。 */
|
||||
export function useCountdown(
|
||||
seconds: number,
|
||||
onComplete: () => void,
|
||||
enabled = true,
|
||||
) {
|
||||
const remaining = ref(seconds)
|
||||
|
||||
let timer: number | undefined
|
||||
let finished = false
|
||||
|
||||
onMounted(() => {
|
||||
if (!enabled) return
|
||||
|
||||
timer = window.setInterval(() => {
|
||||
remaining.value = Math.max(0, remaining.value - 1)
|
||||
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
import { onBeforeUnmount, ref } from 'vue'
|
||||
import {
|
||||
HubConnectionBuilder,
|
||||
HubConnectionState,
|
||||
LogLevel,
|
||||
type HubConnection,
|
||||
} from '@microsoft/signalr'
|
||||
import {
|
||||
getStageHubUrl,
|
||||
type ScanProgress,
|
||||
type ScanSegment,
|
||||
} from '@/api/scan'
|
||||
import type { DeviceStatus } from '@/api/device'
|
||||
|
||||
export interface StagePosition {
|
||||
x: number
|
||||
y: number
|
||||
z: number
|
||||
moving: boolean
|
||||
updatedAt: string
|
||||
}
|
||||
|
||||
export interface ScanSpectrum {
|
||||
scanId: string
|
||||
pointIndex: number
|
||||
totalPoints: number
|
||||
wavelengths: number[]
|
||||
spectrum: number[]
|
||||
segments: ScanSegment[]
|
||||
}
|
||||
|
||||
export interface ScanFinished {
|
||||
scanId: string
|
||||
status: string
|
||||
error: string | null
|
||||
}
|
||||
|
||||
export type StageHubState = 'idle' | 'connecting' | 'connected' | 'offline'
|
||||
|
||||
/**
|
||||
* 扫描实时通道(/stageHub)。
|
||||
* 事件契约见 HardwareWeb/Services/StageBroadcastService.cs:
|
||||
* StagePosition -> { x, y, z, moving, updatedAt }
|
||||
* ScanProgress -> ScanProgress
|
||||
* ScanSpectrum -> { scanId, pointIndex, wavelengths, spectrum, segments }
|
||||
* ScanFinished -> { scanId, status, error }
|
||||
* DeviceStatus -> DeviceStatus
|
||||
*/
|
||||
export function useStageHub() {
|
||||
const state = ref<StageHubState>('idle')
|
||||
const position = ref<StagePosition | null>(null)
|
||||
const progress = ref<ScanProgress | null>(null)
|
||||
const spectrum = ref<ScanSpectrum | null>(null)
|
||||
const finished = ref<ScanFinished | null>(null)
|
||||
const deviceStatus = ref<DeviceStatus | null>(null)
|
||||
const errorMessage = ref('')
|
||||
|
||||
let connection: HubConnection | null = null
|
||||
let stoppedByUser = false
|
||||
|
||||
async function connect() {
|
||||
if (connection && connection.state !== HubConnectionState.Disconnected) {
|
||||
return
|
||||
}
|
||||
|
||||
stoppedByUser = false
|
||||
state.value = 'connecting'
|
||||
errorMessage.value = ''
|
||||
|
||||
const hub = new HubConnectionBuilder()
|
||||
.withUrl(getStageHubUrl(), { withCredentials: false })
|
||||
.withAutomaticReconnect({
|
||||
nextRetryDelayInMilliseconds: (context) =>
|
||||
Math.min(1000 * 2 ** context.previousRetryCount, 10000),
|
||||
})
|
||||
.configureLogging(LogLevel.Warning)
|
||||
.build()
|
||||
|
||||
hub.on('StagePosition', (payload: StagePosition) => {
|
||||
position.value = payload
|
||||
})
|
||||
hub.on('ScanProgress', (payload: ScanProgress) => {
|
||||
progress.value = payload
|
||||
})
|
||||
hub.on('ScanSpectrum', (payload: ScanSpectrum) => {
|
||||
spectrum.value = payload
|
||||
})
|
||||
hub.on('ScanFinished', (payload: ScanFinished) => {
|
||||
finished.value = payload
|
||||
})
|
||||
hub.on('DeviceStatus', (payload: DeviceStatus) => {
|
||||
deviceStatus.value = payload
|
||||
})
|
||||
hub.onreconnecting(() => {
|
||||
state.value = 'connecting'
|
||||
})
|
||||
hub.onreconnected(async () => {
|
||||
state.value = 'connected'
|
||||
await syncSnapshot(hub)
|
||||
})
|
||||
hub.onclose(() => {
|
||||
if (stoppedByUser) return
|
||||
state.value = 'offline'
|
||||
})
|
||||
|
||||
connection = hub
|
||||
|
||||
try {
|
||||
await hub.start()
|
||||
state.value = 'connected'
|
||||
await syncSnapshot(hub)
|
||||
} catch (error) {
|
||||
state.value = 'offline'
|
||||
errorMessage.value =
|
||||
error instanceof Error ? error.message : '实时通道连接失败'
|
||||
connection = null
|
||||
await hub.stop().catch(() => undefined)
|
||||
}
|
||||
}
|
||||
|
||||
async function syncSnapshot(hub: HubConnection) {
|
||||
try {
|
||||
const current = await hub.invoke<ScanProgress>('GetScanProgress')
|
||||
progress.value = current
|
||||
} catch {
|
||||
// 快照失败不影响实时事件
|
||||
}
|
||||
|
||||
try {
|
||||
const current = await hub.invoke<StagePosition | null>('GetStagePosition')
|
||||
if (current) position.value = current
|
||||
} catch {
|
||||
// 位移台未连接时返回 null
|
||||
}
|
||||
}
|
||||
|
||||
async function disconnect() {
|
||||
stoppedByUser = true
|
||||
const hub = connection
|
||||
connection = null
|
||||
state.value = 'idle'
|
||||
if (hub) {
|
||||
await hub.stop().catch(() => undefined)
|
||||
}
|
||||
}
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
void disconnect()
|
||||
})
|
||||
|
||||
return {
|
||||
state,
|
||||
position,
|
||||
progress,
|
||||
spectrum,
|
||||
finished,
|
||||
deviceStatus,
|
||||
errorMessage,
|
||||
connect,
|
||||
disconnect,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user