56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
import type { AppDeviceVo, DeviceListItem, RoomTab } from './device.types'
|
||
import type { RoomOption } from '@/types/location'
|
||
import { sameId } from '@/utils/device-id'
|
||
|
||
export type LocationMap = Map<string, RoomOption[]>
|
||
|
||
function resolveLocationName(
|
||
locationId?: string | number,
|
||
locations: RoomOption[] = [],
|
||
): string {
|
||
if (locationId == null) return '未分配'
|
||
const matched = locations.find((item) => sameId(item.locationId, locationId))
|
||
return matched?.name ?? '未分配'
|
||
}
|
||
|
||
export function mapDeviceItem(
|
||
device: AppDeviceVo,
|
||
locationMap: LocationMap,
|
||
): DeviceListItem {
|
||
const homeKey = device.homeId != null ? String(device.homeId) : ''
|
||
const locations = homeKey ? locationMap.get(homeKey) ?? [] : []
|
||
return {
|
||
...device,
|
||
locationName: resolveLocationName(device.locationId, locations),
|
||
isOnline: device.status === '1',
|
||
isCharging: device.charging === '1',
|
||
}
|
||
}
|
||
|
||
/** 根据家庭房间位置列表构建首页 Tab(与所在房间页数据源一致) */
|
||
export function buildRoomTabs(
|
||
locations: { locationId: string | number; name: string }[],
|
||
): RoomTab[] {
|
||
const tabs: RoomTab[] = [{ id: 'all', name: '全部' }]
|
||
for (const location of locations) {
|
||
tabs.push({
|
||
id: String(location.locationId),
|
||
name: location.name,
|
||
locationId: location.locationId,
|
||
})
|
||
}
|
||
return tabs
|
||
}
|
||
|
||
export function filterDevicesByRoom(
|
||
devices: DeviceListItem[],
|
||
activeRoomId: string,
|
||
): DeviceListItem[] {
|
||
if (activeRoomId === 'all') return devices
|
||
return devices.filter((item) => sameId(item.locationId, activeRoomId))
|
||
}
|
||
|
||
export function countOnlineDevices(devices: DeviceListItem[]): number {
|
||
return devices.filter((item) => item.isOnline).length
|
||
}
|