From d2fd7cfc1a426baabe4fac47f88b4db03432e22b Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期二, 31 十月 2023 12:32:32 +0800
Subject: [PATCH] #websocket

---
 src/main/webapp/views/shuttle2.html |  207 ++++++++++++++++++++++++++++-----------------------
 1 files changed, 115 insertions(+), 92 deletions(-)

diff --git a/src/main/webapp/views/shuttle2.html b/src/main/webapp/views/shuttle2.html
index 7ca9591..c1cdc91 100644
--- a/src/main/webapp/views/shuttle2.html
+++ b/src/main/webapp/views/shuttle2.html
@@ -48,6 +48,7 @@
                                 <th>鏁呴殰鐘舵��</th>
                                 <th>鏁呴殰鐮�</th>
                                 <th>绠″埗鐘舵��</th>
+                                <th>浣庣數閲�</th>
                             </tr>
                         </thead>
                         <tbody>
@@ -229,18 +230,46 @@
     var shuttleMsgTableFullRows = 0;
     // 鍒濆鍖�
     var shuttleOutputDom = document.getElementById("shuttle-output");
+
+    var ws = new WebSocket("ws://" + window.location.host + baseUrl + "/shuttle/websocket");
+
+    //褰揥ebSocket鍒涘缓鎴愬姛鏃讹紝瑙﹀彂onopen浜嬩欢
+    ws.onopen = function(){
+        console.log("open");
+    }
+
+    //褰撳鎴风鏀跺埌鏈嶅姟绔彂鏉ョ殑娑堟伅鏃讹紝瑙﹀彂onmessage浜嬩欢
+    ws.onmessage = function(e){
+        const result = JSON.parse(e.data);
+        if (result.url == "/shuttle/table/shuttle/state") {
+            setShuttleStateInfo(JSON.parse(result.data))
+            setShuttleMsgInfo(JSON.parse(result.data))
+        }else if (result.url == "/shuttle/output/shuttle") {
+            setShuttleOutput(JSON.parse(result.data))
+        }
+        // console.log(e.data,result);
+    }
+
+    //褰撳鎴风鏀跺埌鏈嶅姟绔彂閫佺殑鍏抽棴杩炴帴璇锋眰鏃讹紝瑙﹀彂onclose浜嬩欢
+    ws.onclose = function(e){
+        console.log("close");
+    }
+
+    //濡傛灉鍑虹幇杩炴帴銆佸鐞嗐�佹帴鏀躲�佸彂閫佹暟鎹け璐ョ殑鏃跺�欒Е鍙憃nerror浜嬩欢
+    ws.onerror = function(e) {
+        console.log(error);
+    }
+
     $(document).ready(function() {
         initShuttleStateTable();
         getShuttleStateInfo();
         initShuttleMsgTable();
-        getShuttleMsgInfo();
         operatorBlockShow();
         setShuttleRadio();
     });
 
     setInterval(function () {
-        getShuttleStateInfo()
-        getShuttleMsgInfo();
+        getShuttleStateInfo();
     },1000)
     setInterval(function () {
         getShuttleOutput();
@@ -318,107 +347,96 @@
 
     // 鍥涘悜绌挎杞︿俊鎭〃鑾峰彇 ---- 琛ㄤ竴
     function getShuttleStateInfo() {
+        sendWs("{\"url\":\"/shuttle/table/shuttle/state\",\"data\":{}}")
+    }
+
+    // 鍥涘悜绌挎杞︿俊鎭〃璁剧疆 ---- 琛ㄤ竴
+    function setShuttleStateInfo(res) {
         let tableEl = $('#shuttle-state-table');
-        $.ajax({
-            url: baseUrl+ "/shuttle/table/shuttle/state",
-            headers: {'token': localStorage.getItem('token')},
-            method: 'POST',
-            success: function (res) {
-                if (res.code === 200){
-                    let table = res.data;
-                    if (table.length > shuttleStateTableBlankRows && table.length !== shuttleStateTableFullRows) {
-                        initShuttleStateTable(table.length-shuttleStateTableBlankRows);
-                        shuttleStateTableFullRows = table.length;
-                    }
-                    for (let i=1;i<=table.length;i++){
-                        // $("#mode-"+table[i-1].shuttleNo).html(table[i-1].statusVal===0?'鑱旀満':'鑴辨満');
-                        let tr = tableEl.find("tr").eq(i);
-                        setVal(tr.children("td").eq(0), table[i-1].shuttleNo);
-                        setVal(tr.children("td").eq(1), table[i-1].protocolStatus$);
-                        setVal(tr.children("td").eq(2), table[i-1].free$);
-                        setVal(tr.children("td").eq(3), table[i-1].workingMode$);
-                        setVal(tr.children("td").eq(4), table[i-1].point$);
-                        setVal(tr.children("td").eq(5), table[i-1].point$$);
-                        setVal(tr.children("td").eq(6), table[i-1].powerPercent$);
-                        setVal(tr.children("td").eq(7), table[i-1].speed);
-                        setVal(tr.children("td").eq(8), table[i-1].loadState$);
-                        setVal(tr.children("td").eq(9), table[i-1].liftPosition$);
-                        setVal(tr.children("td").eq(10), table[i-1].runDir$);
-                        setVal(tr.children("td").eq(11), table[i-1].runDir2$);
-                        setVal(tr.children("td").eq(12), table[i-1].chargState$);
-                        setVal(tr.children("td").eq(13), table[i-1].errState$);
-                        setVal(tr.children("td").eq(14), table[i-1].errCode$);
-                        setVal(tr.children("td").eq(15), table[i-1].suspendState$);
-                    }
-                } else if (res.code === 403){
-                    window.location.href = baseUrl+"/login";
-                }  else {
-                    console.log(res.msg);
-                }
+        if (res.code === 200){
+            let table = res.data;
+            if (table.length > shuttleStateTableBlankRows && table.length !== shuttleStateTableFullRows) {
+                initShuttleStateTable(table.length-shuttleStateTableBlankRows);
+                shuttleStateTableFullRows = table.length;
             }
-        });
+            for (let i=1;i<=table.length;i++){
+                // $("#mode-"+table[i-1].shuttleNo).html(table[i-1].statusVal===0?'鑱旀満':'鑴辨満');
+                let tr = tableEl.find("tr").eq(i);
+                setVal(tr.children("td").eq(0), table[i-1].shuttleNo);
+                setVal(tr.children("td").eq(1), table[i-1].protocolStatus$);
+                setVal(tr.children("td").eq(2), table[i-1].free$);
+                setVal(tr.children("td").eq(3), table[i-1].workingMode$);
+                setVal(tr.children("td").eq(4), table[i-1].point$);
+                setVal(tr.children("td").eq(5), table[i-1].point$$);
+                setVal(tr.children("td").eq(6), table[i-1].powerPercent$);
+                setVal(tr.children("td").eq(7), table[i-1].speed);
+                setVal(tr.children("td").eq(8), table[i-1].loadState$);
+                setVal(tr.children("td").eq(9), table[i-1].liftPosition$);
+                setVal(tr.children("td").eq(10), table[i-1].runDir$);
+                setVal(tr.children("td").eq(11), table[i-1].runDir2$);
+                setVal(tr.children("td").eq(12), table[i-1].chargState$);
+                setVal(tr.children("td").eq(13), table[i-1].errState$);
+                setVal(tr.children("td").eq(14), table[i-1].errCode$);
+                setVal(tr.children("td").eq(15), table[i-1].suspendState$);
+                setVal(tr.children("td").eq(16), table[i-1].lowerPower);
+            }
+        } else if (res.code === 403){
+            window.location.href = baseUrl+"/login";
+        }  else {
+            console.log(res.msg);
+        }
     }
 
     // 鍥涘悜绌挎杞︽暟鎹〃鑾峰彇 ---- 琛ㄤ簩
-    function getShuttleMsgInfo() {
+    function setShuttleMsgInfo(res) {
         let tableEl = $('#shuttle-msg-table');
-        $.ajax({
-            url: baseUrl+ "/shuttle/table/shuttle/state",
-            headers: {'token': localStorage.getItem('token')},
-            method: 'POST',
-            success: function (res) {
-                if (res.code === 200){
-                    var table = res.data;
-                    if (table.length > shuttleMsgTableBlankRows && table.length !== shuttleMsgTableFullRows) {
-                        initShuttleMsgTable(table.length-shuttleMsgTableBlankRows);
-                        shuttleMsgTableFullRows = table.length;
-                    }
-                    for (var i=1;i<=table.length;i++){
-                        var tr = tableEl.find("tr").eq(i);
-                        setVal(tr.children("td").eq(0), table[i-1].shuttleNo);
-                        setVal(tr.children("td").eq(1), table[i-1].taskNo);
-                        setVal(tr.children("td").eq(2), table[i-1].sourceLocNo);
-                        setVal(tr.children("td").eq(3), table[i-1].locNo);
-                        setVal(tr.children("td").eq(4), table[i-1].maxCellVoltage$);
-                        setVal(tr.children("td").eq(5), table[i-1].minCellVoltage$);
-                        setVal(tr.children("td").eq(6), table[i-1].voltage$);
-                        setVal(tr.children("td").eq(7), table[i-1].chargeCycleTimes);
-                        setVal(tr.children("td").eq(8), table[i-1].surplusQuantity);
-                        setVal(tr.children("td").eq(9), table[i-1].countQuantity);
-                        setVal(tr.children("td").eq(10), table[i-1].statusSum ? table[i - 1].statusSum.mileage : '');
-                        setVal(tr.children("td").eq(11), table[i-1].pakMk$);
-                        setVal(tr.children("td").eq(12), table[i-1].currentLocNo);
-                        setVal(tr.children("td").eq(13), table[i-1].token);
-                        if (table[i-1].shuttleNo == parseInt($('input[name="shuttleSelect"]:checked').val())) {
-                            $("#runSpeedText").text(table[i-1].runSpeed)
-                            $("#chargeLineText").text(table[i-1].chargeLine + "%")
-                        }
-                    }
-                } else if (res.code === 403){
-                    window.location.href = baseUrl+"/login";
-                }  else {
-                    console.log(res.msg);
+        if (res.code === 200){
+            var table = res.data;
+            if (table.length > shuttleMsgTableBlankRows && table.length !== shuttleMsgTableFullRows) {
+                initShuttleMsgTable(table.length-shuttleMsgTableBlankRows);
+                shuttleMsgTableFullRows = table.length;
+            }
+            for (var i=1;i<=table.length;i++){
+                var tr = tableEl.find("tr").eq(i);
+                setVal(tr.children("td").eq(0), table[i-1].shuttleNo);
+                setVal(tr.children("td").eq(1), table[i-1].taskNo);
+                setVal(tr.children("td").eq(2), table[i-1].sourceLocNo);
+                setVal(tr.children("td").eq(3), table[i-1].locNo);
+                setVal(tr.children("td").eq(4), table[i-1].maxCellVoltage$);
+                setVal(tr.children("td").eq(5), table[i-1].minCellVoltage$);
+                setVal(tr.children("td").eq(6), table[i-1].voltage$);
+                setVal(tr.children("td").eq(7), table[i-1].chargeCycleTimes);
+                setVal(tr.children("td").eq(8), table[i-1].surplusQuantity);
+                setVal(tr.children("td").eq(9), table[i-1].countQuantity);
+                setVal(tr.children("td").eq(10), table[i-1].statusSum ? table[i - 1].statusSum.mileage : '');
+                setVal(tr.children("td").eq(11), table[i-1].pakMk$);
+                setVal(tr.children("td").eq(12), table[i-1].currentLocNo);
+                setVal(tr.children("td").eq(13), table[i-1].token);
+                if (table[i-1].shuttleNo == parseInt($('input[name="shuttleSelect"]:checked').val())) {
+                    $("#runSpeedText").text(table[i-1].runSpeed)
+                    $("#chargeLineText").text(table[i-1].chargeLine + "%")
                 }
             }
-        });
+        } else if (res.code === 403){
+            window.location.href = baseUrl+"/login";
+        }  else {
+            console.log(res.msg);
+        }
     }
 
     // 绌挎杞︽棩蹇楄緭鍑� -----------------------------------------------------------------------
     function getShuttleOutput() {
-        $.ajax({
-            url: baseUrl + "/shuttle/output/shuttle",
-            headers: {'token': localStorage.getItem('token')},
-            method: 'POST',
-            success: function (res) {
-                if (res.code === 200) {
-                    shuttleOutput(res.data);
-                } else if (res.code === 403) {
-                    window.location.href = baseUrl + "/login";
-                } else {
-                    console.log(res.msg);
-                }
-            }
-        })
+        sendWs("{\"url\":\"/shuttle/output/shuttle\",\"data\":{}}")
+    }
+
+    function setShuttleOutput(res) {
+        if (res.code === 200) {
+            shuttleOutput(res.data);
+        } else if (res.code === 403) {
+            window.location.href = baseUrl + "/login";
+        } else {
+            console.log(res.msg);
+        }
     }
 
     // 浠诲姟鎸囦护涓嬪彂
@@ -622,5 +640,10 @@
         layer.close(layerDetl);
     })
 
+    function sendWs(message) {
+        if (ws.readyState == WebSocket.OPEN) {
+            ws.send(message)
+        }
+    }
 
 </script>

--
Gitblit v1.9.1