Files
ndm-web-client/src/composables/query/device/use-line-devices-query.ts

96 lines
3.8 KiB
TypeScript

import type { Station } from '@/apis/domains';
import type { PageParams } from '@/apis/models';
import { postNdmCameraPage, postNdmDecoderPage, postNdmKeyboardPage, postNdmMediaServerPage, postNdmNvrPage, postNdmSecurityBoxPage, postNdmSwitchPage, postNdmVideoServerPage } from '@/apis/requests';
import { DeviceType } from '@/enums/device-type';
import { useQueryControlStore } from '@/stores/query-control';
import { useStationStore } from '@/stores/station';
import { useQuery } from '@tanstack/vue-query';
import { storeToRefs } from 'pinia';
import { computed } from 'vue';
import type { StationDevices } from './domains';
export interface LineDevices {
[stationCode: Station['code']]: StationDevices;
}
const createEmptyStationDevices = (): StationDevices => {
return {
[DeviceType.Camera]: [],
[DeviceType.Decoder]: [],
[DeviceType.Keyboard]: [],
[DeviceType.MediaServer]: [],
[DeviceType.Nvr]: [],
[DeviceType.SecurityBox]: [],
[DeviceType.Switch]: [],
[DeviceType.VideoServer]: [],
};
};
export function useLineDevicesQuery() {
const stationStore = useStationStore();
const { updatedTime, stationList, onlineStationList } = storeToRefs(stationStore);
const queryControlStore = useQueryControlStore();
const { pollingEnabled } = storeToRefs(queryControlStore);
return useQuery({
queryKey: ['line-devices', updatedTime],
enabled: computed(() => onlineStationList.value.length > 0 && pollingEnabled.value),
queryFn: async ({ signal }): Promise<LineDevices> => {
// console.time('useLineDevicesQuery');
const pageQuery: PageParams<{}> = { model: {}, extra: {}, size: 5000, current: 1, sort: 'id', order: 'ascending' };
const lineDevices: LineDevices = {};
// 如果没有车站列表,返回空对象
if (!stationList?.value) {
return lineDevices;
}
// 遍历所有车站
for (const station of stationList.value) {
// 如果车站离线,设置空数组
if (!station.online) {
lineDevices[station.code] = createEmptyStationDevices();
continue;
}
try {
// 并行获取该车站的所有设备类型数据
const [cameraData, decoderData, keyboardData, mediaServerData, nvrData, securityBoxData, switchData, videoServerData] = await Promise.all([
postNdmCameraPage(station.code, pageQuery, signal),
postNdmDecoderPage(station.code, pageQuery, signal),
postNdmKeyboardPage(station.code, pageQuery, signal),
postNdmMediaServerPage(station.code, pageQuery, signal),
postNdmNvrPage(station.code, pageQuery, signal),
postNdmSecurityBoxPage(station.code, pageQuery, signal),
postNdmSwitchPage(station.code, pageQuery, signal),
postNdmVideoServerPage(station.code, pageQuery, signal),
]);
// 存储该车站的设备数据
lineDevices[station.code] = {
[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] = createEmptyStationDevices();
}
}
// console.timeEnd('useLineDevicesQuery');
return lineDevices;
},
placeholderData: (prev) => prev,
});
}