95 lines
3.1 KiB
TypeScript
95 lines
3.1 KiB
TypeScript
import { DeviceType } from '@/enums/device-type';
|
|
import { useQueryControlStore } from '@/stores/query-control';
|
|
import { useStationStore } from '@/stores/station';
|
|
import { isCancelledError, useMutation, useQuery } from '@tanstack/vue-query';
|
|
import { storeToRefs } from 'pinia';
|
|
import { computed } from 'vue';
|
|
import type { StationDevices } from './domains';
|
|
import { useLineDevicesStore } from '@/stores/line-devices';
|
|
import { LINE_DEVICES_QUERY_KEY } from '@/constants';
|
|
import { ndmClient } from '@/apis/client';
|
|
import { sleepFrame } from '@/utils/sleep';
|
|
import type { Station } from '@/apis/domains';
|
|
import { runTask } from '@/utils/run-task';
|
|
|
|
const createEmptyStationDevices = (): StationDevices => {
|
|
return {
|
|
[DeviceType.Camera]: [],
|
|
[DeviceType.Decoder]: [],
|
|
[DeviceType.Keyboard]: [],
|
|
[DeviceType.MediaServer]: [],
|
|
[DeviceType.Nvr]: [],
|
|
[DeviceType.SecurityBox]: [],
|
|
[DeviceType.Switch]: [],
|
|
[DeviceType.VideoServer]: [],
|
|
};
|
|
};
|
|
|
|
const getNdmDevicesAll = async (stationCode: string, signal?: AbortSignal) => {
|
|
const resp = await ndmClient.get<StationDevices>(`/${stationCode}/api/ndm/ndmDevices/all`, { retRaw: true, signal });
|
|
const [err, data] = resp;
|
|
if (err || !data) {
|
|
throw err;
|
|
}
|
|
return data;
|
|
};
|
|
|
|
export function useLineDevicesQuery() {
|
|
const stationStore = useStationStore();
|
|
const { stationList } = storeToRefs(stationStore);
|
|
const queryControlStore = useQueryControlStore();
|
|
const { deviceQueryStamp } = storeToRefs(queryControlStore);
|
|
const { mutateAsync: getStationDevices } = useStationDevicesMutation();
|
|
|
|
return useQuery({
|
|
queryKey: [LINE_DEVICES_QUERY_KEY, deviceQueryStamp],
|
|
enabled: computed(() => deviceQueryStamp.value > 0),
|
|
staleTime: Infinity,
|
|
refetchOnMount: false,
|
|
refetchOnReconnect: false,
|
|
refetchOnWindowFocus: false,
|
|
queryFn: async ({ signal }) => {
|
|
console.time('useLineDevicesQuery');
|
|
for (const station of stationList.value) {
|
|
await getStationDevices({ station, signal });
|
|
}
|
|
console.timeEnd('useLineDevicesQuery');
|
|
return null;
|
|
},
|
|
});
|
|
}
|
|
|
|
interface StationDevicesMutationParams {
|
|
station: Station;
|
|
signal?: AbortSignal;
|
|
}
|
|
|
|
function useStationDevicesMutation() {
|
|
const lineDevicesStore = useLineDevicesStore();
|
|
const { lineDevices } = storeToRefs(lineDevicesStore);
|
|
|
|
return useMutation<StationDevices, Error, StationDevicesMutationParams>({
|
|
mutationFn: async ({ station, signal }) => {
|
|
if (!station.online) {
|
|
return createEmptyStationDevices();
|
|
}
|
|
return await getNdmDevicesAll(station.code, signal);
|
|
},
|
|
onSuccess: (stationDevices, { station }) => {
|
|
// TODO: 优化性能,避免阻塞主线程(待测试)
|
|
// lineDevices.value[station.code] = stationDevices;
|
|
// await sleepFrame();
|
|
runTask(() => {
|
|
lineDevices.value[station.code] = stationDevices;
|
|
});
|
|
},
|
|
onError: (error, { station }) => {
|
|
if (!isCancelledError(error)) {
|
|
console.error(`获取车站 ${station.name} 设备数据失败:`, error);
|
|
lineDevices.value[station.code] = createEmptyStationDevices();
|
|
throw error;
|
|
}
|
|
},
|
|
});
|
|
}
|