import { layuiObj, loginInfo } from "./index.js"; import { serverIp } from "../../../public/js/url.js"; var sosSdk = null; //当前sos设备告警列表 var currentSosAlarmTask = {}; var sosInitState=false;//监测sos初始化是否成功。 function connectMapInit(){ if(sosInitState==false){ //加载地图 sosInitState=true; } } function initSosSdk() { //内网情况就不用连接了。 if(serverIp!="43.15.198.61"){ sosSdk = new SOS.SDK({ Account: loginInfo["user_name"],// 这里填写account Password: "123456",// 这里填写password }); try { //sdk初始化会有一个过程,包括与初始化自己本身,与服务器交互等过程,需要等待 sosSdk.init().then(() => { // 初始化成功(这里成功之后,也会通过sdk.addEventListener(SOS.EVENT.SDK_STATE)的消息通知到) console.log('SDK启动成功,版本号:', sosSdk.version()); //加载地图 connectMapInit(); }).catch(error => { console.error(error); //加载地图 connectMapInit(); }).finally(()=>{ console.log("完成") }) } catch (error) { //加载地图 connectMapInit(); } //SDK运行状态事件通知 sosSdk.addEventListener(SOS.EVENT.SDK_STATE, (event, code, description) => { switch (event) { case SOS.EVENT.SDK_STATE_CODE.WARN: connectMapInit(); // 一般是网络原因与服务器连接断开,会warn layuiObj.layer.msg('sdk state: warn', description); break; case SOS.EVENT.SDK_STATE_CODE.ERROR: if (code === SOS.ERROR_CODE.LOGIN_CONFLICT) { // 账号在其他地方登录,这个需要处理下 layuiObj.layer.msg('你的账号已在其他地方登录'); // 这里一定要记得把sdk destroy掉,不然会触发sdk的重连机制 sosSdk && sdk.destroy(); sosSdk = null; } //sos告警账号密码不存在 if( code==6 || code==1 || code==9 ){ //一般是网络原因与服务器连接断开,会warn //layuiObj.layer.msg('报警器登录失败'); //加载地图 connectMapInit(); } break; case SOS.EVENT.SDK_STATE_CODE.SUCCESS: console.info('sdk state: success'); break; default: break; } }) //设备告警事件通知 sosSdk.addEventListener(SOS.EVENT.DEVICE_ALARM_STATE, (event, device, task, operator) => { switch (event) { case SOS.EVENT.DEVICE_ALARM_STATE_CODE.RING: var deviceId = device.Id; if (!(deviceId in currentSosAlarmTask)) { currentSosAlarmTask[deviceId] = task; } console.log('设备:', device.Id, device.Name, '正在告警,TaskCode:' + task.TaskCode); break; case SOS.EVENT.DEVICE_ALARM_STATE_CODE.ACCEPT: // 本端sdk接警,也会有这个通知,需要判断出来是自己接的,还是其他人接的 if (sosSdk.isMe(operator.SipNum)) { console.log('设备:', device.Id, device.Name, '已经被我自己接警'); } else { console.log('设备:', device.Id, device.Name, '已经被其他人接警'); } break; case SOS.EVENT.DEVICE_ALARM_STATE_CODE.NOANSWER: console.log('设备:', device.Id, device.Name, '无人接警'); break; case SOS.EVENT.DEVICE_ALARM_STATE_CODE.FINISHED: console.log('设备:', device.Id, device.Name, '已完成接警'); break; default: break; } }) } } //开始接警 async function sosDeviceAlarmAccept(obj) { var deviceid=obj["deviceId"]; try { const disconnectedCallback = (SipNum) => { // 开始接警之后,有设备(或自己)退出 console.warn(SipNum, '已退出接警', sosSdk && sosSdk.isMe(SipNum)); } const stopCallback = () => { console.warn('服务器结束接警'); } if (deviceid in currentSosAlarmTask) { var alarmTaskObject = currentSosAlarmTask[deviceid] // 下面这个alarmTaskObject,就是通过告警通知,或者告警列表获取到的 const rsp = await sosSdk.deviceAlarmAccept(alarmTaskObject, { AudioDomId: `sosAlarmAudio${deviceid}`, // 非必填参数 OnDisconnected: disconnectedCallback, OnStop: stopCallback, }); } else { openSosAlarmAudio(deviceid); } console.log('接警成功, expire(接警时间,单位秒):' ); } catch (error) { console.log('接警失败', error); } } //结束接警 async function sosDeviceAlarmDone(obj) { var deviceid=obj["deviceId"]; try { if (deviceid in currentSosAlarmTask) { var alarmTaskObject = currentSosAlarmTask[deviceid]; const rsp = await sosSdk.deviceAlarmDone(alarmTaskObject); delete currentSosAlarmTask[deviceid] } else { stopSosAlarmAudio(deviceid) } } catch (error) { console.log('结束接警失败', error) } } var currentAlarmSosAudio = {}; //开启报警的对讲 function openSosAlarmAudio(deviceid) { if (sosSdk) { let disconnectedCallback = (SipNum) => { // 开始对讲之后,有设备或自己退出对讲 console.warn(SipNum, "已退出对讲", sosSdk && sosSdk.isMe(SipNum)); } let stopCallback = () => { // 开始对讲之后,服务器结束了对讲 console.warn("服务器结束对讲"); } sosSdk.audioSpeakStart({ // 必填参数 DeviceId: parseInt(deviceid), AudioDomId: `sosAlarmAudio${deviceid}`, // 要保证document.getElementById("html_audio_remote")非null,你的audio标签,一定要存在 // 非必填参数 Force: false, OnDisconnected: disconnectedCallback, OnStop: stopCallback, }).then((ret) => { currentAlarmSosAudio[deviceid] = ret.TaskCode console.log("!!!!!!!!!!", ret); }).catch((e) => { console.log(e); switch (e.code) { case 2005: break; case 3551: break; case 3552: break; default: break; } }) } else { alert("对讲失败,请先启动SDK") } } // 结束对讲 function stopSosAlarmAudio(deviceid) { if (sosSdk) { if (deviceid in currentAlarmSosAudio) { sosSdk.audioSpeakStop({ TaskCode: currentAlarmSosAudio[deviceid] }).then(() => { console.log("对讲停止成功"); }).catch((error) => { console.error(error); }) } } else { alert("对讲停止失败,请先启动SDK") } } export { initSosSdk, sosSdk, sosDeviceAlarmAccept, sosDeviceAlarmDone }