fix:最新
This commit is contained in:
@@ -1,14 +1,22 @@
|
||||
import { buildBleDeviceName } from '@/utils/ble-device-name'
|
||||
import {
|
||||
findDeviceInBleScan,
|
||||
isBluetoothApiSupported,
|
||||
scanBluetoothDevices,
|
||||
} from '@/utils/bluetooth'
|
||||
import { sameId } from '@/utils/device-id'
|
||||
import {
|
||||
recordPresenceHit,
|
||||
recordPresenceMiss,
|
||||
shouldMarkDeviceOffline,
|
||||
} from './device-presence-state'
|
||||
import { updateDeviceRuntime } from './detail/device-detail.api'
|
||||
import type { DeviceListItem } from './device.types'
|
||||
|
||||
let syncing = false
|
||||
|
||||
const PRESENCE_SCAN_MS = 8000
|
||||
|
||||
const PRESENCE_DEBUG = import.meta.env.DEV
|
||||
|| import.meta.env.VITE_BLE_DEBUG === 'true'
|
||||
|
||||
@@ -23,27 +31,57 @@ function logPresence(message: string, payload?: unknown) {
|
||||
|
||||
function applyPresence(
|
||||
device: DeviceListItem,
|
||||
present: boolean,
|
||||
blePresent: boolean,
|
||||
): DeviceListItem {
|
||||
const nextStatus = present ? '1' : '2'
|
||||
const nextBluetooth = present ? '1' : '0'
|
||||
if (!device.deviceId) return device
|
||||
|
||||
if (blePresent) {
|
||||
recordPresenceHit(device.deviceId)
|
||||
return {
|
||||
...device,
|
||||
status: '1',
|
||||
bluetoothConnected: '1',
|
||||
isOnline: true,
|
||||
}
|
||||
}
|
||||
|
||||
recordPresenceMiss(device.deviceId)
|
||||
if (!shouldMarkDeviceOffline(device.deviceId, device.isOnline)) {
|
||||
return device
|
||||
}
|
||||
|
||||
return {
|
||||
...device,
|
||||
status: nextStatus,
|
||||
bluetoothConnected: nextBluetooth,
|
||||
isOnline: present,
|
||||
status: '2',
|
||||
bluetoothConnected: '0',
|
||||
isOnline: false,
|
||||
}
|
||||
}
|
||||
|
||||
/** 通过 BLE 扫描刷新设备在线状态(status: 1在线 / 2离线) */
|
||||
/** 列表页仅上报「确认在线」,不因单次扫描未命中把云端写成离线 */
|
||||
async function reportPresenceOnlineToCloud(device: DeviceListItem) {
|
||||
if (!device.deviceId) return
|
||||
if (device.status === '1' && device.bluetoothConnected === '1') return
|
||||
try {
|
||||
await updateDeviceRuntime({
|
||||
deviceId: device.deviceId,
|
||||
status: '1',
|
||||
bluetoothConnected: '1',
|
||||
})
|
||||
} catch (err) {
|
||||
logPresence('updateRuntime failed', err)
|
||||
}
|
||||
}
|
||||
|
||||
/** 通过 BLE 扫描刷新设备在线状态(仅 MAC 精确匹配,不用缓存广播) */
|
||||
export async function refreshDevicePresence(
|
||||
devices: DeviceListItem[],
|
||||
): Promise<DeviceListItem[]> {
|
||||
if (!isBluetoothApiSupported()) return devices
|
||||
|
||||
const targets = devices.filter((item) => item.deviceId && (item.mac || item.name))
|
||||
const targets = devices.filter((item) => item.deviceId && item.mac)
|
||||
if (!targets.length) {
|
||||
logPresence('skip: no mac/name on devices')
|
||||
logPresence('skip: no mac on devices')
|
||||
return devices
|
||||
}
|
||||
|
||||
@@ -51,7 +89,7 @@ export async function refreshDevicePresence(
|
||||
syncing = true
|
||||
|
||||
try {
|
||||
const scanned = await scanBluetoothDevices({ timeoutMs: 5000 })
|
||||
const scanned = await scanBluetoothDevices({ timeoutMs: PRESENCE_SCAN_MS })
|
||||
logPresence('scan result', {
|
||||
count: scanned.length,
|
||||
names: scanned.map((item) => item.name || item.localName || item.deviceId),
|
||||
@@ -62,42 +100,27 @@ export async function refreshDevicePresence(
|
||||
const isTarget = targets.some((item) => sameId(item.deviceId, device.deviceId))
|
||||
if (!isTarget) return device
|
||||
|
||||
const found = findDeviceInBleScan(
|
||||
{ mac: device.mac, name: device.name },
|
||||
scanned,
|
||||
{ strictName: true },
|
||||
)
|
||||
const found = device.mac
|
||||
? findDeviceInBleScan({ mac: device.mac }, scanned, { strictName: true })
|
||||
: null
|
||||
const present = !!found
|
||||
logPresence('device presence', {
|
||||
name: device.name,
|
||||
mac: device.mac,
|
||||
present,
|
||||
foundId: found?.deviceId,
|
||||
bleName: device.mac ? buildBleDeviceName(device.mac) : '',
|
||||
})
|
||||
return applyPresence(device, present)
|
||||
})
|
||||
|
||||
void Promise.allSettled(
|
||||
targets.map(async (device) => {
|
||||
const found = findDeviceInBleScan(
|
||||
{ mac: device.mac, name: device.name },
|
||||
scanned,
|
||||
{ strictName: true },
|
||||
)
|
||||
const present = !!found
|
||||
const nextStatus = present ? '1' : '2'
|
||||
const nextBluetooth = present ? '1' : '0'
|
||||
if (device.status === nextStatus && device.bluetoothConnected === nextBluetooth) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
await updateDeviceRuntime({
|
||||
deviceId: device.deviceId!,
|
||||
status: nextStatus,
|
||||
bluetoothConnected: nextBluetooth,
|
||||
})
|
||||
} catch (err) {
|
||||
logPresence('updateRuntime failed', err)
|
||||
const found = device.mac
|
||||
? findDeviceInBleScan({ mac: device.mac }, scanned, { strictName: true })
|
||||
: null
|
||||
if (found) {
|
||||
await reportPresenceOnlineToCloud(device)
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user