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 = [];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,8 @@ import { storeToRefs } from 'pinia';
|
||||
import { computed } from 'vue';
|
||||
import type { StationDevices } from './domains';
|
||||
import { useLineDevicesStore } from '@/stores/line-devices';
|
||||
import { CanceledError } from 'axios';
|
||||
import { LINE_DEVICES_QUERY_KEY } from '@/constants';
|
||||
|
||||
export interface LineDevices {
|
||||
[stationCode: Station['code']]: StationDevices;
|
||||
@@ -36,7 +38,7 @@ export function useLineDevicesQuery() {
|
||||
const { lineDevices } = storeToRefs(lineDevicesStore);
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['line-devices'],
|
||||
queryKey: [LINE_DEVICES_QUERY_KEY],
|
||||
enabled: computed(() => onlineStationList.value.length > 0 && pollingEnabled.value),
|
||||
staleTime: Infinity,
|
||||
refetchOnMount: false,
|
||||
@@ -74,6 +76,7 @@ export function useLineDevicesQuery() {
|
||||
stationDevices[DeviceType.Camera] = records;
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error instanceof CanceledError) return;
|
||||
console.error(`获取车站 ${station.name} 摄像机数据失败:`, error);
|
||||
stationDevices[DeviceType.Camera] = [];
|
||||
}),
|
||||
@@ -82,6 +85,7 @@ export function useLineDevicesQuery() {
|
||||
stationDevices[DeviceType.Decoder] = records;
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error instanceof CanceledError) return;
|
||||
console.error(`获取车站 ${station.name} 解码器数据失败:`, error);
|
||||
stationDevices[DeviceType.Decoder] = [];
|
||||
}),
|
||||
@@ -90,6 +94,7 @@ export function useLineDevicesQuery() {
|
||||
stationDevices[DeviceType.Keyboard] = records;
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error instanceof CanceledError) return;
|
||||
console.error(`获取车站 ${station.name} 网络键盘数据失败:`, error);
|
||||
stationDevices[DeviceType.Keyboard] = [];
|
||||
}),
|
||||
@@ -98,6 +103,7 @@ export function useLineDevicesQuery() {
|
||||
stationDevices[DeviceType.MediaServer] = records;
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error instanceof CanceledError) return;
|
||||
console.error(`获取车站 ${station.name} 媒体服务器数据失败:`, error);
|
||||
stationDevices[DeviceType.MediaServer] = [];
|
||||
}),
|
||||
@@ -106,6 +112,7 @@ export function useLineDevicesQuery() {
|
||||
stationDevices[DeviceType.Nvr] = records;
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error instanceof CanceledError) return;
|
||||
console.error(`获取车站 ${station.name} 录像机数据失败:`, error);
|
||||
stationDevices[DeviceType.Nvr] = [];
|
||||
}),
|
||||
@@ -114,6 +121,7 @@ export function useLineDevicesQuery() {
|
||||
stationDevices[DeviceType.SecurityBox] = records;
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error instanceof CanceledError) return;
|
||||
console.error(`获取车站 ${station.name} 安防箱数据失败:`, error);
|
||||
stationDevices[DeviceType.SecurityBox] = [];
|
||||
}),
|
||||
@@ -122,6 +130,7 @@ export function useLineDevicesQuery() {
|
||||
stationDevices[DeviceType.Switch] = records;
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error instanceof CanceledError) return;
|
||||
console.error(`获取车站 ${station.name} 交换机数据失败:`, error);
|
||||
stationDevices[DeviceType.Switch] = [];
|
||||
}),
|
||||
@@ -130,6 +139,7 @@ export function useLineDevicesQuery() {
|
||||
stationDevices[DeviceType.VideoServer] = records;
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error instanceof CanceledError) return;
|
||||
console.error(`获取车站 ${station.name} 视频服务器数据失败:`, error);
|
||||
stationDevices[DeviceType.VideoServer] = [];
|
||||
}),
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { Station } from '@/apis/domains';
|
||||
import { ndmVerify } 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';
|
||||
@@ -14,10 +15,12 @@ export function useStationListQuery() {
|
||||
const { stationList } = storeToRefs(stationStore);
|
||||
const queryControlStore = useQueryControlStore();
|
||||
const { pollingEnabled } = storeToRefs(queryControlStore);
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['station-list'],
|
||||
queryKey: [STATION_LIST_QUERY_KEY],
|
||||
enabled: computed(() => pollingEnabled.value),
|
||||
refetchInterval: getAppEnvConfig().requestInterval * 1000,
|
||||
queryFn: async () => {
|
||||
const { data: ndmStationList } = await axios.get<{ code: string; name: string }[]>(`/minio/ndm/ndm-stations.json?_t=${dayjs().unix()}`);
|
||||
|
||||
@@ -45,7 +48,6 @@ export function useStationListQuery() {
|
||||
stationList.value.splice(0, stationList.value.length, ...stations);
|
||||
}
|
||||
|
||||
const queryClient = useQueryClient();
|
||||
// queryClient.invalidateQueries({ queryKey: ['station-devices'] });
|
||||
// queryClient.invalidateQueries({ queryKey: ['station-alarms'] });
|
||||
queryClient.invalidateQueries({ queryKey: ['line-devices'] });
|
||||
@@ -53,6 +55,5 @@ export function useStationListQuery() {
|
||||
|
||||
return stations;
|
||||
},
|
||||
refetchInterval: getAppEnvConfig().requestInterval * 1000,
|
||||
});
|
||||
}
|
||||
|
||||
1
src/constants/device.ts
Normal file
1
src/constants/device.ts
Normal file
@@ -0,0 +1 @@
|
||||
export const NDM_SWITCH_PROBE_INTERVAL = 5;
|
||||
@@ -1,4 +1,3 @@
|
||||
export const JAVA_INTEGER_MAX_VALUE = 2147483647;
|
||||
export const JAVA_UNSIGNED_INTEGER_MAX_VALUE = 4294967295;
|
||||
|
||||
export const NDM_SWITCH_PROBE_INTERVAL = 5;
|
||||
export * from './device';
|
||||
export * from './java';
|
||||
export * from './query';
|
||||
|
||||
2
src/constants/java.ts
Normal file
2
src/constants/java.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export const JAVA_INTEGER_MAX_VALUE = 2147483647;
|
||||
export const JAVA_UNSIGNED_INTEGER_MAX_VALUE = 4294967295;
|
||||
3
src/constants/query.ts
Normal file
3
src/constants/query.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export const STATION_LIST_QUERY_KEY = 'station-list';
|
||||
export const LINE_DEVICES_QUERY_KEY = 'line-devices';
|
||||
export const LINE_ALARMS_QUERY_KEY = 'line-alarms';
|
||||
Reference in New Issue
Block a user