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' function logPresence(message: string, payload?: unknown) { if (!PRESENCE_DEBUG) return if (payload !== undefined) { console.log(`[Device Presence] ${message}`, payload) return } console.log(`[Device Presence] ${message}`) } function applyPresence( device: DeviceListItem, blePresent: boolean, ): DeviceListItem { 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: '2', bluetoothConnected: '0', isOnline: false, } } /** 列表页仅上报「确认在线」,不因单次扫描未命中把云端写成离线 */ 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 { if (!isBluetoothApiSupported()) return devices const targets = devices.filter((item) => item.deviceId && item.mac) if (!targets.length) { logPresence('skip: no mac on devices') return devices } if (syncing) return devices syncing = true try { const scanned = await scanBluetoothDevices({ timeoutMs: PRESENCE_SCAN_MS }) logPresence('scan result', { count: scanned.length, names: scanned.map((item) => item.name || item.localName || item.deviceId), }) const nextDevices = devices.map((device) => { if (!device.deviceId) return device const isTarget = targets.some((item) => sameId(item.deviceId, device.deviceId)) if (!isTarget) return device 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 = device.mac ? findDeviceInBleScan({ mac: device.mac }, scanned, { strictName: true }) : null if (found) { await reportPresenceOnlineToCloud(device) } }), ) return nextDevices } catch (err) { logPresence('scan failed', err) return devices } finally { syncing = false } }