feat:绑定设备和设备详情
This commit is contained in:
67
src/pages/device/device.utils.ts
Normal file
67
src/pages/device/device.utils.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import type { AppDeviceVo, DeviceListItem, RoomTab } from './device.types'
|
||||
|
||||
/** 位置 ID → 房间名(后端暂无 locationName 时的本地映射) */
|
||||
const LOCATION_NAME_MAP: Record<number, string> = {
|
||||
1: '客厅',
|
||||
2: '主卧',
|
||||
3: '卧室',
|
||||
4: '厨房',
|
||||
5: '卫生间',
|
||||
6: '书房',
|
||||
}
|
||||
|
||||
export function resolveLocationName(locationId?: number): string {
|
||||
if (locationId == null) return '未分配'
|
||||
return LOCATION_NAME_MAP[locationId] ?? `位置${locationId}`
|
||||
}
|
||||
|
||||
export function mapDeviceItem(device: AppDeviceVo): DeviceListItem {
|
||||
return {
|
||||
...device,
|
||||
locationName: resolveLocationName(device.locationId),
|
||||
isOnline: device.status === '1',
|
||||
isCharging: device.charging === '1',
|
||||
}
|
||||
}
|
||||
|
||||
export function buildRoomTabs(
|
||||
devices: DeviceListItem[],
|
||||
customRooms: RoomTab[] = [],
|
||||
): RoomTab[] {
|
||||
const tabs: RoomTab[] = [{ id: 'all', name: '全部' }]
|
||||
const seen = new Set<string>()
|
||||
|
||||
for (const device of devices) {
|
||||
if (device.locationId == null) continue
|
||||
const id = String(device.locationId)
|
||||
if (seen.has(id)) continue
|
||||
seen.add(id)
|
||||
tabs.push({
|
||||
id,
|
||||
name: device.locationName,
|
||||
locationId: device.locationId,
|
||||
})
|
||||
}
|
||||
|
||||
for (const room of customRooms) {
|
||||
if (seen.has(room.id)) continue
|
||||
seen.add(room.id)
|
||||
tabs.push(room)
|
||||
}
|
||||
|
||||
return tabs
|
||||
}
|
||||
|
||||
export function filterDevicesByRoom(
|
||||
devices: DeviceListItem[],
|
||||
activeRoomId: string,
|
||||
): DeviceListItem[] {
|
||||
if (activeRoomId === 'all') return devices
|
||||
const locationId = Number(activeRoomId)
|
||||
if (Number.isNaN(locationId)) return devices
|
||||
return devices.filter((item) => item.locationId === locationId)
|
||||
}
|
||||
|
||||
export function countOnlineDevices(devices: DeviceListItem[]): number {
|
||||
return devices.filter((item) => item.isOnline).length
|
||||
}
|
||||
Reference in New Issue
Block a user