import { RESULT_CODE_ERROR } from '@/constants/result-constants'; import { OptionsType, WS } from '@/plugins/ws-websocket'; import { onBeforeUnmount, onMounted } from 'vue'; import { ueEventParse, cdrEventParse, eventData, eventTotal, eventId, } from './useUserActivity'; import { upfTotalFlow, upfTFParse } from './useUPFTotalFlow'; import { neStateParse } from './useTopology'; /**websocket连接 */ export default function useWS() { const ws = new WS(); /**发消息 */ function wsSend(data: Record) { ws.send(data); } /**接收数据后回调 */ function wsError(ev: any) { // 接收数据后回调 console.error(ev); } /**接收数据后回调 */ function wsMessage(res: Record) { // console.log(res); const { code, requestId, data } = res; if (code === RESULT_CODE_ERROR) { console.warn(res.msg); return; } // 网元状态 if (requestId && requestId.startsWith('neState')) { const neType = requestId.split('_')[1]; neStateParse(neType, data); return; } // 普通信息 switch (requestId) { // ueEvent UE会话事件 case '1010': if (Array.isArray(data.rows)) { eventTotal.value += data.total; for (const item of data.rows) { const v = ueEventParse(item); if (v) { eventData.value.push(v); } } eventId.value = eventData.value[0].eId; } break; //cdrEvent CDR会话事件 case '1005': if (Array.isArray(data.rows)) { eventTotal.value += data.total; for (const item of data.rows) { const v = cdrEventParse(item); if (v) { eventData.value.push(v); } } eventId.value = eventData.value[0].eId; } break; //UPF-总流量数 case '1030_0': const v0 = upfTFParse(data); upfTotalFlow.value[0].up = v0.up; upfTotalFlow.value[0].down = v0.down; break; case '1030_7': const v7 = upfTFParse(data); upfTotalFlow.value[1].up = v7.up; upfTotalFlow.value[1].down = v7.down; break; case '1030_30': const v30 = upfTFParse(data); upfTotalFlow.value[2].up = v30.up; upfTotalFlow.value[2].down = v30.down; break; } // 订阅组信息 if (!data?.groupId) { return; } switch (data.groupId) { // ueEvent UE会话事件 case '1010': if (data.data) { const v = ueEventParse(data.data); if (v) { eventData.value.unshift(v); eventTotal.value += 1; eventId.value = v.eId; if (eventData.value.length > 50) { eventData.value.pop(); } } } break; // cdrEvent CDR会话事件 case '1005': if (data.data) { const v = cdrEventParse(data.data); if (v) { eventData.value.unshift(v); eventTotal.value += 1; eventId.value = v.eId; if (eventData.value.length > 50) { eventData.value.pop(); } } } break; } } /**UPF-总流量数 发消息*/ function upfTFSend(day: 0 | 7 | 30) { ws.send({ requestId: `1030_${day}`, type: 'upf_tf', data: { neType: 'UPF', neId: '001', day: day, }, }); } /**ueEvent UE会话事件 发消息*/ function ueEventSend() { ws.send({ requestId: '1010', type: 'ue', data: { neType: 'AMF', neId: '001', sortField: 'timestamp', sortOrder: 'desc', pageNum: 1, pageSize: 25, }, }); } /**cdrEvent CDR会话事件 发消息*/ function cdrEventSend() { ws.send({ requestId: '1005', type: 'cdr', data: { neType: 'IMS', neId: '001', sortField: 'timestamp', sortOrder: 'desc', pageNum: 1, pageSize: 25, }, }); } onMounted(() => { const options: OptionsType = { url: '/ws', params: { /**订阅通道组 * * UE会话事件-AMF (GroupID:1010) * CDR会话事件-IMS (GroupID:1005) */ subGroupID: '1010,1005', }, onmessage: wsMessage, onerror: wsError, }; ws.connect(options); }); onBeforeUnmount(() => { ws.close(); }); return { wsSend, cdrEventSend, ueEventSend, upfTFSend, }; }