refactor:
- extend NdmDeviceAlarmLogVO - only query alarm counts - separate request and store update in useQuery - refactor station card and alarm modal, data fetching is now inside modal - optimize device tree - optimize query station list - make export size follow page size - fix query sequence and make them follow stations -> devices -> alarms
This commit is contained in:
@@ -1 +1,34 @@
|
||||
export * from './station-alarms';
|
||||
import type { NdmDeviceAlarmLogResultVO } from '@/apis/models';
|
||||
import type { DeviceType } from '@/enums/device-type';
|
||||
|
||||
export interface StationAlarms {
|
||||
[DeviceType.Camera]: NdmDeviceAlarmLogResultVO[];
|
||||
[DeviceType.Decoder]: NdmDeviceAlarmLogResultVO[];
|
||||
[DeviceType.Keyboard]: NdmDeviceAlarmLogResultVO[];
|
||||
[DeviceType.MediaServer]: NdmDeviceAlarmLogResultVO[];
|
||||
[DeviceType.Nvr]: NdmDeviceAlarmLogResultVO[];
|
||||
[DeviceType.SecurityBox]: NdmDeviceAlarmLogResultVO[];
|
||||
[DeviceType.Switch]: NdmDeviceAlarmLogResultVO[];
|
||||
[DeviceType.VideoServer]: NdmDeviceAlarmLogResultVO[];
|
||||
unclassified: NdmDeviceAlarmLogResultVO[];
|
||||
}
|
||||
|
||||
export interface LineAlarms {
|
||||
[stationCode: string]: StationAlarms;
|
||||
}
|
||||
|
||||
export interface StationAlarmCounts {
|
||||
[DeviceType.Camera]: number;
|
||||
[DeviceType.Decoder]: number;
|
||||
[DeviceType.Keyboard]: number;
|
||||
[DeviceType.MediaServer]: number;
|
||||
[DeviceType.Nvr]: number;
|
||||
[DeviceType.SecurityBox]: number;
|
||||
[DeviceType.Switch]: number;
|
||||
[DeviceType.VideoServer]: number;
|
||||
unclassified: number;
|
||||
}
|
||||
|
||||
export interface LineAlarmCounts {
|
||||
[stationCode: string]: StationAlarmCounts;
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
import type { NdmDeviceAlarmLogResultVO } from '@/apis/models';
|
||||
import type { DeviceType } from '@/enums/device-type';
|
||||
|
||||
export interface StationAlarms {
|
||||
[DeviceType.Camera]: NdmDeviceAlarmLogResultVO[];
|
||||
[DeviceType.Decoder]: NdmDeviceAlarmLogResultVO[];
|
||||
[DeviceType.Keyboard]: NdmDeviceAlarmLogResultVO[];
|
||||
[DeviceType.MediaServer]: NdmDeviceAlarmLogResultVO[];
|
||||
[DeviceType.Nvr]: NdmDeviceAlarmLogResultVO[];
|
||||
[DeviceType.SecurityBox]: NdmDeviceAlarmLogResultVO[];
|
||||
[DeviceType.Switch]: NdmDeviceAlarmLogResultVO[];
|
||||
[DeviceType.VideoServer]: NdmDeviceAlarmLogResultVO[];
|
||||
unclassified: NdmDeviceAlarmLogResultVO[];
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
export * from './domains';
|
||||
|
||||
export * from './use-line-alarms-query';
|
||||
export * from './use-line-alarm-counts-query';
|
||||
|
||||
104
src/composables/query/alarm/use-line-alarm-counts-query.ts
Normal file
104
src/composables/query/alarm/use-line-alarm-counts-query.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
import { LINE_ALARM_COUNTS_QUERY_KEY } from '@/constants';
|
||||
import { useLineAlarmCountsStore } from '@/stores/line-alarm-counts';
|
||||
import { useQueryControlStore } from '@/stores/query-control';
|
||||
import { useStationStore } from '@/stores/station';
|
||||
import { useMutation, useQuery } from '@tanstack/vue-query';
|
||||
import { storeToRefs } from 'pinia';
|
||||
import { computed } from 'vue';
|
||||
import dayjs from 'dayjs';
|
||||
import { postNdmDeviceAlarmLogPage } from '@/apis/requests';
|
||||
import { sleep } from '@/utils/sleep';
|
||||
import type { Station } from '@/apis/domains';
|
||||
import { DeviceType, getDeviceTypeVal } from '@/enums/device-type';
|
||||
import type { StationAlarmCounts } from './domains';
|
||||
|
||||
const createEmptyStationAlarmCounts = () => {
|
||||
return {
|
||||
[DeviceType.Camera]: 0,
|
||||
[DeviceType.Decoder]: 0,
|
||||
[DeviceType.Keyboard]: 0,
|
||||
[DeviceType.MediaServer]: 0,
|
||||
[DeviceType.Nvr]: 0,
|
||||
[DeviceType.SecurityBox]: 0,
|
||||
[DeviceType.Switch]: 0,
|
||||
[DeviceType.VideoServer]: 0,
|
||||
unclassified: 0,
|
||||
};
|
||||
};
|
||||
|
||||
export function useLineAlarmCountsQuery() {
|
||||
const stationStore = useStationStore();
|
||||
const { stationList } = storeToRefs(stationStore);
|
||||
const queryControlStore = useQueryControlStore();
|
||||
const { alarmQueryStamp } = storeToRefs(queryControlStore);
|
||||
const { mutateAsync: getStationAlarmCounts } = useStationAlarmCountsMutation();
|
||||
|
||||
return useQuery({
|
||||
queryKey: [LINE_ALARM_COUNTS_QUERY_KEY, alarmQueryStamp],
|
||||
enabled: computed(() => alarmQueryStamp.value > 0),
|
||||
staleTime: Infinity,
|
||||
refetchOnMount: false,
|
||||
refetchOnReconnect: false,
|
||||
refetchOnWindowFocus: false,
|
||||
queryFn: async ({ signal }) => {
|
||||
console.time('useLineALarmCountsQuery');
|
||||
for (const station of stationList.value) {
|
||||
await getStationAlarmCounts({ station, signal });
|
||||
}
|
||||
console.timeEnd('useLineALarmCountsQuery');
|
||||
return null;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
interface StationAlarmCountsMutationParams {
|
||||
station: Station;
|
||||
signal?: AbortSignal;
|
||||
}
|
||||
|
||||
function useStationAlarmCountsMutation() {
|
||||
const lineAlarmCountsStore = useLineAlarmCountsStore();
|
||||
const { lineAlarmCounts } = storeToRefs(lineAlarmCountsStore);
|
||||
|
||||
return useMutation<StationAlarmCounts, Error, StationAlarmCountsMutationParams>({
|
||||
mutationFn: async ({ station, signal }) => {
|
||||
const stationAlarmCounts = createEmptyStationAlarmCounts();
|
||||
if (!station.online) {
|
||||
return stationAlarmCounts;
|
||||
}
|
||||
const now = dayjs();
|
||||
const todayStart = now.startOf('date').valueOf();
|
||||
const todayEnd = now.endOf('date').valueOf();
|
||||
const { records: alarmList, total } = await postNdmDeviceAlarmLogPage(
|
||||
station.code,
|
||||
{
|
||||
model: {
|
||||
stationCode: station.code,
|
||||
},
|
||||
extra: {
|
||||
alarmDate_ge: todayStart,
|
||||
alarmDate_le: todayEnd,
|
||||
},
|
||||
size: 50000,
|
||||
current: 1,
|
||||
sort: 'id',
|
||||
order: 'descending',
|
||||
},
|
||||
signal,
|
||||
);
|
||||
for (const alarm of alarmList) {
|
||||
stationAlarmCounts[getDeviceTypeVal(alarm.deviceType)]++;
|
||||
}
|
||||
stationAlarmCounts.unclassified = parseInt(total);
|
||||
return stationAlarmCounts;
|
||||
},
|
||||
onSuccess: async (stationAlarmCounts, { station }) => {
|
||||
lineAlarmCounts.value[station.code] = stationAlarmCounts;
|
||||
await sleep();
|
||||
},
|
||||
onError: (error, { station }) => {
|
||||
console.error(`获取车站 ${station.name} 设备告警数据失败:`, error);
|
||||
lineAlarmCounts.value[station.code] = createEmptyStationAlarmCounts();
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -1,117 +0,0 @@
|
||||
import type { Station } from '@/apis/domains';
|
||||
import { postNdmDeviceAlarmLogPage } from '@/apis/requests';
|
||||
import { DeviceType, getDeviceTypeVal } from '@/enums/device-type';
|
||||
import { useQueryControlStore } from '@/stores/query-control';
|
||||
import { useStationStore } from '@/stores/station';
|
||||
import { useQuery } from '@tanstack/vue-query';
|
||||
import dayjs from 'dayjs';
|
||||
import { storeToRefs } from 'pinia';
|
||||
import { computed } from 'vue';
|
||||
import type { StationAlarms } from './domains';
|
||||
import { useLineAlarmsStore } from '@/stores/line-alarms';
|
||||
import { CanceledError } from 'axios';
|
||||
import { LINE_ALARMS_QUERY_KEY } from '@/constants';
|
||||
import { sleep } from '@/utils/sleep';
|
||||
|
||||
export interface LineAlarms {
|
||||
[stationCode: Station['code']]: StationAlarms;
|
||||
}
|
||||
|
||||
const createEmptyStationAlarms = (): StationAlarms => {
|
||||
return {
|
||||
[DeviceType.Camera]: [],
|
||||
[DeviceType.Decoder]: [],
|
||||
[DeviceType.Keyboard]: [],
|
||||
[DeviceType.MediaServer]: [],
|
||||
[DeviceType.Nvr]: [],
|
||||
[DeviceType.SecurityBox]: [],
|
||||
[DeviceType.Switch]: [],
|
||||
[DeviceType.VideoServer]: [],
|
||||
unclassified: [],
|
||||
};
|
||||
};
|
||||
|
||||
export function useLineAlarmsQuery() {
|
||||
const stationStore = useStationStore();
|
||||
const { stationList, onlineStationList } = storeToRefs(stationStore);
|
||||
const queryControlStore = useQueryControlStore();
|
||||
const { pollingEnabled } = storeToRefs(queryControlStore);
|
||||
const lineAlarmsStore = useLineAlarmsStore();
|
||||
const { lineAlarms } = storeToRefs(lineAlarmsStore);
|
||||
|
||||
return useQuery({
|
||||
queryKey: [LINE_ALARMS_QUERY_KEY],
|
||||
enabled: computed(() => onlineStationList.value.length > 0 && pollingEnabled.value),
|
||||
staleTime: Infinity,
|
||||
refetchOnMount: false,
|
||||
refetchOnReconnect: false,
|
||||
refetchOnWindowFocus: false,
|
||||
queryFn: async ({ signal }): Promise<LineAlarms> => {
|
||||
console.time('useLineAlarmsQuery');
|
||||
|
||||
if (!stationList?.value) {
|
||||
lineAlarms.value = {};
|
||||
return lineAlarms.value;
|
||||
}
|
||||
|
||||
for (const station of stationList.value) {
|
||||
if (!station.online) {
|
||||
lineAlarms.value[station.code] = createEmptyStationAlarms();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!lineAlarms.value[station.code]) {
|
||||
lineAlarms.value[station.code] = createEmptyStationAlarms();
|
||||
}
|
||||
|
||||
const stationAlarms = createEmptyStationAlarms();
|
||||
|
||||
const now = dayjs();
|
||||
const todayStart = now.startOf('date').valueOf();
|
||||
const todayEnd = now.endOf('date').valueOf();
|
||||
try {
|
||||
const { records: alarmList } = await postNdmDeviceAlarmLogPage(
|
||||
station.code,
|
||||
{
|
||||
model: {},
|
||||
extra: {
|
||||
alarmDate_ge: todayStart,
|
||||
alarmDate_le: todayEnd,
|
||||
deviceId_likeRight: station.code,
|
||||
},
|
||||
size: 50000,
|
||||
current: 1,
|
||||
sort: 'id',
|
||||
order: 'descending',
|
||||
},
|
||||
signal,
|
||||
);
|
||||
for (const alarm of alarmList) {
|
||||
const deviceType = getDeviceTypeVal(alarm.deviceType);
|
||||
stationAlarms[deviceType].push(alarm);
|
||||
}
|
||||
stationAlarms.unclassified = alarmList;
|
||||
lineAlarms.value[station.code] = stationAlarms;
|
||||
await sleep();
|
||||
} catch (error) {
|
||||
if (error instanceof CanceledError) return lineAlarms.value;
|
||||
console.error(`获取车站 ${station.name} 设备告警数据失败:`, error);
|
||||
stationAlarms[DeviceType.Camera] = [];
|
||||
stationAlarms[DeviceType.Decoder] = [];
|
||||
stationAlarms[DeviceType.Keyboard] = [];
|
||||
stationAlarms[DeviceType.MediaServer] = [];
|
||||
stationAlarms[DeviceType.Nvr] = [];
|
||||
stationAlarms[DeviceType.SecurityBox] = [];
|
||||
stationAlarms[DeviceType.Switch] = [];
|
||||
stationAlarms[DeviceType.VideoServer] = [];
|
||||
stationAlarms.unclassified = [];
|
||||
}
|
||||
}
|
||||
|
||||
console.timeEnd('useLineAlarmsQuery');
|
||||
|
||||
return lineAlarms.value;
|
||||
},
|
||||
placeholderData: (prev) => prev,
|
||||
});
|
||||
}
|
||||
@@ -1 +1,26 @@
|
||||
export * from './station-devices';
|
||||
import type {
|
||||
NdmCameraResultVO,
|
||||
NdmDecoderResultVO,
|
||||
NdmKeyboardResultVO,
|
||||
NdmMediaServerResultVO,
|
||||
NdmNvrResultVO,
|
||||
NdmSecurityBoxResultVO,
|
||||
NdmSwitchResultVO,
|
||||
NdmVideoServerResultVO,
|
||||
} from '@/apis/models';
|
||||
import type { DeviceType } from '@/enums/device-type';
|
||||
|
||||
export interface StationDevices {
|
||||
[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 interface LineDevices {
|
||||
[stationCode: string]: StationDevices;
|
||||
}
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
import type {
|
||||
NdmCameraResultVO,
|
||||
NdmDecoderResultVO,
|
||||
NdmKeyboardResultVO,
|
||||
NdmMediaServerResultVO,
|
||||
NdmNvrResultVO,
|
||||
NdmSecurityBoxResultVO,
|
||||
NdmSwitchResultVO,
|
||||
NdmVideoServerResultVO,
|
||||
} from '@/apis/models';
|
||||
import type { DeviceType } from '@/enums/device-type';
|
||||
|
||||
export interface StationDevices {
|
||||
[DeviceType.Camera]: NdmCameraResultVO[];
|
||||
[DeviceType.Decoder]: NdmDecoderResultVO[];
|
||||
[DeviceType.Keyboard]: NdmKeyboardResultVO[];
|
||||
[DeviceType.MediaServer]: NdmMediaServerResultVO[];
|
||||
[DeviceType.Nvr]: NdmNvrResultVO[];
|
||||
[DeviceType.SecurityBox]: NdmSecurityBoxResultVO[];
|
||||
[DeviceType.Switch]: NdmSwitchResultVO[];
|
||||
[DeviceType.VideoServer]: NdmVideoServerResultVO[];
|
||||
}
|
||||
@@ -1,10 +1,7 @@
|
||||
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, useQueryClient } from '@tanstack/vue-query';
|
||||
import { useMutation, useQuery } from '@tanstack/vue-query';
|
||||
import { storeToRefs } from 'pinia';
|
||||
import { computed } from 'vue';
|
||||
import type { StationDevices } from './domains';
|
||||
@@ -12,10 +9,7 @@ import { useLineDevicesStore } from '@/stores/line-devices';
|
||||
import { LINE_DEVICES_QUERY_KEY } from '@/constants';
|
||||
import { ndmClient } from '@/apis/client';
|
||||
import { sleep } from '@/utils/sleep';
|
||||
|
||||
export interface LineDevices {
|
||||
[stationCode: Station['code']]: StationDevices;
|
||||
}
|
||||
import type { Station } from '@/apis/domains';
|
||||
|
||||
const createEmptyStationDevices = (): StationDevices => {
|
||||
return {
|
||||
@@ -30,138 +24,64 @@ const createEmptyStationDevices = (): StationDevices => {
|
||||
};
|
||||
};
|
||||
|
||||
const getNdmDevicesAll = async (stationCode: string, signal: AbortSignal) => {
|
||||
const getNdmDevicesAll = async (stationCode: string, signal?: AbortSignal) => {
|
||||
const resp = await ndmClient.get<StationDevices>(`/${stationCode}/api/ndm/ndmDevices/all`, { retRaw: true, signal });
|
||||
const [err, stationDevices] = resp;
|
||||
if (err || !stationDevices) {
|
||||
const [err, data] = resp;
|
||||
if (err || !data) {
|
||||
throw err;
|
||||
}
|
||||
return stationDevices;
|
||||
return data;
|
||||
};
|
||||
|
||||
export function useLineDevicesQuery() {
|
||||
const stationStore = useStationStore();
|
||||
const { stationList, onlineStationList } = storeToRefs(stationStore);
|
||||
const { stationList } = storeToRefs(stationStore);
|
||||
const queryControlStore = useQueryControlStore();
|
||||
const { pollingEnabled } = storeToRefs(queryControlStore);
|
||||
const lineDevicesStore = useLineDevicesStore();
|
||||
const { lineDevices } = storeToRefs(lineDevicesStore);
|
||||
const queryClient = useQueryClient();
|
||||
const { deviceQueryStamp } = storeToRefs(queryControlStore);
|
||||
const { mutateAsync: getStationDevices } = useStationDevicesMutation();
|
||||
|
||||
return useQuery({
|
||||
queryKey: [LINE_DEVICES_QUERY_KEY],
|
||||
enabled: computed(() => onlineStationList.value.length > 0 && pollingEnabled.value),
|
||||
queryKey: [LINE_DEVICES_QUERY_KEY, deviceQueryStamp],
|
||||
enabled: computed(() => deviceQueryStamp.value > 0),
|
||||
staleTime: Infinity,
|
||||
refetchOnMount: false,
|
||||
refetchOnReconnect: false,
|
||||
refetchOnWindowFocus: false,
|
||||
queryFn: async ({ signal }): Promise<LineDevices> => {
|
||||
queryFn: async ({ signal }) => {
|
||||
console.time('useLineDevicesQuery');
|
||||
|
||||
// const pageQuery: PageParams<{}> = { model: {}, extra: {}, size: 5000, current: 1, sort: 'id', order: 'ascending' };
|
||||
|
||||
// const lineDevices: LineDevices = {};
|
||||
|
||||
// 如果没有车站列表,返回空数据
|
||||
if (!stationList?.value) {
|
||||
lineDevices.value = {};
|
||||
return lineDevices.value;
|
||||
}
|
||||
|
||||
// 遍历所有车站
|
||||
for (const station of stationList.value) {
|
||||
// 如果车站离线,设置空数据
|
||||
if (!station.online) {
|
||||
lineDevices.value[station.code] = createEmptyStationDevices();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!lineDevices.value[station.code]) {
|
||||
lineDevices.value[station.code] = createEmptyStationDevices();
|
||||
}
|
||||
|
||||
// const stationDevices = createEmptyStationDevices();
|
||||
|
||||
// await Promise.allSettled([
|
||||
// postNdmCameraPage(station.code, pageQuery, signal)
|
||||
// .then(({ records }) => {
|
||||
// stationDevices[DeviceType.Camera] = records;
|
||||
// })
|
||||
// .catch((error) => {
|
||||
// console.error(`获取车站 ${station.name} 摄像机数据失败:`, error);
|
||||
// stationDevices[DeviceType.Camera] = [];
|
||||
// }),
|
||||
// postNdmDecoderPage(station.code, pageQuery, signal)
|
||||
// .then(({ records }) => {
|
||||
// stationDevices[DeviceType.Decoder] = records;
|
||||
// })
|
||||
// .catch((error) => {
|
||||
// console.error(`获取车站 ${station.name} 解码器数据失败:`, error);
|
||||
// stationDevices[DeviceType.Decoder] = [];
|
||||
// }),
|
||||
// postNdmKeyboardPage(station.code, pageQuery, signal)
|
||||
// .then(({ records }) => {
|
||||
// stationDevices[DeviceType.Keyboard] = records;
|
||||
// })
|
||||
// .catch((error) => {
|
||||
// console.error(`获取车站 ${station.name} 网络键盘数据失败:`, error);
|
||||
// stationDevices[DeviceType.Keyboard] = [];
|
||||
// }),
|
||||
// postNdmMediaServerPage(station.code, pageQuery, signal)
|
||||
// .then(({ records }) => {
|
||||
// stationDevices[DeviceType.MediaServer] = records;
|
||||
// })
|
||||
// .catch((error) => {
|
||||
// console.error(`获取车站 ${station.name} 媒体服务器数据失败:`, error);
|
||||
// stationDevices[DeviceType.MediaServer] = [];
|
||||
// }),
|
||||
// postNdmNvrPage(station.code, pageQuery, signal)
|
||||
// .then(({ records }) => {
|
||||
// stationDevices[DeviceType.Nvr] = records;
|
||||
// })
|
||||
// .catch((error) => {
|
||||
// console.error(`获取车站 ${station.name} 录像机数据失败:`, error);
|
||||
// stationDevices[DeviceType.Nvr] = [];
|
||||
// }),
|
||||
// postNdmSecurityBoxPage(station.code, pageQuery, signal)
|
||||
// .then(({ records }) => {
|
||||
// stationDevices[DeviceType.SecurityBox] = records;
|
||||
// })
|
||||
// .catch((error) => {
|
||||
// console.error(`获取车站 ${station.name} 安防箱数据失败:`, error);
|
||||
// stationDevices[DeviceType.SecurityBox] = [];
|
||||
// }),
|
||||
// postNdmSwitchPage(station.code, pageQuery, signal)
|
||||
// .then(({ records }) => {
|
||||
// stationDevices[DeviceType.Switch] = records;
|
||||
// })
|
||||
// .catch((error) => {
|
||||
// console.error(`获取车站 ${station.name} 交换机数据失败:`, error);
|
||||
// stationDevices[DeviceType.Switch] = [];
|
||||
// }),
|
||||
// postNdmVideoServerPage(station.code, pageQuery, signal)
|
||||
// .then(({ records }) => {
|
||||
// stationDevices[DeviceType.VideoServer] = records;
|
||||
// })
|
||||
// .catch((error) => {
|
||||
// console.error(`获取车站 ${station.name} 视频服务器数据失败:`, error);
|
||||
// stationDevices[DeviceType.VideoServer] = [];
|
||||
// }),
|
||||
// ]);
|
||||
|
||||
const stationDevices = await getNdmDevicesAll(station.code, signal);
|
||||
|
||||
lineDevices.value[station.code] = stationDevices;
|
||||
|
||||
await sleep();
|
||||
await getStationDevices({ station, signal });
|
||||
}
|
||||
|
||||
console.timeEnd('useLineDevicesQuery');
|
||||
|
||||
queryClient.invalidateQueries({ queryKey: ['line-alarms'] });
|
||||
|
||||
return lineDevices.value;
|
||||
queryControlStore.updateAlarmQueryStamp();
|
||||
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: async (stationDevices, { station }) => {
|
||||
lineDevices.value[station.code] = stationDevices;
|
||||
await sleep();
|
||||
},
|
||||
onError: (error, { station }) => {
|
||||
console.error(`获取车站 ${station.name} 设备数据失败:`, error);
|
||||
lineDevices.value[station.code] = createEmptyStationDevices();
|
||||
},
|
||||
placeholderData: (prev) => prev,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,66 +1,63 @@
|
||||
import { userClient } from '@/apis/client';
|
||||
import type { Station } from '@/apis/domains';
|
||||
import { ndmVerify } from '@/apis/requests';
|
||||
import { batchVerify } from '@/apis/requests';
|
||||
import { STATION_LIST_QUERY_KEY } from '@/constants';
|
||||
import { useQueryControlStore } from '@/stores/query-control';
|
||||
import { useStationStore } from '@/stores/station';
|
||||
import { getAppEnvConfig } from '@/utils/env';
|
||||
import { useQuery, useQueryClient } from '@tanstack/vue-query';
|
||||
import { useMutation, useQuery } from '@tanstack/vue-query';
|
||||
import axios from 'axios';
|
||||
import dayjs from 'dayjs';
|
||||
import { storeToRefs } from 'pinia';
|
||||
import { computed } from 'vue';
|
||||
|
||||
export function useStationListQuery() {
|
||||
const stationStore = useStationStore();
|
||||
const { stationList } = storeToRefs(stationStore);
|
||||
const queryControlStore = useQueryControlStore();
|
||||
const { pollingEnabled } = storeToRefs(queryControlStore);
|
||||
const queryClient = useQueryClient();
|
||||
const { mutateAsync: getStationList } = useStationListMutation();
|
||||
|
||||
return useQuery({
|
||||
queryKey: [STATION_LIST_QUERY_KEY],
|
||||
enabled: computed(() => pollingEnabled.value),
|
||||
refetchInterval: getAppEnvConfig().requestInterval * 1000,
|
||||
staleTime: getAppEnvConfig().requestInterval * 1000,
|
||||
queryFn: async ({ signal }) => {
|
||||
// 主动登录校验
|
||||
const [err] = await userClient.post<void>(`/api/ndm/ndmKeepAlive/verify`, {}, { timeout: 5000, signal });
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
console.time('useStationListQuery');
|
||||
await getStationList({ signal });
|
||||
console.timeEnd('useStationListQuery');
|
||||
queryControlStore.updateDeviceQueryStamp();
|
||||
return null;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
interface StationListMutationParams {
|
||||
signal?: AbortSignal;
|
||||
}
|
||||
|
||||
function useStationListMutation() {
|
||||
const stationStore = useStationStore();
|
||||
const { stationList } = storeToRefs(stationStore);
|
||||
return useMutation<Station[], Error, StationListMutationParams>({
|
||||
mutationFn: async ({ signal }) => {
|
||||
const { data: ndmStationList } = await axios.get<{ code: string; name: string }[]>(`/minio/ndm/ndm-stations.json?_t=${dayjs().unix()}`, { signal });
|
||||
|
||||
let stations = ndmStationList.map<Station>((station) => ({
|
||||
const stations = ndmStationList.map<Station>((station) => ({
|
||||
code: station.code ?? '',
|
||||
name: station.name ?? '',
|
||||
online: false,
|
||||
}));
|
||||
|
||||
const pingResultList = await Promise.allSettled(stations.map((station) => ndmVerify(station.code, signal)));
|
||||
|
||||
stations = stations.map((station, index) => ({
|
||||
...station,
|
||||
online: pingResultList[index].status === 'fulfilled',
|
||||
}));
|
||||
|
||||
const isSame =
|
||||
stationList.value.length === stations.length &&
|
||||
stationList.value.every((oldStation, index) => {
|
||||
const newStation = stations[index];
|
||||
return oldStation.code === newStation.code && oldStation.name === newStation.name && oldStation.online === newStation.online;
|
||||
});
|
||||
|
||||
const verifyList = await batchVerify(signal);
|
||||
return stations.map((station) => {
|
||||
return {
|
||||
...station,
|
||||
online: !!verifyList.find((stn) => stn.stationCode === station.code)?.onlineState,
|
||||
};
|
||||
});
|
||||
},
|
||||
onSuccess: (stations) => {
|
||||
const isSame = JSON.stringify(stationList.value) === JSON.stringify(stations);
|
||||
if (!isSame) {
|
||||
stationList.value.splice(0, stationList.value.length, ...stations);
|
||||
}
|
||||
|
||||
// queryClient.invalidateQueries({ queryKey: ['station-devices'] });
|
||||
// queryClient.invalidateQueries({ queryKey: ['station-alarms'] });
|
||||
queryClient.invalidateQueries({ queryKey: ['line-devices'] });
|
||||
// queryClient.invalidateQueries({ queryKey: ['line-alarms'] });
|
||||
|
||||
return stations;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user