Index_plan.html 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. <!doctype html>
  2. <html>
  3. <title>宝智达物联网平台</title>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  6. <script type="text/javascript" src="{{.OSS_Static}}/panel/jquery.min.js"></script>
  7. <script type="text/javascript" src="{{.OSS_Static}}/panel/layer/layer.js"></script>
  8. <script type="text/javascript" src="{{.OSS_Static}}/panel/context/context.js"></script>
  9. <script type="text/javascript" src="{{.OSS_Static}}/panel/drag.js"></script>
  10. <link rel="stylesheet" type="text/css" href="{{.OSS_Static}}/panel/context/context.standalone.css">
  11. <!--右侧菜单-->
  12. <link rel="stylesheet" type="text/css" href="{{.OSS_Static}}/panel/SlidePushMenus/css/default.css"/>
  13. <link rel="stylesheet" type="text/css" href="{{.OSS_Static}}/panel/SlidePushMenus/css/component.css"/>
  14. <script src="{{.OSS_Static}}/panel/SlidePushMenus/js/modernizr.custom.js"></script>
  15. <script src="{{.OSS_Static}}/panel/SlidePushMenus/js/classie.js"></script>
  16. <meta name="page-view-size" content="2920*1080" />
  17. <link rel="stylesheet" type="text/css" href="https://www.jq22.com/demo/csstoggle202003032356/style.css"/>
  18. <style type="text/css">
  19. html, body {
  20. margin: 0;
  21. padding: 0;
  22. overflow: hidden;
  23. }
  24. .transparent {
  25. filter: alpha(opacity=50);
  26. -moz-opacity: 0.5;
  27. -khtml-opacity: 0.5;
  28. /*opacity: 0.5; // 模糊*/
  29. }
  30. .box {
  31. width: 200px;
  32. height: 100px;
  33. cursor: move;
  34. position: absolute;
  35. top: 30px;
  36. left: 30px;
  37. z-index: 99;
  38. }
  39. .box .bg {
  40. width: 100%;
  41. height: 100%;
  42. background-color: orange;
  43. }
  44. .box .coor {
  45. width: 10px;
  46. height: 10px;
  47. overflow: hidden;
  48. cursor: se-resize;
  49. position: absolute;
  50. right: 0;
  51. bottom: 0;
  52. background-color: red;
  53. z-index: 9999999999;
  54. }
  55. .box .content {
  56. position: absolute;
  57. left: 50%;
  58. top: 50%;
  59. z-index: 99;
  60. text-align: center;
  61. font: bold 14px/1.5em simsun;
  62. }
  63. #debug {
  64. position: absolute;
  65. right: 10px;
  66. top: 10px;
  67. z-index: 88;
  68. border: 1px solid #ccc;
  69. width: 100px;
  70. height: 100px;
  71. background: #fff;
  72. }
  73. #toolbar {
  74. position: absolute;
  75. left: 10px;
  76. top: 10px;
  77. z-index: 88;
  78. }
  79. </style>
  80. <style>
  81. .help-left {
  82. width: 310px;
  83. font-family: 'microsoft YaHei';
  84. float: left;
  85. overflow-x: hidden;
  86. max-height: 100%;
  87. overflow-y: auto;
  88. }
  89. .menu {
  90. border-left: 1px solid #ccc;
  91. border-right: 1px solid #ccc;
  92. background-color:#FEFEFE;
  93. width:100%;
  94. float: left;
  95. }
  96. .menu:last-child {
  97. border-bottom: 1px solid #ccc;
  98. }
  99. .menu summary {
  100. height: 40px;
  101. line-height: 40px;
  102. text-indent: 10px;
  103. outline: none;
  104. font-size: 14px;
  105. font-weight: 700;
  106. border-top: 1px solid #ddd;
  107. background: -webkit-gradient(linear, left top, left bottom, color-stop(0, #FEFEFE), color-stop(1, #CCCCCC));
  108. cursor: pointer;
  109. width:100%;
  110. float: left;
  111. text-align: left;
  112. }
  113. .menu summary::-webkit-details-marker {
  114. display: none;
  115. }
  116. .menu .divul {
  117. padding: 10px 0;
  118. clear:left;
  119. width:100%;
  120. float: left;
  121. margin-left: 1px;
  122. }
  123. .menu .divulli {
  124. padding: 10px 0;
  125. width:97px;
  126. height: 100px;
  127. float: left;
  128. cursor: move;
  129. }
  130. .menu .divulli img {
  131. width:60px;
  132. height:60px;
  133. -webkit-user-select: none;
  134. cursor: default;
  135. pointer-events: none;
  136. }
  137. .menu .divulli div {
  138. width:97%;
  139. margin-top:-8px;
  140. overflow: hidden;
  141. text-overflow: ellipsis;
  142. white-space: nowrap;
  143. text-align: center;
  144. display:inline-block
  145. }
  146. .menu .divulli pre {
  147. width:0px;
  148. height:0px;
  149. overflow: hidden;
  150. }
  151. .menu .divulli:hover {
  152. background-color: #ececec;
  153. /*text-decoration: underline;*/
  154. }
  155. </style>
  156. <style>
  157. #tab-header span {
  158. padding: 0px 10px ;
  159. height: 38px;
  160. font-size: 20px;
  161. border-radius: 5px;
  162. padding-bottom: 10px;
  163. padding-top: 4px;
  164. }
  165. #tab-content .dom {
  166. display: none;
  167. }
  168. .open_layer_layui-layer-input {
  169. /*display: block;*/
  170. width: 220px;
  171. height: 30px;
  172. margin: 0 auto;
  173. line-height: 30px;
  174. padding: 0 5px;
  175. border: 1px solid #ccc;
  176. box-shadow: 1px 1px 5px rgba(0, 0, 0, .1) inset;
  177. color: #333;
  178. user-select: auto;
  179. }
  180. #open_layer_save_layer button {
  181. display: block;
  182. height: 38px;
  183. line-height: 38px;
  184. border: 1px solid transparent;
  185. padding: 0 18px;
  186. margin: 20px auto;
  187. background-color: #1E9FFF;;
  188. color: #fff;
  189. white-space: nowrap;
  190. text-align: center;
  191. font-size: 14px;
  192. border-radius: 2px;
  193. cursor: pointer;
  194. }
  195. .layer_tool_tab{
  196. position: absolute;
  197. top: 10px;
  198. right: 10px;
  199. /*background:#FFF;*/
  200. width: 80px;
  201. height: 30px;
  202. z-index:999999999999999999999;
  203. }
  204. .layer_tool_tab button {
  205. height: 28px;
  206. line-height: 28px;
  207. float: left;
  208. border: 1px solid transparent;
  209. padding: 0 18px;
  210. margin: 1px;
  211. background-color: #1E9FFF;;
  212. color: #fff;
  213. white-space: nowrap;
  214. text-align: center;
  215. font-size: 12px;
  216. border-radius: 2px;
  217. cursor: pointer;
  218. }
  219. .layer_tool_tab button:hover {
  220. background-color: #0092ff;;
  221. font-size: 12px;
  222. }
  223. .PanelCanvas_div{
  224. cursor: default;
  225. user-select: none;
  226. position: absolute;
  227. border: 2px dashed transparent;
  228. /*padding: 10px;*/
  229. }
  230. .PanelCanvas_rel_div{
  231. border: 2px dashed transparent;
  232. }
  233. .gridbackground { /*网格背景*/
  234. /*!*90度直线,100灰度,0.1透明度,5%宽度,0透明度背景*!*/
  235. /*background: linear-gradient(90deg, rgba(141, 128, 128, 0.1) 5%, transparent 0),*/
  236. /* !*0度直线,100灰度,0.1透明度,5%宽度,0透明度背景*!*/
  237. /*linear-gradient(rgba(100, 100, 100, 0.1) 5%, transparent 0);*/
  238. /*!*横纵向宽距*!*/
  239. /*background-size: 20px 20px;*/
  240. }
  241. </style>
  242. <style>
  243. /*#open_layer_Class select {*/
  244. /* margin-top: 2px;*/
  245. /* appearance: none;*/
  246. /* -webkit-appearance: none;*/
  247. /* -moz-appearance: none;*/
  248. /* border: 1px solid #e1e1e1;*/
  249. /* border-radius: 4px;*/
  250. /* height: 2.2em;*/
  251. /* padding: 0 24px 0 8px;*/
  252. /* font-family: inherit;*/
  253. /* color: #666;*/
  254. /* cursor: pointer;*/
  255. /* position: relative;*/
  256. /* background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAMAAADXqc3KAAAASFBMVEUAAAD////Nzc3Nzc3V1dXNzc3MzMzMzMzMzMzNzc3Ozs7j4+PMzMzMzMzMzMzPz8/MzMzNzc3Ozs7Ozs7MzMzNzc3Nzc3Nzc1mbvnCAAAAGHRSTlMAAymOBrtVs9RlPgnPltxPlWwvRJzBt+CSuXutAAAAM0lEQVR4nGMYBbgALycjiGIUZEaTEBLl4WJgEOZgZcLQwybOLsbHj800bhEBFqo7cfACACvdARau8cpxAAAAAElFTkSuQmCC') no-repeat right center;*/
  257. /*}*/
  258. .layui-layer-iframe iframe {
  259. display: block;
  260. width: 100%;
  261. height: 600px;
  262. }
  263. </style>
  264. </head>
  265. <body>
  266. <input hidden type="file" id="UpLoadFile"/>
  267. <div id="PanelCanvas" class="gridbackground" draggable="false" style="user-select: none;height: 4000px;width: 4000px;position: absolute;left: 0px;top: 0px;background-color: #f0f0f0">
  268. </div>
  269. {{if le .admin_r.Admin_power 6}}
  270. <div class="layer_tool_tab" >
  271. <button onclick="f_PaneView_E();">编辑</button>
  272. </div>
  273. <script>
  274. // 切换编辑
  275. function f_PaneView_E() {
  276. ToUrl = encodeURIComponent('../..'+window.location.pathname+'?T_viewid='+"{{.T_viewid}}")
  277. window.location.href='PaneView_E?T_viewid='+"{{.T_viewid}}"+"&ToUrl="+ToUrl
  278. }
  279. </script>
  280. {{end}}
  281. <!--页面 加载完成后执行-->
  282. <script>
  283. var OSS_Static = "{{.OSS_Static}}"
  284. var USER_UUID = "{{.USER_UUID}}" // 全局 USER_UUID
  285. var PanelCanvas_lock = true; //区块锁定标识
  286. var PanelCanvas_mode = 0; //0 自由模式 1 瀑瀑模式
  287. // 页面 加载完成后执行
  288. window.onload = function () {
  289. console.log("页面 加载完成后执行")
  290. $('img').on('dragstart', function(event) { event.preventDefault(); });
  291. f_PaneView_Get()
  292. }
  293. // 调整视图 大小
  294. function win_show() {
  295. PC_width = parseFloat($("#PanelCanvas").css("width"))
  296. PC_height = parseFloat($("#PanelCanvas").css("height"))
  297. setInterval(function(){
  298. Width = $(window).width();
  299. Height = $(window).height();
  300. //
  301. // console.log("W:",Width,Height)
  302. // console.log("PC:",PC_width,PC_height)
  303. //
  304. // console.log("X:",Width/PC_width)
  305. // console.log("Y:",Height/PC_height)
  306. //
  307. $("#PanelCanvas").css({
  308. 'transform': 'scale('+Width/PC_width+', '+Height/PC_height+')',
  309. 'transform-origin': '0 0'
  310. });
  311. },500);
  312. }
  313. </script>
  314. <!--移动面板-->
  315. <script>
  316. //创建拖拽方法
  317. PanelCanvas_move = true
  318. // $("#PanelCanvas").mousedown(function(e){
  319. // if(!PanelCanvas_move){return;}
  320. //
  321. // var canvas = $(this);
  322. // var pos = $(this).position();
  323. // this.posix = {'x': e.pageX - pos.left, 'y': e.pageY - pos.top};
  324. // $.extend(document, {'move': true, 'move_target': this, 'call_down' : function(e, posix){
  325. // canvas.css({
  326. // 'cursor': 'move',
  327. // 'top': e.pageY - posix.y,
  328. // 'left': e.pageX - posix.x
  329. // });
  330. // }, 'call_up' : function(){
  331. // canvas.css('cursor', 'default');
  332. // }});
  333. // })
  334. function f_PanelCanvas_onmouseover() {
  335. // console.log("进入")
  336. PanelCanvas_move = false
  337. }
  338. function f_PanelCanvas_onmouseout() {
  339. // console.log("出去")
  340. PanelCanvas_move = true
  341. }
  342. </script>
  343. <!--数据缓存-->
  344. <script>
  345. var Public_Value_map = new Map();
  346. //读取 同步缓存数据
  347. function f_Public_Value_get(tab) {
  348. if(Public_Value_map.has(tab)){ //判断映射是否存在
  349. return Public_Value_map.get(tab),true
  350. }
  351. return 0,false
  352. }
  353. //设置 同步缓存数据
  354. function f_Public_Value_set(tab,value) {
  355. Public_Value_map.set(tab,value); //添加设置映射
  356. }
  357. </script>
  358. <!--画板入口-->
  359. <script src="{{.OSS_Static}}/panel/PanelCanvas.js"></script>
  360. <script>
  361. // "#canvas"
  362. //
  363. // V_PanelCanvas1 =
  364. // V_PanelCanvas2 = f_canvas2(500,500,"{{.OSS_Static}}/images/demo_picture.jpg")
  365. var V_PanelCanvas_Map = new Map();
  366. //
  367. // V_PanelCanvas = new f_canvas2(500, 500, "{{.OSS_Static}}/images/demo_picture.jpg", 1, "{\"sn_sn\":\"866222050911984\",\"tab_tab\":\"v_do_2\",\"text1_text\":\"替换的文本\"}")
  368. // V_PanelCanvas_Map.set(V_PanelCanvas.PanelCanvas_id,V_PanelCanvas)
  369. // //测试加载
  370. // var loadData = [
  371. //
  372. // {text: "A0000001v_xx_7", rotate: 0, color: "rgb(255, 165, 0)", height: 70, width: 77, pageX: 89, pageY: 39}];
  373. // $.each(loadData, function (i, row) {
  374. // V_PanelCanvas.F_CreateBox(row);
  375. // });
  376. //
  377. //
  378. // V_PanelCanvas = new f_canvas2(500, 500, "{{.OSS_Static}}/images/demo_picture.jpg", 1, "{\"sn_sn\":\"866222050911984\"}")
  379. // V_PanelCanvas_Map.set(V_PanelCanvas.PanelCanvas_id,V_PanelCanvas)
  380. // var loadData = [
  381. // {text: "A0000001v_xx_1", rotate: 1, color: "rgb(255, 255, 0)", height: 70, width: 77, pageX: 29, pageY: 64}
  382. // ];
  383. // $.each(loadData, function (i, row) {
  384. // V_PanelCanvas.F_CreateBox(row);
  385. // });
  386. // var V_PanelCanvas1 = new Function(f_canvas(500,500,"{{.OSS_Static}}/images/demo_picture.jpg"))
  387. // var V_PanelCanvas2 = new Function(f_canvas(500,500,"{{.OSS_Static}}/images/demo_picture.jpg"))
  388. Height = $(window).height();
  389. $("#PanelTool_Device_List").css("height",(Height-125)+"px")
  390. function f_PaneView_Get() {
  391. $.ajax({
  392. type: 'POST',
  393. url: '../../Panel/PaneView_Get',//发送请求
  394. data: {
  395. T_viewid: "{{.T_viewid}}",
  396. },
  397. success: function (result) {
  398. console.log(result)
  399. if (result.Code == 200) {
  400. T_PanelCanvas = result.Data
  401. PanelCanvas_mode = T_PanelCanvas.T_mode
  402. f_PanelCanvas_mode_0(result)
  403. switch (PanelCanvas_mode) {
  404. case 0:
  405. $("#PanelCanvas_mode").html("自由模式")
  406. break
  407. case 1:
  408. $("#PanelCanvas_mode").html("瀑瀑模式")
  409. break
  410. case 2:
  411. $("#PanelCanvas_mode").html("满屏模式")
  412. win_show() // 调整视图 大小
  413. break
  414. }
  415. $("#PanelCanvas_wh").html("["+T_PanelCanvas.T_width+"*"+T_PanelCanvas.T_height+"]")
  416. } else {
  417. layer.msg('视图获取错误!');
  418. setTimeout(function(){
  419. //要执行的代码
  420. f_PaneView_Get()
  421. },2000);
  422. }
  423. }
  424. });
  425. }
  426. // 自由模式
  427. function f_PanelCanvas_mode_0(result) {
  428. T_PanelCanvas = result.Data
  429. // CSS 样式
  430. $('#PanelCanvas').css({
  431. width: T_PanelCanvas.T_width + "px",
  432. height: T_PanelCanvas.T_height + "px"
  433. });
  434. // CSS 自定义样式
  435. f_Up_css(T_PanelCanvas.T_css,"PanelCanvas")
  436. // 加载 组件
  437. if(result.Data.T_text.length < 10 ){
  438. return;
  439. }
  440. PaneView_json = JSON.parse(result.Data.T_text)
  441. for(var i = 0;i < PaneView_json.length;i++){
  442. Layer_Mould_json = PaneView_json[i]
  443. console.log("创建面板 PaneView_json",Layer_Mould_json)
  444. V_PanelCanvas = new f_canvas2(Layer_Mould_json.PanelCanvas_top,Layer_Mould_json.PanelCanvas_left,Layer_Mould_json.PanelCanvas_width, Layer_Mould_json.PanelCanvas_height, Layer_Mould_json.PanelCanvas_z, JSON.stringify(Layer_Mould_json.PanelCanvas_map), Layer_Mould_json.PanelCanvas_css)
  445. V_PanelCanvas_Map.set(V_PanelCanvas.PanelCanvas_id,V_PanelCanvas)
  446. for(var row_i = 0; row_i < Layer_Mould_json.Data_list.length; row_i++){
  447. // rowjson = Layer_Mould_json.Data_list[i]
  448. console.log("row json+ ",Layer_Mould_json.Data_list[row_i])
  449. V_PanelCanvas.F_CreateBox(Layer_Mould_json.Data_list[row_i]);
  450. }
  451. }
  452. }
  453. // 瀑瀑模式
  454. function f_PanelCanvas_mode_1(result){
  455. T_PanelCanvas = result.Data
  456. // CSS 样式
  457. $('#PanelCanvas').css({
  458. width: '100%',
  459. height: '100%',
  460. overflowX: "hidden",
  461. overflowY: "auto",
  462. });
  463. // CSS 自定义样式
  464. f_Up_css(T_PanelCanvas.T_css,"PanelCanvas")
  465. // 加载 组件
  466. if(result.Data.T_text.length < 10 ){
  467. return;
  468. }
  469. PaneView_json = JSON.parse(result.Data.T_text)
  470. for(var i = 0;i < PaneView_json.length;i++){
  471. Layer_Mould_json = PaneView_json[i]
  472. console.log("创建面板 PaneView_json",Layer_Mould_json)
  473. V_PanelCanvas = new f_canvas2(Layer_Mould_json.PanelCanvas_top,Layer_Mould_json.PanelCanvas_left,Layer_Mould_json.PanelCanvas_width, Layer_Mould_json.PanelCanvas_height, Layer_Mould_json.PanelCanvas_z, JSON.stringify(Layer_Mould_json.PanelCanvas_map), Layer_Mould_json.PanelCanvas_css)
  474. V_PanelCanvas_Map.set(V_PanelCanvas.PanelCanvas_id,V_PanelCanvas)
  475. for(var row_i = 0; row_i < Layer_Mould_json.Data_list.length; row_i++){
  476. // rowjson = Layer_Mould_json.Data_list[i]
  477. console.log("row json+ ",Layer_Mould_json.Data_list[row_i])
  478. V_PanelCanvas.F_CreateBox(Layer_Mould_json.Data_list[row_i]);
  479. }
  480. }
  481. }
  482. function f_PaneView_Save(T_text) {
  483. $.ajax({
  484. type: 'POST',
  485. url: '../../Panel/PaneView_Post',//发送请求
  486. data: {
  487. T_viewid: "{{.T_viewid}}",
  488. T_text: T_text,
  489. T_mode: T_PanelCanvas.T_mode,
  490. T_width: T_PanelCanvas.T_width,
  491. T_height: T_PanelCanvas.T_height,
  492. T_css: T_PanelCanvas.T_css,
  493. T_data: T_PanelCanvas.T_data,
  494. },
  495. success: function (result) {
  496. console.log(result)
  497. if (result.Code == 200) {
  498. layer.msg('保存成功!');
  499. } else {
  500. layer.msg('保存失败!');
  501. }
  502. }
  503. });
  504. }
  505. </script>
  506. <!--发布订阅模式-->
  507. <script>
  508. // 控制中心
  509. var pubSub = {
  510. list: {},
  511. // 订阅
  512. subscribe: function(key, fn) {
  513. console.log("加入:",key)
  514. Public_V = f_Public_Value_get(key)
  515. if (Public_V.is){
  516. this.publish(key,Public_V.V)
  517. }
  518. if (!this.list[key]) this.list[key] = [];
  519. this.list[key].push(fn);
  520. key_list = key.split("/")
  521. if(key_list[0].length > 3){
  522. NewSubscribe(key_list[0]) // 订阅SN
  523. }
  524. },
  525. //取消订阅
  526. unsubscribe: function(key, fn) {
  527. let fnList = this.list[key];
  528. if (!fnList) return false;
  529. if (!fn) { // 不传入指定的方法,清空所用 key 下的订阅
  530. fnList && (fnList.length = 0);
  531. } else {
  532. fnList.forEach((item, index) => {
  533. item === fn && fnList.splice(index, 1);
  534. });
  535. }
  536. },
  537. // 发布
  538. publish: function(key, ...args) {
  539. if (this.list[key]){
  540. for (let fn of this.list[key]) fn.call(this, ...args);
  541. }
  542. }
  543. }
  544. function f_Data_Read(T_sn) {
  545. T_sn_list = T_sn.split("-")
  546. $.ajax({
  547. type: 'POST',
  548. url: '../../Data/Device_Sensor',//发送请求
  549. data: {
  550. T_sn: T_sn_list[0],
  551. T_id: T_sn_list[1],
  552. },
  553. success: function (result) {
  554. console.log(result)
  555. if (result.Code == 200) {
  556. for (const [key, value] of Object.entries(result.Data)) {
  557. // console.log(key,value);
  558. pubSub.publish(T_sn+"/"+key, value);
  559. f_Public_Value_set(T_sn+"/"+key, value)
  560. }
  561. }
  562. }
  563. });
  564. }
  565. </script>
  566. <!--websocket-->
  567. <script src="{{.OSS_Static}}/js/jquery.cookie.min.js"></script>
  568. <script>
  569. var websocket = null;
  570. //判断当前浏览器是否支持WebSocket
  571. run_WebSocket()
  572. function run_WebSocket() {
  573. console.log("run_WebSocket");
  574. if ('WebSocket' in window) {
  575. url = window.location.host
  576. if (url.indexOf("baozhida") != -1) {
  577. websocket = new WebSocket("wss://" + window.location.host + "/wsPanel/join?User_tokey=" + $.cookie('User_tokey'));
  578. } else {
  579. websocket = new WebSocket("ws://" + window.location.host + "/wsPanel/join?User_tokey=" + $.cookie('User_tokey'));
  580. }
  581. } else {
  582. alert('Dont support websocket')
  583. }
  584. }
  585. //连接成功建立的回调方法
  586. websocket.onopen = function () {
  587. console.log("websocket open");
  588. // get_DeviceCh_List(0)
  589. setTimeout(function(){
  590. SubscribeSend()
  591. },2000);
  592. // send("{\"Sn\":\"" + getQueryString("Sn") + "\"}")
  593. };
  594. //接收到消息的回调方法
  595. websocket.onmessage = function (event) {
  596. // console.log("websocket:",event.data)
  597. var obj = JSON.parse(event.data);
  598. console.log("websocket:",obj)
  599. if(obj.Code == 200){
  600. return
  601. }
  602. sn_id = obj.T_sn+"-"+obj.T_id
  603. for (const [key, value] of Object.entries(obj)) {
  604. // console.log(key,value);
  605. pubSub.publish(sn_id+"/"+key, value);
  606. f_Public_Value_set(sn_id+"/"+key, value)
  607. }
  608. // pubSub.publish(sn_id+'/'+obj.tab, obj.value);
  609. // // op:类型u8, //操作 0-主动上传 1-读 2- 写 3-成功 4-失败
  610. // switch (obj.op) {
  611. // case 3:
  612. // Del_Mid(obj.mid)
  613. // break;
  614. // case 4:
  615. // Err_Mid(obj.mid)
  616. // break;
  617. // }
  618. // console.log(obj.sn,"进入。。 Device_Sn = ",obj.sn)
  619. // Pu_DeviceCh_List_g(obj.sn,obj.tab,obj.value)
  620. //
  621. // if (obj.type == 0) {
  622. // // console.log("data.length:",obj.sensor.length)
  623. //
  624. // Pu_DeviceCh_List_g(obj.sn, obj.sensor)
  625. //
  626. // if (obj.type == 0) {
  627. // $("#T_time").html("刷新时间:" + dateChangeFormat_x('YYYY-mm-dd HH:MM:SS', obj.sensor[0].UT) + " 上传刷新间隔:" + Math.abs(timestamp_V - obj.sensor[0].UT).toFixed(0) + "s")
  628. //
  629. // // console.log(timestamp_V, obj.sensor[0].UT, " 刷新间隔:" + (timestamp_V - obj.sensor[0].UT) + "s")
  630. // timestamp_V = obj.sensor[0].UT
  631. //
  632. // }
  633. //
  634. //
  635. // }
  636. };
  637. //连接关闭的回调方法
  638. websocket.onclose = function () {
  639. console.log("close");
  640. // setTimeout(function () {
  641. // //要执行的代码
  642. // run_WebSocket();
  643. // }, 2000);
  644. };
  645. //连接发生错误的回调方法
  646. websocket.onerror = function () {
  647. console.log("error");
  648. setTimeout(function () {
  649. //要执行的代码
  650. run_WebSocket();
  651. }, 2000);
  652. };
  653. //监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
  654. window.onbeforeunload = function () {
  655. websocket.close();
  656. console.log("窗口关闭事件");
  657. };
  658. //关闭连接
  659. function closeWebSocket() {
  660. websocket.close();
  661. console.log("onbeforeunload");
  662. window.clearInterval(intervalId);
  663. }
  664. //发送消息
  665. function send(message) {
  666. websocket.send(message);
  667. }
  668. var SbscribeMap = new Set();
  669. // 订阅 SN
  670. function Subscribe(SN) {
  671. if(!SbscribeMap.has(SN)){
  672. console.log("加入订阅:",SN)
  673. SbscribeMap.add(SN)
  674. }
  675. //
  676. }
  677. // 订阅 SN
  678. function NewSubscribe(SN) {
  679. if(!SbscribeMap.has(SN)){
  680. console.log("加入订阅:",SN)
  681. SbscribeMap.add(SN)
  682. send("{ \"Sn\":\""+SN+"\"}")
  683. f_Data_Read(SN)
  684. }
  685. //
  686. }
  687. // 订阅 SN
  688. function SubscribeSend() {
  689. for (var SN of SbscribeMap) { // 遍历Set
  690. send("{ \"Sn\":\""+SN+"\"}")
  691. }
  692. console.log("开始订阅:",SbscribeMap)
  693. }
  694. </script>
  695. </body>
  696. </html>