125 lines
4.9 KiB
TypeScript
125 lines
4.9 KiB
TypeScript
import type { Station } from '@/apis/domains';
|
|
import { postNdmDeviceAlarmLogPage } 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 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';
|
|
|
|
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 = lineAlarms.value[station.code];
|
|
|
|
try {
|
|
const now = dayjs();
|
|
const todayStart = now.startOf('date').valueOf();
|
|
const todayEnd = now.endOf('date').valueOf();
|
|
const { records: alarmList } = await postNdmDeviceAlarmLogPage(
|
|
station.code,
|
|
{
|
|
model: {},
|
|
extra: {
|
|
alarmDate_ge: todayStart,
|
|
alarmDate_le: todayEnd,
|
|
},
|
|
size: 50000,
|
|
current: 1,
|
|
sort: 'id',
|
|
order: 'descending',
|
|
},
|
|
signal,
|
|
);
|
|
const cameraAlarms = alarmList.filter((device) => device.deviceType === DeviceType.Camera);
|
|
stationAlarms[DeviceType.Camera] = cameraAlarms;
|
|
const decoderAlarms = alarmList.filter((device) => device.deviceType === DeviceType.Decoder);
|
|
stationAlarms[DeviceType.Decoder] = decoderAlarms;
|
|
const keyboardAlarms = alarmList.filter((device) => device.deviceType === DeviceType.Keyboard);
|
|
stationAlarms[DeviceType.Keyboard] = keyboardAlarms;
|
|
const mediaServerAlarms = alarmList.filter((device) => device.deviceType === DeviceType.MediaServer);
|
|
stationAlarms[DeviceType.MediaServer] = mediaServerAlarms;
|
|
const nvrAlarms = alarmList.filter((device) => device.deviceType === DeviceType.Nvr);
|
|
stationAlarms[DeviceType.Nvr] = nvrAlarms;
|
|
const securityBoxAlarms = alarmList.filter((device) => device.deviceType === DeviceType.SecurityBox);
|
|
stationAlarms[DeviceType.SecurityBox] = securityBoxAlarms;
|
|
const switchAlarms = alarmList.filter((device) => device.deviceType === DeviceType.Switch);
|
|
stationAlarms[DeviceType.Switch] = switchAlarms;
|
|
const videoServerAlarms = alarmList.filter((device) => device.deviceType === DeviceType.VideoServer);
|
|
stationAlarms[DeviceType.VideoServer] = videoServerAlarms;
|
|
stationAlarms.unclassified = alarmList;
|
|
} 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,
|
|
});
|
|
}
|