helmetLive.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. var heAnth = null,heList = [],nowHe = null,setItime = null,setItimes = 0;
  2. var sip_id = null,
  3. sip_pwd = null,
  4. sip_host = null,
  5. wss_url = null,
  6. stun_host = null,
  7. turn_host = null,
  8. turn_pwd = null,
  9. turn_user = null,
  10. device_sipId = null;
  11. var userAgent = null;
  12. var sipsession = null;
  13. var localVideo = document.getElementById("remoteMonitor");
  14. var remoteVideo = document.getElementById("helmeVi");
  15. window.addEventListener('message',(_data)=>{
  16. if(_data.data.cmd != undefined){
  17. if(_data.data.cmd == "ma_push_take_photo"){
  18. if(_data.data.status){
  19. ShowLoading("截图成功!",1);
  20. return;
  21. }else{
  22. ShowLoading("截图失败!",1);
  23. return;
  24. }
  25. }else{
  26. setPlay(_data.data);
  27. }
  28. }else{
  29. if(nowHe != null){
  30. $("._Call img").attr("src","../img/voice.yz4975.png");
  31. if(sipsession) sipsession.terminate();
  32. clearInterval(setItime);
  33. setItime = null;
  34. setItimes = 0;
  35. $("._Call .margin-top-sm").text("未连接");
  36. $("._Call .text-lg").text("00:00:00");
  37. $("._helme").css("display","none");
  38. $("#html_view li .text-sm").removeClass("act");
  39. window.parent.postMessage({info:{act: "ma_stop_rtsp",device_id: nowHe.device_id},change: "helemt"}, '*');
  40. }
  41. heAnth = JSON.parse(localStorage.getItem("nowShip"));
  42. getList(0);
  43. }
  44. })
  45. var User = JSON.parse(localStorage.getItem("_yz_TU"));
  46. $(document).ready(function(){
  47. heAnth = JSON.parse(localStorage.getItem("nowShip"));
  48. getList(0);
  49. setInterval(()=>{
  50. getList(1);
  51. },60*1000);
  52. sipInit();
  53. });
  54. // 截图
  55. function handleScreen(){
  56. if(nowHe != null){
  57. window.parent.postMessage({info:{
  58. act: "ma_push_take_photo",
  59. user_id: nowHe.user_id
  60. }, change: "helemt" }, '*');
  61. }
  62. }
  63. function setPlay(_data){
  64. if(_data.cmd == "ma_open_rtsp"&& _data.status){
  65. let _ind = _data.play_url.findIndex((i,ind) => {return i.indexOf("webrtc://") != -1;});
  66. player = new JSWebrtc.Player(_data.api_url, _data.play_url[_ind], {
  67. video: document.getElementById("localMonitor"),
  68. autoplay: !0,
  69. onPlay: startPlay,
  70. onFail: onFail
  71. })
  72. }else if(_data.cmd == "ma_stop_rtsp"&& _data.status){
  73. $("._Call").css("display","none");
  74. callErr();
  75. $("#_View").html('<div class="noBg"><img src="../img/maozi.png" ></div>');
  76. }else if(_data.cmd == "ma_set_sip_info"&& _data.status){
  77. sipCall();
  78. }
  79. }
  80. var startPlay = function () {
  81. console.log("开始播放");
  82. $(".loadImg").remove();
  83. };
  84. var onFail = function () {
  85. setTimeout(function() {
  86. window.parent.postMessage({info:{act: "ma_open_rtsp",device_id: nowHe.device_id},change: "helemt"}, '*');
  87. }, 1000)
  88. }
  89. function sipInit() {
  90. let _obj = JSON.parse(localStorage.getItem("sipInfo"));
  91. sip_id = _obj["sip_id"];
  92. sip_pwd = _obj["sip_pwd"];
  93. sip_host = _obj["sip_host"];
  94. wss_url = _obj["wss_url"];
  95. stun_host = _obj["stun_host"];
  96. turn_host = _obj["turn_host"];
  97. turn_pwd = _obj["turn_pwd"];
  98. turn_user = _obj["turn_user"];
  99. var e = {
  100. uri: sip_id + '@' + sip_host, //此sip_id为拨打者账号
  101. transportOptions: {
  102. wsServers: [wss_url],
  103. connectionTimeout: 30
  104. },
  105. authorizationUser: sip_id,
  106. password: sip_pwd,
  107. sessionDescriptionHandlerFactoryOptions: {
  108. peerConnectionOptions: {
  109. rtcConfiguration: {
  110. iceServers: [{
  111. urls: 'stun:' + stun_host
  112. },
  113. {
  114. urls: 'turn:' + turn_host,
  115. username: turn_user,
  116. credential: turn_pwd
  117. }
  118. ]
  119. }
  120. }
  121. }
  122. };
  123. userAgent = new SIP.UA(e);
  124. userAgent.on("registered", function() {
  125. console.log("registered ok");
  126. })
  127. userAgent.on("registrationFailed", function(){
  128. console.log("registrationFailed ok")
  129. })
  130. userAgent.on("invite", function(session) {
  131. var url = session.remoteIdentity.uri.toString() + "--->call";
  132. var isaccept = confirm(url);
  133. if (isaccept) {
  134. //接收来电
  135. session.accept({
  136. sessionDescriptionHandlerOptions: {
  137. constraints: {
  138. audio: true,
  139. video: false
  140. }
  141. }
  142. });
  143. sipsession = session;
  144. session.on("accepted", function() {
  145. var pc = session.sessionDescriptionHandler.peerConnection;
  146. var remoteStream = new MediaStream();
  147. pc.getReceivers().forEach(function(receiver) {
  148. remoteStream.addTrack(receiver.track);
  149. });
  150. remoteVideo.srcObject = remoteStream;
  151. remoteVideo.play();
  152. if (pc.getSenders()) {
  153. var localStream = new MediaStream();
  154. pc.getSenders().forEach(function(sender) {
  155. localStream.addTrack(sender.track);
  156. });
  157. localVideo.srcObject = localStream;
  158. localVideo.play();
  159. }
  160. });
  161. } else {
  162. //拒绝来电
  163. session.reject();
  164. }
  165. })
  166. }
  167. function sipCall(){
  168. sipsession = userAgent.invite(device_sipId + '@' + sip_host, {
  169. sessionDescriptionHandlerOptions: {
  170. constraints: {
  171. audio: true,
  172. video: false //音频通话则为false
  173. }
  174. }
  175. });
  176. sipsession.on("accepted", function() {
  177. var pc = sipsession.sessionDescriptionHandler.peerConnection;
  178. // Gets remote tracks
  179. var remoteStream = new MediaStream();
  180. pc.getReceivers().forEach(function(receiver) {
  181. remoteStream.addTrack(receiver.track);
  182. });
  183. //此处remoteVideo为一个video标签
  184. remoteVideo.srcObject = remoteStream;
  185. remoteVideo.play();
  186. setTimeout(()=>{
  187. callSes();
  188. },4000)
  189. });
  190. sipsession.on("rejected", function(t, i) {
  191. // console.log("onRejected - ", t, i)
  192. });
  193. sipsession.on("failed", function(t, i) {
  194. // console.log("onFailed - ", t, i)
  195. });
  196. sipsession.on("terminated", function(t, i) {
  197. // console.log("onTerminated - ", t, i);
  198. })
  199. }
  200. function getList(num){
  201. if(heAnth !=null && heAnth != "null"&&heAnth.other_info !=null && heAnth.other_info != ""){
  202. var dsd = heAnth.other_info.split(",");
  203. let _opts = {
  204. url: helUrl + "/index.php?ctl=bruce&act=get_group_member_list",
  205. type:"POST"
  206. };
  207. let _data = {
  208. admin_id: 7903,
  209. udid: "11111111",
  210. g_id: dsd[0]
  211. };
  212. postAxios(_opts, _data, {}).then((res) =>{
  213. heList = [];
  214. if(res.status){
  215. if(res.data.length){
  216. setHelme(num, res.data, dsd);
  217. }else{
  218. _str=`<li class="text-center">暂无设备列表</li>`
  219. }
  220. }else{
  221. _str=`<li class="text-center">暂无设备列表</li>`
  222. }
  223. })
  224. }else{
  225. heList = [];
  226. $("#html_view").html(`<li class="text-center">暂无设备列表</li>`);
  227. }
  228. }
  229. function setHelme(num,_obj,_sji){
  230. let _str = "";
  231. _obj.forEach((item,ind)=>{
  232. if(item.device_id == _sji[1]){
  233. if(num == 0){
  234. _str += `<li class="flex align-center" data-yz="${item.device_id}">
  235. <img src="../img/${item.on_line==0?'maozi_no':'maozi'}.png" style="width:26px;margin-right:6px;"/>
  236. <div class="text-sm ${item.on_line==0?'text-rgb':'text-white'}">${item.nick}</div>
  237. </li>`;
  238. }else{
  239. if(item.on_line==0){
  240. $('#html_view li[data-yz="'+item.device_id+'] img').attr("src",'../img/maozi_no.png');
  241. $('#html_view li[data-yz="'+item.device_id+'] div').removeClass("text-white").addClass("text-rgb");
  242. }else{
  243. $('#html_view li[data-yz="'+item.device_id+'] img').attr("src",'../img/maozi.png');
  244. $('#html_view li[data-yz="'+item.device_id+'] div').removeClass("text-rgb").addClass("text-white");
  245. }
  246. }
  247. }
  248. });
  249. heList = _obj;
  250. if(num == 0){$("#html_view").html(_str);}
  251. }
  252. $(document).on("click","#html_view li",function(){
  253. let _id = $(this).data("yz");
  254. let _ind = heList.findIndex((i,ind) => {return i.device_id == _id});
  255. if(_ind != -1){
  256. nowHe = heList[_ind];
  257. }else{
  258. nowHe = null;
  259. }
  260. if(nowHe != null){
  261. if(nowHe.on_line==0){
  262. ShowLoading("安全帽离线中!",1);
  263. return;
  264. }else{
  265. $("#html_view li .text-sm").removeClass("act");
  266. $(this).find(".text-sm").addClass("act");
  267. $("#_View").html('<img src="../img/load.png" class="loadImg" alt=""><video style="width:100%;height:100%;" id="localMonitor" poster="" controls></video>');
  268. window.parent.postMessage({info:{act: "ma_open_rtsp",device_id: nowHe.device_id},change: "helemt"}, '*');
  269. }
  270. }
  271. })
  272. $(".tool").on("click",".icon-mic-on-full",function(){
  273. if(nowHe != null){
  274. if(nowHe.on_line==0){
  275. ShowLoading("安全帽离线中!",1);
  276. return;
  277. }else{
  278. $("._Call").css("display","block");
  279. }
  280. }else{
  281. ShowLoading("请先选择一个安全帽!",1);
  282. return;
  283. }
  284. })
  285. $(document).on("click","._Call .icon-cha",function(){
  286. $("._Call").css("display","none");
  287. document.getElementById("localMonitor").muted = !1;
  288. callErr();
  289. })
  290. $(".tool").on("click",".icon-zhuxiaoguanji",function(){
  291. if(nowHe != null){
  292. $("#html_view li .text-sm").removeClass("act");
  293. window.parent.postMessage({info:{act: "ma_stop_rtsp",device_id: nowHe.device_id},change: "helemt"}, '*');
  294. }else{
  295. ShowLoading("暂无安全帽在播放!",1);
  296. return;
  297. }
  298. })
  299. $(".tool").on("click",".icon-quanping",function(){
  300. let _elem = document.getElementById("_View");
  301. requesetFullScreen(_elem);
  302. })
  303. $(document).on("click","._Call img",function(){
  304. if($(this).attr("src").indexOf("yz4975") != -1){
  305. navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator
  306. .mozGetUserMedia || navigator.msGetUserMedia, navigator.getUserMedia ? navigator
  307. .getUserMedia({ audio: true, video: false },
  308. function(e) {
  309. console.log("已点击允许,开启成功",1);
  310. let _sjh = JSON.parse(localStorage.getItem("sipIds"));
  311. let _opts = {
  312. url: helUrl + "/index.php?ctl=device&act=get_user_sip_id&user_id="+nowHe.user_id,
  313. type:"GET"
  314. };
  315. let _data = {};
  316. getAxios(_opts, _data, {}).then((res) =>{
  317. if(res.status){
  318. device_sipId = res.data.sip_id;
  319. $("._Call .margin-top-sm").text("连接中....");
  320. window.parent.postMessage({info:{
  321. act: "ma_set_sip_info",
  322. user_id: nowHe.user_id,
  323. v_type: 0
  324. },change: "helemt"}, '*');
  325. }
  326. })
  327. }, function(e) {
  328. ShowLoading("获取麦克风失败!",1)
  329. }) : ShowLoading("获取麦克风权限失败!",1)
  330. }else{
  331. callErr();
  332. }
  333. })
  334. function callErr(){
  335. $("._Call img").attr("src","../img/voice.yz4975.png");
  336. if (sipsession) sipsession.terminate();
  337. clearInterval(setItime);
  338. setItime = null;
  339. setItimes = 0;
  340. $("._Call .margin-top-sm").text("未连接");
  341. $("._Call .text-lg").text("00:00:00");
  342. }
  343. function callSes(){
  344. $("._Call .margin-top-sm").text("已连接");
  345. $("._Call img").attr("src","../img/voice.yz8975.png");
  346. document.getElementById("localMonitor").muted = !1;
  347. // $("._helme").css("display","block");
  348. clearInterval(setItime);
  349. setItime = setInterval(()=>{
  350. setItimes += 1;
  351. let hou = Math.floor(setItimes/3600);
  352. let a = setItimes - (hou*3600);
  353. let min = Math.floor(a/60);
  354. let sed = a - (min*60);
  355. $("._Call .text-lg").text((hou>9?hou:'0'+hou)+":"+(min>9?min:'0'+min)+":"+(sed>9?sed:'0'+sed));
  356. },1000);
  357. }