var _this, baseTok = null, heAnth = null; var nowHeml = {}; var playSucc = false; var lightSucc = false; var sip_id = null, sip_pwd = null, sip_host = null, wss_url = null, stun_host = null, turn_host = null, turn_pwd = null, turn_user = null, device_sipId = null; var userAgent = null; var sipsession = null; var errorNum = 0; var remoteVideo = document.getElementById("helmeVi"); $(document).ready(function(){ heAnth = JSON.parse(localStorage.getItem("nowShip")); initAjax(); sipInit(); }) window.addEventListener('message',(respon)=>{ let _data = respon.data; if(_data.type == "helmet"){ if(_data.cmd == "ma_open_rtsp"&& _data.status){ let _ind = _data.play_url.findIndex((i,ind) => {return i.indexOf("webrtc://") != -1;}); startPlay(_data.api_url, _data.play_url[_ind]); $(".playSucc").css("display","none"); }else if(_data.cmd == "ma_stop_rtsp"&& _data.status){ $(".playSucc").css("display","block"); playSucc = false; }else if(_data.cmd == "ma_push_take_photo"){ if(_data.status){ ShowLoading("截图成功!",1); return; }else{ ShowLoading(_data.msg,1); return; } }else if(_data.msg.indexOf("打开手") != -1){ lightSucc = true; }else if(_data.msg.indexOf("关闭手") != -1){ lightSucc = false; } }else{ if(playSucc){ stopPlay(); } heAnth = JSON.parse(localStorage.getItem("nowShip")); initAjax(); } }) function sipInit() { let _obj = JSON.parse(localStorage.getItem("sipInfo")); sip_id = _obj["sip_id"]; sip_pwd = _obj["sip_pwd"]; sip_host = _obj["sip_host"]; wss_url = _obj["wss_url"]; stun_host = _obj["stun_host"]; turn_host = _obj["turn_host"]; turn_pwd = _obj["turn_pwd"]; turn_user = _obj["turn_user"]; var e = { uri: sip_id + '@' + sip_host, //此sip_id为拨打者账号 transportOptions: { wsServers: [wss_url], connectionTimeout: 30 }, authorizationUser: sip_id, password: sip_pwd, sessionDescriptionHandlerFactoryOptions: { peerConnectionOptions: { rtcConfiguration: { iceServers: [{ urls: 'stun:' + stun_host }, { urls: 'turn:' + turn_host, username: turn_user, credential: turn_pwd } ] } } } }; userAgent = new SIP.UA(e); userAgent.on("registered", function() { console.log("registered ok"); }) userAgent.on("registrationFailed", function(){ console.log("registrationFailed ok") }) userAgent.on("invite", function(session) { var url = session.remoteIdentity.uri.toString() + "--->call"; var isaccept = confirm(url); if (isaccept) { //接收来电 session.accept({ sessionDescriptionHandlerOptions: { constraints: { audio: true, video: false } } }); sipsession = session; session.on("accepted", function() { var pc = session.sessionDescriptionHandler.peerConnection; var remoteStream = new MediaStream(); pc.getReceivers().forEach(function(receiver) { remoteStream.addTrack(receiver.track); }); remoteVideo.srcObject = remoteStream; remoteVideo.play(); if (pc.getSenders()) { var localStream = new MediaStream(); pc.getSenders().forEach(function(sender) { localStream.addTrack(sender.track); }); localVideo.srcObject = localStream; localVideo.play(); } }); } else { //拒绝来电 session.reject(); } }) } function initAjax(){ if(heAnth !=null && heAnth != "null"&&heAnth.other_info !=null && heAnth.other_info != ""){ var dsd = heAnth.other_info.split(","); let _opts = { url: helUrl + "/index.php?ctl=bruce&act=get_group_member_list", type:"POST" }; let _data = { admin_id: 7903, udid: "11111111", g_id: dsd[0] }; postAxios(_opts, _data, {}).then((res) =>{ if(res.status){ let _obj = res.data; let sipList = JSON.parse(localStorage.getItem("sipLists")) || []; let _shui = _obj.filter(item => item.device_id == dsd[1])[0]; let _anm = sipList.filter(sipitem => sipitem.user_info.device_id == _shui.device_id); if(_anm.length){ _shui["locaInfo"] = _anm[0]["location_info"]; } nowHeml = _shui; setValhtml(); // changePlay(); }else{ nowHeml = {}; setValhtml(); } }) }else{ nowHeml = {}; setValhtml(); } }; function setValhtml(){ let _tem = nowHeml.locaInfo?nowHeml.locaInfo.tempdata:"--0000"; $("#helName").text(nowHeml.nick); $("#helId").text(nowHeml.device_id); $("#Elec").text(nowHeml.locaInfo?nowHeml.locaInfo.bat_l:"--"); $("#Temp").text((parseInt(_tem.split("--").pop())/1000).toFixed(1)); $("#Flow").text(nowHeml.locaInfo?nowHeml.locaInfo.sim_data_num:"--"); $("#Signal").text(nowHeml.locaInfo?(nowHeml.locaInfo.net_strenth<2?"弱":(nowHeml.locaInfo.net_strenth<4?"中":"强")):"--"); } function jumpToSee(key){ top.location.href = "./"+key+".html"; }; function changePlay(){ errorNum = 0; if(nowHeml.on_line == 0){ ShowLoading("安全帽离线中!",1); return; }else{ window.parent.postMessage({ act: "ma_open_rtsp", device_id: nowHeml.device_id, },staticUrl+ "/"); } }; function stopPlay(){ if(nowHeml.on_line == 0){ ShowLoading("安全帽离线中!",1); return; }else{ window.parent.postMessage({ act: "ma_stop_rtsp", device_id: nowHeml.device_id, },staticUrl+ "/"); } } var apiVal = null; var urlVal = null; function startPlay(_api, _url){ var video = document.getElementById('playWind'); var player = new JSWebrtc.Player(_api, _url, { video: video, autoplay: !0, onPlay: onPlay, onFail: onFail }); apiVal = _api; urlVal = _url; }; function onPlay() { playSucc = true; }; function onFail() { if(errorNum < 5){ errorNum += 1; setTimeout(function() { startPlay(apiVal, urlVal); }, 1000) }else{ setTimeout(function() { stopPlay(); }, 1000) } } // 截图 function handleCamer(){ if(nowHeml.on_line == 0){ ShowLoading("安全帽离线中!",1); return; }else{ window.parent.postMessage({ act: "ma_push_take_photo", user_id: nowHeml.user_id, device_id: nowHeml.device_id },staticUrl+ "/"); } } // 手电筒 function handleLight(){ let _uuid = guid(); window.parent.postMessage({ device_id: nowHeml.device_id, act: "push_to_client", data: { cmd: "server_push_turnLights", talkback_id: _uuid, type: lightSucc?0:1 }, p_id: nowHeml.user_id, p_type: nowHeml.cap_type, sendback: 1, talkback_id: _uuid },staticUrl+ "/"); } function guid() { return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(e) { var t = 16 * Math.random() | 0; return ("x" == e ? t : 3 & t | 8).toString(16) }) }