refactor: extract constants
This commit is contained in:
@@ -9,11 +9,27 @@ 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);
|
||||
@@ -23,7 +39,7 @@ export function useLineAlarmsQuery() {
|
||||
const { lineAlarms } = storeToRefs(lineAlarmsStore);
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['line-alarms'],
|
||||
queryKey: [LINE_ALARMS_QUERY_KEY],
|
||||
enabled: computed(() => onlineStationList.value.length > 0 && pollingEnabled.value),
|
||||
staleTime: Infinity,
|
||||
refetchOnMount: false,
|
||||
@@ -38,17 +54,15 @@ export function useLineAlarmsQuery() {
|
||||
}
|
||||
|
||||
for (const station of stationList.value) {
|
||||
lineAlarms.value[station.code] = {
|
||||
[DeviceType.Camera]: [],
|
||||
[DeviceType.Decoder]: [],
|
||||
[DeviceType.Keyboard]: [],
|
||||
[DeviceType.MediaServer]: [],
|
||||
[DeviceType.Nvr]: [],
|
||||
[DeviceType.SecurityBox]: [],
|
||||
[DeviceType.Switch]: [],
|
||||
[DeviceType.VideoServer]: [],
|
||||
unclassified: [],
|
||||
};
|
||||
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();
|
||||
@@ -70,26 +84,34 @@ export function useLineAlarmsQuery() {
|
||||
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);
|
||||
lineAlarms.value[station.code] = {
|
||||
[DeviceType.Camera]: cameraAlarms,
|
||||
[DeviceType.Decoder]: decoderAlarms,
|
||||
[DeviceType.Keyboard]: keyboardAlarms,
|
||||
[DeviceType.MediaServer]: mediaServerAlarms,
|
||||
[DeviceType.Nvr]: nvrAlarms,
|
||||
[DeviceType.SecurityBox]: securityBoxAlarms,
|
||||
[DeviceType.Switch]: switchAlarms,
|
||||
[DeviceType.VideoServer]: videoServerAlarms,
|
||||
unclassified: alarmList,
|
||||
};
|
||||
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 = [];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user