refactor: extract constants

This commit is contained in:
yangsy
2025-08-26 11:39:21 +08:00
parent 40bc4b76ef
commit 9fc5c170bc
7 changed files with 69 additions and 31 deletions

View File

@@ -9,11 +9,27 @@ import { storeToRefs } from 'pinia';
import { computed } from 'vue'; import { computed } from 'vue';
import type { StationAlarms } from './domains'; import type { StationAlarms } from './domains';
import { useLineAlarmsStore } from '@/stores/line-alarms'; import { useLineAlarmsStore } from '@/stores/line-alarms';
import { CanceledError } from 'axios';
import { LINE_ALARMS_QUERY_KEY } from '@/constants';
export interface LineAlarms { export interface LineAlarms {
[stationCode: Station['code']]: StationAlarms; [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() { export function useLineAlarmsQuery() {
const stationStore = useStationStore(); const stationStore = useStationStore();
const { stationList, onlineStationList } = storeToRefs(stationStore); const { stationList, onlineStationList } = storeToRefs(stationStore);
@@ -23,7 +39,7 @@ export function useLineAlarmsQuery() {
const { lineAlarms } = storeToRefs(lineAlarmsStore); const { lineAlarms } = storeToRefs(lineAlarmsStore);
return useQuery({ return useQuery({
queryKey: ['line-alarms'], queryKey: [LINE_ALARMS_QUERY_KEY],
enabled: computed(() => onlineStationList.value.length > 0 && pollingEnabled.value), enabled: computed(() => onlineStationList.value.length > 0 && pollingEnabled.value),
staleTime: Infinity, staleTime: Infinity,
refetchOnMount: false, refetchOnMount: false,
@@ -38,17 +54,15 @@ export function useLineAlarmsQuery() {
} }
for (const station of stationList.value) { for (const station of stationList.value) {
lineAlarms.value[station.code] = { if (!station.online) {
[DeviceType.Camera]: [], lineAlarms.value[station.code] = createEmptyStationAlarms();
[DeviceType.Decoder]: [], continue;
[DeviceType.Keyboard]: [], }
[DeviceType.MediaServer]: [],
[DeviceType.Nvr]: [], if (!lineAlarms.value[station.code]) {
[DeviceType.SecurityBox]: [], lineAlarms.value[station.code] = createEmptyStationAlarms();
[DeviceType.Switch]: [], }
[DeviceType.VideoServer]: [], const stationAlarms = lineAlarms.value[station.code];
unclassified: [],
};
try { try {
const now = dayjs(); const now = dayjs();
@@ -70,26 +84,34 @@ export function useLineAlarmsQuery() {
signal, signal,
); );
const cameraAlarms = alarmList.filter((device) => device.deviceType === DeviceType.Camera); const cameraAlarms = alarmList.filter((device) => device.deviceType === DeviceType.Camera);
stationAlarms[DeviceType.Camera] = cameraAlarms;
const decoderAlarms = alarmList.filter((device) => device.deviceType === DeviceType.Decoder); const decoderAlarms = alarmList.filter((device) => device.deviceType === DeviceType.Decoder);
stationAlarms[DeviceType.Decoder] = decoderAlarms;
const keyboardAlarms = alarmList.filter((device) => device.deviceType === DeviceType.Keyboard); const keyboardAlarms = alarmList.filter((device) => device.deviceType === DeviceType.Keyboard);
stationAlarms[DeviceType.Keyboard] = keyboardAlarms;
const mediaServerAlarms = alarmList.filter((device) => device.deviceType === DeviceType.MediaServer); const mediaServerAlarms = alarmList.filter((device) => device.deviceType === DeviceType.MediaServer);
stationAlarms[DeviceType.MediaServer] = mediaServerAlarms;
const nvrAlarms = alarmList.filter((device) => device.deviceType === DeviceType.Nvr); const nvrAlarms = alarmList.filter((device) => device.deviceType === DeviceType.Nvr);
stationAlarms[DeviceType.Nvr] = nvrAlarms;
const securityBoxAlarms = alarmList.filter((device) => device.deviceType === DeviceType.SecurityBox); const securityBoxAlarms = alarmList.filter((device) => device.deviceType === DeviceType.SecurityBox);
stationAlarms[DeviceType.SecurityBox] = securityBoxAlarms;
const switchAlarms = alarmList.filter((device) => device.deviceType === DeviceType.Switch); const switchAlarms = alarmList.filter((device) => device.deviceType === DeviceType.Switch);
stationAlarms[DeviceType.Switch] = switchAlarms;
const videoServerAlarms = alarmList.filter((device) => device.deviceType === DeviceType.VideoServer); const videoServerAlarms = alarmList.filter((device) => device.deviceType === DeviceType.VideoServer);
lineAlarms.value[station.code] = { stationAlarms[DeviceType.VideoServer] = videoServerAlarms;
[DeviceType.Camera]: cameraAlarms, stationAlarms.unclassified = alarmList;
[DeviceType.Decoder]: decoderAlarms,
[DeviceType.Keyboard]: keyboardAlarms,
[DeviceType.MediaServer]: mediaServerAlarms,
[DeviceType.Nvr]: nvrAlarms,
[DeviceType.SecurityBox]: securityBoxAlarms,
[DeviceType.Switch]: switchAlarms,
[DeviceType.VideoServer]: videoServerAlarms,
unclassified: alarmList,
};
} catch (error) { } catch (error) {
if (error instanceof CanceledError) return lineAlarms.value;
console.error(`获取车站 ${station.name} 设备告警数据失败:`, error); 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 = [];
} }
} }

View File

@@ -9,6 +9,8 @@ import { storeToRefs } from 'pinia';
import { computed } from 'vue'; import { computed } from 'vue';
import type { StationDevices } from './domains'; import type { StationDevices } from './domains';
import { useLineDevicesStore } from '@/stores/line-devices'; import { useLineDevicesStore } from '@/stores/line-devices';
import { CanceledError } from 'axios';
import { LINE_DEVICES_QUERY_KEY } from '@/constants';
export interface LineDevices { export interface LineDevices {
[stationCode: Station['code']]: StationDevices; [stationCode: Station['code']]: StationDevices;
@@ -36,7 +38,7 @@ export function useLineDevicesQuery() {
const { lineDevices } = storeToRefs(lineDevicesStore); const { lineDevices } = storeToRefs(lineDevicesStore);
return useQuery({ return useQuery({
queryKey: ['line-devices'], queryKey: [LINE_DEVICES_QUERY_KEY],
enabled: computed(() => onlineStationList.value.length > 0 && pollingEnabled.value), enabled: computed(() => onlineStationList.value.length > 0 && pollingEnabled.value),
staleTime: Infinity, staleTime: Infinity,
refetchOnMount: false, refetchOnMount: false,
@@ -74,6 +76,7 @@ export function useLineDevicesQuery() {
stationDevices[DeviceType.Camera] = records; stationDevices[DeviceType.Camera] = records;
}) })
.catch((error) => { .catch((error) => {
if (error instanceof CanceledError) return;
console.error(`获取车站 ${station.name} 摄像机数据失败:`, error); console.error(`获取车站 ${station.name} 摄像机数据失败:`, error);
stationDevices[DeviceType.Camera] = []; stationDevices[DeviceType.Camera] = [];
}), }),
@@ -82,6 +85,7 @@ export function useLineDevicesQuery() {
stationDevices[DeviceType.Decoder] = records; stationDevices[DeviceType.Decoder] = records;
}) })
.catch((error) => { .catch((error) => {
if (error instanceof CanceledError) return;
console.error(`获取车站 ${station.name} 解码器数据失败:`, error); console.error(`获取车站 ${station.name} 解码器数据失败:`, error);
stationDevices[DeviceType.Decoder] = []; stationDevices[DeviceType.Decoder] = [];
}), }),
@@ -90,6 +94,7 @@ export function useLineDevicesQuery() {
stationDevices[DeviceType.Keyboard] = records; stationDevices[DeviceType.Keyboard] = records;
}) })
.catch((error) => { .catch((error) => {
if (error instanceof CanceledError) return;
console.error(`获取车站 ${station.name} 网络键盘数据失败:`, error); console.error(`获取车站 ${station.name} 网络键盘数据失败:`, error);
stationDevices[DeviceType.Keyboard] = []; stationDevices[DeviceType.Keyboard] = [];
}), }),
@@ -98,6 +103,7 @@ export function useLineDevicesQuery() {
stationDevices[DeviceType.MediaServer] = records; stationDevices[DeviceType.MediaServer] = records;
}) })
.catch((error) => { .catch((error) => {
if (error instanceof CanceledError) return;
console.error(`获取车站 ${station.name} 媒体服务器数据失败:`, error); console.error(`获取车站 ${station.name} 媒体服务器数据失败:`, error);
stationDevices[DeviceType.MediaServer] = []; stationDevices[DeviceType.MediaServer] = [];
}), }),
@@ -106,6 +112,7 @@ export function useLineDevicesQuery() {
stationDevices[DeviceType.Nvr] = records; stationDevices[DeviceType.Nvr] = records;
}) })
.catch((error) => { .catch((error) => {
if (error instanceof CanceledError) return;
console.error(`获取车站 ${station.name} 录像机数据失败:`, error); console.error(`获取车站 ${station.name} 录像机数据失败:`, error);
stationDevices[DeviceType.Nvr] = []; stationDevices[DeviceType.Nvr] = [];
}), }),
@@ -114,6 +121,7 @@ export function useLineDevicesQuery() {
stationDevices[DeviceType.SecurityBox] = records; stationDevices[DeviceType.SecurityBox] = records;
}) })
.catch((error) => { .catch((error) => {
if (error instanceof CanceledError) return;
console.error(`获取车站 ${station.name} 安防箱数据失败:`, error); console.error(`获取车站 ${station.name} 安防箱数据失败:`, error);
stationDevices[DeviceType.SecurityBox] = []; stationDevices[DeviceType.SecurityBox] = [];
}), }),
@@ -122,6 +130,7 @@ export function useLineDevicesQuery() {
stationDevices[DeviceType.Switch] = records; stationDevices[DeviceType.Switch] = records;
}) })
.catch((error) => { .catch((error) => {
if (error instanceof CanceledError) return;
console.error(`获取车站 ${station.name} 交换机数据失败:`, error); console.error(`获取车站 ${station.name} 交换机数据失败:`, error);
stationDevices[DeviceType.Switch] = []; stationDevices[DeviceType.Switch] = [];
}), }),
@@ -130,6 +139,7 @@ export function useLineDevicesQuery() {
stationDevices[DeviceType.VideoServer] = records; stationDevices[DeviceType.VideoServer] = records;
}) })
.catch((error) => { .catch((error) => {
if (error instanceof CanceledError) return;
console.error(`获取车站 ${station.name} 视频服务器数据失败:`, error); console.error(`获取车站 ${station.name} 视频服务器数据失败:`, error);
stationDevices[DeviceType.VideoServer] = []; stationDevices[DeviceType.VideoServer] = [];
}), }),

View File

@@ -1,5 +1,6 @@
import type { Station } from '@/apis/domains'; import type { Station } from '@/apis/domains';
import { ndmVerify } from '@/apis/requests'; import { ndmVerify } from '@/apis/requests';
import { STATION_LIST_QUERY_KEY } from '@/constants';
import { useQueryControlStore } from '@/stores/query-control'; import { useQueryControlStore } from '@/stores/query-control';
import { useStationStore } from '@/stores/station'; import { useStationStore } from '@/stores/station';
import { getAppEnvConfig } from '@/utils/env'; import { getAppEnvConfig } from '@/utils/env';
@@ -14,10 +15,12 @@ export function useStationListQuery() {
const { stationList } = storeToRefs(stationStore); const { stationList } = storeToRefs(stationStore);
const queryControlStore = useQueryControlStore(); const queryControlStore = useQueryControlStore();
const { pollingEnabled } = storeToRefs(queryControlStore); const { pollingEnabled } = storeToRefs(queryControlStore);
const queryClient = useQueryClient();
return useQuery({ return useQuery({
queryKey: ['station-list'], queryKey: [STATION_LIST_QUERY_KEY],
enabled: computed(() => pollingEnabled.value), enabled: computed(() => pollingEnabled.value),
refetchInterval: getAppEnvConfig().requestInterval * 1000,
queryFn: async () => { queryFn: async () => {
const { data: ndmStationList } = await axios.get<{ code: string; name: string }[]>(`/minio/ndm/ndm-stations.json?_t=${dayjs().unix()}`); 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); stationList.value.splice(0, stationList.value.length, ...stations);
} }
const queryClient = useQueryClient();
// queryClient.invalidateQueries({ queryKey: ['station-devices'] }); // queryClient.invalidateQueries({ queryKey: ['station-devices'] });
// queryClient.invalidateQueries({ queryKey: ['station-alarms'] }); // queryClient.invalidateQueries({ queryKey: ['station-alarms'] });
queryClient.invalidateQueries({ queryKey: ['line-devices'] }); queryClient.invalidateQueries({ queryKey: ['line-devices'] });
@@ -53,6 +55,5 @@ export function useStationListQuery() {
return stations; return stations;
}, },
refetchInterval: getAppEnvConfig().requestInterval * 1000,
}); });
} }

1
src/constants/device.ts Normal file
View File

@@ -0,0 +1 @@
export const NDM_SWITCH_PROBE_INTERVAL = 5;

View File

@@ -1,4 +1,3 @@
export const JAVA_INTEGER_MAX_VALUE = 2147483647; export * from './device';
export const JAVA_UNSIGNED_INTEGER_MAX_VALUE = 4294967295; export * from './java';
export * from './query';
export const NDM_SWITCH_PROBE_INTERVAL = 5;

2
src/constants/java.ts Normal file
View 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
View 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';