import { useQuery } from '@tanstack/vue-query'; import type { PageParams } from '@/apis/models/base/page'; import { postNdmCameraPage, postNdmDecoderPage, postNdmKeyboardPage, postNdmMediaServerPage, postNdmNvrPage, postNdmSecurityBoxPage, postNdmSwitchPage, postNdmVideoServerPage, } from '@/apis/requests/device'; import { useStationStore } from '@/stores/station'; import { storeToRefs } from 'pinia'; import { computed } from 'vue'; import { DeviceType } from '@/enums/device-type'; import type { NdmCameraResultVO, NdmDecoderResultVO, NdmKeyboardResultVO, NdmMediaServerResultVO, NdmNvrResultVO, NdmSecurityBoxResultVO, NdmSwitchResultVO, NdmVideoServerResultVO, } from '@/apis/models/device'; // 定义设备数据类型 interface StationDevices { // ndmCameraList: any[]; // ndmDecoderList: any[]; // ndmKeyboardList: any[]; // ndmMedisServerList: any[]; // ndmNvrList: any[]; // ndmSecurityBoxList: any[]; // ndmSwitchList: any[]; // ndmVideoServerList: any[]; [DeviceType.Camera]: NdmCameraResultVO[]; [DeviceType.Decoder]: NdmDecoderResultVO[]; [DeviceType.Keyboard]: NdmKeyboardResultVO[]; [DeviceType.MediaServer]: NdmMediaServerResultVO[]; [DeviceType.Nvr]: NdmNvrResultVO[]; [DeviceType.SecurityBox]: NdmSecurityBoxResultVO[]; [DeviceType.Switch]: NdmSwitchResultVO[]; [DeviceType.VideoServer]: NdmVideoServerResultVO[]; } export function useLineDevicesQuery() { const stationStore = useStationStore(); const { updatedTime, stationList, onlineStationList } = storeToRefs(stationStore); return useQuery({ queryKey: ['line-devices', updatedTime], enabled: computed(() => onlineStationList.value.length > 0), placeholderData: (prev) => prev, queryFn: async (): Promise> => { const pageQuery: PageParams<{}> = { model: {}, extra: {}, size: 5000, current: 1, sort: 'id', order: 'ascending' }; const lineDevices: Record = {}; // 如果没有车站列表,返回空对象 if (!stationList?.value) { return lineDevices; } // 遍历所有车站 for (const station of stationList.value) { // 如果车站离线,设置空数组 if (!station.online) { lineDevices[station.code] = { // ndmCameraList: [], // ndmDecoderList: [], // ndmKeyboardList: [], // ndmMedisServerList: [], // ndmNvrList: [], // ndmSecurityBoxList: [], // ndmSwitchList: [], // ndmVideoServerList: [], [DeviceType.Camera]: [], [DeviceType.Decoder]: [], [DeviceType.Keyboard]: [], [DeviceType.MediaServer]: [], [DeviceType.Nvr]: [], [DeviceType.SecurityBox]: [], [DeviceType.Switch]: [], [DeviceType.VideoServer]: [], }; continue; } try { // 并行获取该车站的所有设备类型数据 const [cameraData, decoderData, keyboardData, mediaServerData, nvrData, securityBoxData, switchData, videoServerData] = await Promise.all([ postNdmCameraPage(station.code, pageQuery), postNdmDecoderPage(station.code, pageQuery), postNdmKeyboardPage(station.code, pageQuery), postNdmMediaServerPage(station.code, pageQuery), postNdmNvrPage(station.code, pageQuery), postNdmSecurityBoxPage(station.code, pageQuery), postNdmSwitchPage(station.code, pageQuery), postNdmVideoServerPage(station.code, pageQuery), ]); // 存储该车站的设备数据 lineDevices[station.code] = { // ndmCameraList: cameraData.records ?? [], // ndmDecoderList: decoderData.records ?? [], // ndmKeyboardList: keyboardData.records ?? [], // ndmMedisServerList: mediaServerData.records ?? [], // ndmNvrList: nvrData.records ?? [], // ndmSecurityBoxList: securityBoxData.records ?? [], // ndmSwitchList: switchData.records ?? [], // ndmVideoServerList: videoServerData.records ?? [], [DeviceType.Camera]: cameraData.records ?? [], [DeviceType.Decoder]: decoderData.records ?? [], [DeviceType.Keyboard]: keyboardData.records ?? [], [DeviceType.MediaServer]: mediaServerData.records ?? [], [DeviceType.Nvr]: nvrData.records ?? [], [DeviceType.SecurityBox]: securityBoxData.records ?? [], [DeviceType.Switch]: switchData.records ?? [], [DeviceType.VideoServer]: videoServerData.records ?? [], }; } catch (error) { console.error(`获取车站 ${station.name} 设备数据失败:`, error); // 如果获取失败,设置空数组 lineDevices[station.code] = { // ndmCameraList: [], // ndmDecoderList: [], // ndmKeyboardList: [], // ndmMedisServerList: [], // ndmNvrList: [], // ndmSecurityBoxList: [], // ndmSwitchList: [], // ndmVideoServerList: [], [DeviceType.Camera]: [], [DeviceType.Decoder]: [], [DeviceType.Keyboard]: [], [DeviceType.MediaServer]: [], [DeviceType.Nvr]: [], [DeviceType.SecurityBox]: [], [DeviceType.Switch]: [], [DeviceType.VideoServer]: [], }; } } return lineDevices; }, }); }