import { ElMessage, ElNotification, ElMessageBox } from 'element-plus' import useUserStore from './user' import { getDictByCode } from '@/api/system/dict' import { getWsUrl } from '@/utils/auth' import {getCurrentInstance} from "vue"; import useSettingsStore from "@/store/modules/settings"; import detectorHSStore from "@/store/modules/detectorHS"; import runPoint from "@/store/modules/runPoint"; import { Refresh, Crop } from '@element-plus/icons-vue' const pointStore = runPoint() const useWebsocketStore = defineStore( 'websocket', { state: () => ({ // wsUrl: `ws://${config.baseUrl.split('://')[1]}/websocket/${useUserStore().id}`, wsUrl: getWsUrl(), websocket: null, // ws连接 wsStatus: false, // websocket连接状态 true为连接 // wsPingTimer: null, // 心跳定时器 wsPingTime: 60000, // 心跳定时时长 wsReconnectCount: 0, // 重连次数 wsReconnectTimer: null, // 重连定时器 wsIsReconnect: true, // 是否重连 wsData: {}, // 默认提醒 mapData: {}, // 建图进度推送消息 positionData: {}, // 实时定位推送消息 positionList: [], robotData: {}, // 机器人状态消息 alarmData: {}, // 告警推送消息 heatData: {}, // 热力图数据 detectorVData: {}, // 探测器电压实时推送 detectorHData: {}, // 探测器实时推送psd和mca数据 psdData: {}, // 检测数值 gridData: {}, // 栅格信息 codeList: {}, result: {}, // 任务结束消息 refreshMap: false, }), getters: {}, actions: { // 连接websocket initWebSocket() { if (typeof (WebSocket) === 'undefined') { ElMessage({ message: '当前浏览器无法接收实时报警信息,请使用谷歌浏览器、火狐或360浏览器极速模式!', type: 'warning', // duration: 0, }) this.wsIsReconnect = true } else { // 实例化socket console.log(`**********socketUrl************`, getWsUrl() + useSettingsStore().robot.id) this.wsIsReconnect = false const socket = new WebSocket(getWsUrl() + useSettingsStore().robot.id) this.websocket = socket // 监听socket打开 socket.onopen = () => { console.log('浏览器WebSocket已打开') getDictByCode('alarmType').then((res) => { res.data.map((item: any) => { this.codeList[item.value] = item.name }) }) this.wsStatus = true } // 监听socket消息接收` socket.onmessage = (msg) => { // console.log('有新消息') // console.log(msg) // 转换为json对象 this.wsData = {} this.mapData = {} this.positionData = {} this.robotData = {} this.alarmData = {} this.heatData = {} this.detectorVData = {} this.detectorHData = {} this.psdData = {} this.gridData = {} this.result = {} const data: any = JSON.parse(msg.data) this.wsData = data switch (data.msgKey) { case 'pcd_load': // 机器人数据更新完毕 ElMessage.success('机器人数据更新成功') break case 'grid': // 建图进度推送消息 this.gridData = data.data break case 'process_message': // 建图进度推送消息 this.mapData = data.data break case 'pose_message': // 实时定位推送消息 this.positionData = data.data if(window.localStorage.getItem('isRunRoute') === '0') { pointStore.clear() } else { pointStore.push(data.data) } break case 'robot_status': // 机器人状态消息 this.robotData = data.data break case 'alarm': // 告警推送消息 this.alarmData = data.data break case 'psd': // 告警推送消息 this.psdData = data.data break case 'heat_map': // 热力图数据 this.heatData = data.data break case 'detector_vol': // 热力图数据 this.detectorVData = data.data break case 'detector_hs': // 热力图数据 detectorHSStore().setData(data.data) this.detectorHData = data.data break case 'task_end': // 热力图数据 if(data.data.robotId === useSettingsStore().robot.id) { if(data.data.code.toString() === '1') { ElMessageBox.confirm( data.data.hasOwnProperty('message') ? data.data.message: data.data.msg, '未寻找到放射源', { confirmButtonText: '确 认', type: 'error', center: true, } ) } else if(data.data.code.toString() === '0') { ElMessageBox.confirm( '寻找到放射源,坐标为:' + data.data.coordinate.x + ','+ data.data.coordinate.y, '寻源成功', { confirmButtonText: '确 认', type: 'success', center: true, } ) // ElNotification({ // message: '任务已完成,可以点击停止任务按钮', // type: 'success', // title: '任务提示', // duration: 0, // })00 } } break } } // 监听socket错误 socket.onerror = function () { ElMessage({ mesage: '无法接收实时报警信息,请检查服务器后重新刷新页面', type: 'error', // duration: 0, }) } // 监听socket关闭 socket.onclose = () => { console.log('WebSocket已关闭') this.wsStatus = false // if (this.wsPingTimer) { // console.log('停止定时器') // clearInterval(this.wsPingTimer) // this.wsPingTimer = null // } if (this.wsReconnectTimer) { clearTimeout(this.wsReconnectTimer) this.wsReconnectTimer = null } if (this.wsIsReconnect && this.wsReconnectCount === 0) { this.reConnectWebsocket() } } } }, // 断开websocket destroyWebSocket() { this.wsIsReconnect = false if (this.websocket) { this.websocket.close() } // if (this.wsPingTimer) { // console.log('停止定时器') // clearInterval(this.wsPingTimer) // this.wsPingTimer = null // } if (this.wsReconnectTimer) { clearTimeout(this.wsReconnectTimer) this.wsReconnectTimer = null } this.wsStatus = false }, // websocket重连 reConnectWebsocket() { console.log('重连', this.wsReconnectCount) // 逐渐延长重连时间 var time = 3000 if (this.wsReconnectCount < 20) { time = 3000 } else if (this.wsReconnectCount < 50) { time = 10000 } else if (this.wsReconnectCount < 100) { time = 450000 } else { return false } const wsReconnectTimer = setTimeout(() => { this.initWebSocket() }, time) this.wsReconnectTimer = wsReconnectTimer this.wsReconnectCount = this.wsReconnectCount + 1 }, }, }, ) export default useWebsocketStore