fix:bug解决

This commit is contained in:
李伊帆
2026-10-28 10:36:39 +08:00
parent c369127166
commit 91bca314ba
57 changed files with 2839 additions and 384 deletions

View File

@@ -6,9 +6,11 @@ import {
} from '@/utils/bluetooth'
import { sameId } from '@/utils/device-id'
import {
markPresenceOfflineReportedToCloud,
recordPresenceHit,
recordPresenceMiss,
shouldMarkDeviceOffline,
shouldReportPresenceOfflineToCloud,
} from './device-presence-state'
import { updateDeviceRuntime } from './detail/device-detail.api'
import type { DeviceListItem } from './device.types'
@@ -29,6 +31,10 @@ function logPresence(message: string, payload?: unknown) {
console.log(`[Device Presence] ${message}`)
}
function wasDeviceOnlineInCloud(device: DeviceListItem): boolean {
return device.status === '1' && device.bluetoothConnected === '1'
}
function applyPresence(
device: DeviceListItem,
blePresent: boolean,
@@ -58,7 +64,6 @@ function applyPresence(
}
}
/** 列表页仅上报「确认在线」,不因单次扫描未命中把云端写成离线 */
async function reportPresenceOnlineToCloud(device: DeviceListItem) {
if (!device.deviceId) return
if (device.status === '1' && device.bluetoothConnected === '1') return
@@ -69,10 +74,35 @@ async function reportPresenceOnlineToCloud(device: DeviceListItem) {
bluetoothConnected: '1',
})
} catch (err) {
logPresence('updateRuntime failed', err)
logPresence('online updateRuntime failed', err)
}
}
async function reportPresenceOfflineToCloud(device: DeviceListItem) {
if (!device.deviceId) return
if (!shouldReportPresenceOfflineToCloud(device.deviceId)) return
try {
await updateDeviceRuntime({
deviceId: device.deviceId,
status: '2',
bluetoothConnected: '0',
signalLevel: '0',
})
markPresenceOfflineReportedToCloud(device.deviceId)
logPresence('reported offline to cloud', { deviceId: device.deviceId })
} catch (err) {
logPresence('offline updateRuntime failed', err)
}
}
function shouldSyncOfflineToCloud(before: DeviceListItem, after: DeviceListItem): boolean {
if (!after.deviceId) return false
if (after.status !== '2' || after.bluetoothConnected !== '0') return false
if (!after.isOnline && before.isOnline) return true
if (wasDeviceOnlineInCloud(before) && !wasDeviceOnlineInCloud(after)) return true
return false
}
/** 通过 BLE 扫描刷新设备在线状态(仅 MAC 精确匹配,不用缓存广播) */
export async function refreshDevicePresence(
devices: DeviceListItem[],
@@ -95,6 +125,8 @@ export async function refreshDevicePresence(
names: scanned.map((item) => item.name || item.localName || item.deviceId),
})
const offlineReports: DeviceListItem[] = []
const nextDevices = devices.map((device) => {
if (!device.deviceId) return device
const isTarget = targets.some((item) => sameId(item.deviceId, device.deviceId))
@@ -111,7 +143,11 @@ export async function refreshDevicePresence(
foundId: found?.deviceId,
bleName: device.mac ? buildBleDeviceName(device.mac) : '',
})
return applyPresence(device, present)
const next = applyPresence(device, present)
if (shouldSyncOfflineToCloud(device, next)) {
offlineReports.push(next)
}
return next
})
void Promise.allSettled(
@@ -125,6 +161,8 @@ export async function refreshDevicePresence(
}),
)
void Promise.allSettled(offlineReports.map((device) => reportPresenceOfflineToCloud(device)))
return nextDevices
} catch (err) {
logPresence('scan failed', err)