feat: 收敛玩法运行时配置并加入故障恢复
This commit is contained in:
@@ -11,6 +11,12 @@ type RawMockHeartRateMessage = {
|
||||
type?: string
|
||||
timestamp?: number
|
||||
bpm?: number
|
||||
channelId?: string
|
||||
}
|
||||
|
||||
function normalizeMockChannelId(rawChannelId: string | null | undefined): string {
|
||||
const trimmed = String(rawChannelId || '').trim()
|
||||
return trimmed || 'default'
|
||||
}
|
||||
|
||||
function safeParseMessage(data: string): RawMockHeartRateMessage | null {
|
||||
@@ -21,11 +27,15 @@ function safeParseMessage(data: string): RawMockHeartRateMessage | null {
|
||||
}
|
||||
}
|
||||
|
||||
function toHeartRateValue(message: RawMockHeartRateMessage): number | null {
|
||||
function toHeartRateValue(message: RawMockHeartRateMessage, expectedChannelId: string): number | null {
|
||||
if (message.type !== 'mock_heart_rate' || !Number.isFinite(message.bpm)) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (normalizeMockChannelId(message.channelId) !== expectedChannelId) {
|
||||
return null
|
||||
}
|
||||
|
||||
const bpm = Math.round(Number(message.bpm))
|
||||
if (bpm <= 0) {
|
||||
return null
|
||||
@@ -40,6 +50,7 @@ export class MockHeartRateBridge {
|
||||
connected: boolean
|
||||
connecting: boolean
|
||||
url: string
|
||||
channelId: string
|
||||
|
||||
constructor(callbacks: MockHeartRateBridgeCallbacks) {
|
||||
this.callbacks = callbacks
|
||||
@@ -47,6 +58,11 @@ export class MockHeartRateBridge {
|
||||
this.connected = false
|
||||
this.connecting = false
|
||||
this.url = DEFAULT_MOCK_HEART_RATE_BRIDGE_URL
|
||||
this.channelId = 'default'
|
||||
}
|
||||
|
||||
setChannelId(channelId: string): void {
|
||||
this.channelId = normalizeMockChannelId(channelId)
|
||||
}
|
||||
|
||||
connect(url = DEFAULT_MOCK_HEART_RATE_BRIDGE_URL): void {
|
||||
@@ -96,7 +112,7 @@ export class MockHeartRateBridge {
|
||||
return
|
||||
}
|
||||
|
||||
const bpm = toHeartRateValue(parsed)
|
||||
const bpm = toHeartRateValue(parsed, this.channelId)
|
||||
if (bpm === null) {
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user