From 8d58c40af40493bd5f066cbfdb1ffd9f113c22fc Mon Sep 17 00:00:00 2001
From: 严智鑫 <test>
Date: 星期三, 03 九月 2025 16:37:21 +0800
Subject: [PATCH] 1.前端推数据使用了最新的webSokect客户现场总是掉线,修改为接口方式 2.设备交互整体增加异常处理以及PLC连接方式改为短连接

---
 UI-Project/src/views/FlipSlice/flipSlice.vue |  423 +++++++++++++++++++++++++++++-----------------------
 1 files changed, 233 insertions(+), 190 deletions(-)

diff --git a/UI-Project/src/views/FlipSlice/flipSlice.vue b/UI-Project/src/views/FlipSlice/flipSlice.vue
index 804ac8d..c035cac 100644
--- a/UI-Project/src/views/FlipSlice/flipSlice.vue
+++ b/UI-Project/src/views/FlipSlice/flipSlice.vue
@@ -26,81 +26,124 @@
 const pageParams=userInfo.user.menus[0].pages[2].params;
 const pageParamsJson=JSON.parse(pageParams);
 const machineId = pageParamsJson.machineId;//褰撳墠椤甸潰鐨勮澶嘔D
+
+
 console.log(machineId);
 //const machineId = 26;//褰撳墠椤甸潰鐨勮澶嘔D
 var errorScanId;
 var errorOpen=false;
-//浣跨敤WebSocket鏂瑰紡灞曠ず鏁版嵁
-let socket = null;
-let socketDownLineTask = null;
-const socketUrl = `ws://${WebSocketHost}:${host}/api/deviceInteraction/api/talk/flipSlice`;
-const socketDownLineTaskUrl = `ws://${WebSocketHost}:${host}/api/deviceInteraction/api/talk/downLineTask`;
-// 瀹氫箟娑堟伅澶勭悊鍑芥暟锛屾洿鏂� receivedData 鍙橀噺
-const handleMessage = (data) => {
-  // 鏇存柊 tableData 鐨勬暟鎹�
-  let dataJson = data[""+machineId][0];
+// 鏂板锛氬畾鏃朵换鍔D锛堢敤浜庢竻闄ゅ畾鏃跺櫒锛�
+const timerId = ref(null);
+/**
+ * 鍗曟鎺ュ彛璇锋眰鍑芥暟
+ * 浣滅敤锛氳皟鐢ㄥ悗绔帴鍙o紝鑾峰彇鏁版嵁鍚庝氦缁� handleMessage 澶勭悊
+ */
+const fetchMachineData = async () => {
+  try {
+    // 浠诲姟鏁版嵁
+    request.post("/deviceInteraction/tasking/findMachineTask",{
+          "id": machineId
+        }).then((res) => { // 鏇挎崲涓轰綘鐨凙PI绔偣
+      if (res.code === 200) {
+        // 鍋囪鍚庣杩斿洖鐨勬暟鎹牸寮忎笌 handleMessage 棰勬湡涓�鑷�
+        const responseData = res.data || {};
+        loadData.value =responseData;
+      }
+    });
 
-  loadData.value = dataJson.taskingList[0];
-  findMachine.value = dataJson.machine[0];
-  sendRecords.value = dataJson.sendRecords[0].reverse();
-  if(findMachine.value.remark.toString()!=errorScanId&&findMachine.value.remark.toString()!=""&&findMachine.value.remark.toString()!=undefined){
-    //鏈夋暟鎹氨寮圭獥 
-    if(errorOpen){
-      ElMessageBox.close();
-    }
-    errorScanId=findMachine.value.remark.toString();
-    errorInfoFuntion(findMachine.value.remark.toString());
-    errorOpen=true;
-  }else if(findMachine.value.remark.toString()==""&&errorOpen){
-    ElMessageBox.close();
-    errorOpen=false;
+    // 璁惧鏁版嵁
+    request.post("/deviceInteraction/machine/findMachine",{
+      "id": machineId
+    }).then((res) => { // 鏇挎崲涓轰綘鐨凙PI绔偣
+      if (res.code === 200) {
+        // 鍋囪鍚庣杩斿洖鐨勬暟鎹牸寮忎笌 handleMessage 棰勬湡涓�鑷�
+        const responseData = res.data || {};
+        findMachine.value =responseData;
+        if (Object.keys(findMachine.value).length === 0){
+          if(findMachine.value.remark.toString()!=errorScanId&&findMachine.value.remark.toString()!=""&&findMachine.value.remark.toString()!=undefined){
+            //鏈夋暟鎹氨寮圭獥
+            if(errorOpen){
+              ElMessageBox.close();
+            }
+            errorScanId=findMachine.value.remark.toString();
+            errorInfoFuntion(findMachine.value.remark.toString());
+            errorOpen=true;
+          }else if(findMachine.value.remark.toString()==""&&errorOpen){
+            ElMessageBox.close();
+            errorOpen=false;
+          }
+        }
+      }
+    });
+    // 绾夸笅鏁版嵁
+  } catch (err) {
+    console.error('瀹氭椂璇锋眰鍚庣鎺ュ彛澶辫触锛�', err);
   }
-   
 };
-// 瀹氫箟娑堟伅澶勭悊鍑芥暟锛屾洿鏂� receivedData 鍙橀噺
-const handleMessage2 = (data) => {
-  downLineTask.value = data.downLineTask[0];
-  // console.log(data);
+
+/**
+ * 鍚姩瀹氭椂鍣�
+ * @param {number} interval - 瀹氭椂闂撮殧锛堟绉掞紝榛樿500ms锛屼笌鍚庣浠诲姟棰戠巼瀵归綈锛�
+ */
+const startTimer = (interval = 500) => {
+  // 鍏堟竻闄ゅ凡鏈夊畾鏃跺櫒锛堥伩鍏嶉噸澶嶅垱寤猴級
+  if (timerId.value) clearInterval(timerId.value);
+  // 绔嬪嵆鎵ц涓�娆★紙閬垮厤棣栨绛夊緟闂撮殧锛�
+  fetchMachineData();
+  // 鍒涘缓鏂板畾鏃跺櫒
+  timerId.value = setInterval(() => {
+    fetchMachineData();
+  }, interval);
 };
+
+/**
+ * 娓呴櫎瀹氭椂鍣紙閬垮厤鍐呭瓨娉勬紡锛�
+ */
+const clearTimer = () => {
+  if (timerId.value) {
+    clearInterval(timerId.value);
+    timerId.value = null; // 閲嶇疆ID
+  }
+};
+
+// 缁勪欢鍗歌浇鏃舵竻闄ゅ畾鏃跺櫒锛堝叧閿細閬垮厤缁勪欢閿�姣佸悗瀹氭椂鍣ㄤ粛杩愯锛�
 onUnmounted(() => {
-  if (socket) {
-    closeWebSocket(socket);
-  }
-  if (socketDownLineTask) {
-    closeWebSocket(socketDownLineTask);
-  }
-});
-onBeforeUnmount(() => {
-  console.log("鍏抽棴浜�")
-  closeWebSocket();
+  clearTimer();
 });
 
 onMounted(async () => {
-  //浣跨敤WebSocket鏂瑰紡灞曠ず鏁版嵁
-  socket = initializeWebSocket(socketUrl, handleMessage);// 鍒濆鍖� WebSocket锛屽苟浼犻�掓秷鎭鐞嗗嚱鏁�
-  //浣跨敤WebSocket鏂瑰紡灞曠ず鏁版嵁
-  socketDownLineTask = initializeWebSocket(socketDownLineTaskUrl, handleMessage2);// 鍒濆鍖� WebSocket锛屽苟浼犻�掓秷鎭鐞嗗嚱鏁�
+  startTimer(500); // 500ms 闂撮殧锛堝彲鏍规嵁涓氬姟璋冩暣锛屽1000ms锛�
+  // //浣跨敤WebSocket鏂瑰紡灞曠ず鏁版嵁
+  // socket = initializeWebSocket(socketUrl, handleMessage);// 鍒濆鍖� WebSocket锛屽苟浼犻�掓秷鎭鐞嗗嚱鏁�
+  // //浣跨敤WebSocket鏂瑰紡灞曠ず鏁版嵁
+  // socketDownLineTask = initializeWebSocket(socketDownLineTaskUrl, handleMessage2);// 鍒濆鍖� WebSocket锛屽苟浼犻�掓秷鎭鐞嗗嚱鏁�
 });
+
+
+
+
+
+
 //鎶ヨ鎻愮ず
 const errorInfoFuntion = async (info) => {
   let infoScancode=info;
   ElMessageBox.confirm(
-    infoScancode,
-    t('delivery.prompt'),
-    {
-      confirmButtonText: t('functionState.sureStart'),
-      cancelButtonText: t('functionState.cancel'),
-      type: 'warning',
-    }
+      infoScancode,
+      t('delivery.prompt'),
+      {
+        confirmButtonText: t('functionState.sureStart'),
+        cancelButtonText: t('functionState.cancel'),
+        type: 'warning',
+      }
   )
-    .then(() => {
-    })
-    .catch(() => {
-      ElMessage({
-        type: 'info',
-        message: t('functionState.cancel'),
+      .then(() => {
       })
-    })
+      .catch(() => {
+        ElMessage({
+          type: 'info',
+          message: t('functionState.cancel'),
+        })
+      })
 }
 //淇敼宸ヤ綔鐘舵�� 銆愬け璐�/姝e湪宸ヤ綔/瀹屽伐銆�
 const workStatus = async (row, state) => {
@@ -115,199 +158,199 @@
     return;
   }
   ElMessageBox.confirm(
-    t('functionState.tips'),
-    t('delivery.prompt'),
-    {
-      confirmButtonText: t('functionState.sure'),
-      cancelButtonText: t('functionState.cancel'),
-      type: 'warning',
-    }
+      t('functionState.tips'),
+      t('delivery.prompt'),
+      {
+        confirmButtonText: t('functionState.sure'),
+        cancelButtonText: t('functionState.cancel'),
+        type: 'warning',
+      }
   )
-    .then(() => {
-      //寮�濮嬩慨鏀�
-      request.post(url,
-        {
-          "id": machineId
-        }).then((res) => { // 鏇挎崲涓轰綘鐨凙PI绔偣  
+      .then(() => {
+        //寮�濮嬩慨鏀�
+        request.post(url,
+            {
+              "id": machineId
+            }).then((res) => { // 鏇挎崲涓轰綘鐨凙PI绔偣
           if (res.code === 200) {
             ElMessage.success(res.message);
           } else {
             ElMessage.warning(res.message)
           }
         })
-    })
-    .catch(() => {
-      ElMessage({
-        type: 'info',
-        message: t('functionState.cancel'),
       })
-    })
+      .catch(() => {
+        ElMessage({
+          type: 'info',
+          message: t('functionState.cancel'),
+        })
+      })
 
 }
 
 //寮�宸�/鏆傚仠
 const machineStatus = async (state) => {
   ElMessageBox.confirm(
-    t('functionState.tips'),
-    t('delivery.prompt'),
-    {
-      confirmButtonText: t('functionState.sure'),
-      cancelButtonText: t('functionState.cancel'),
-      type: 'warning',
-    }
+      t('functionState.tips'),
+      t('delivery.prompt'),
+      {
+        confirmButtonText: t('functionState.sure'),
+        cancelButtonText: t('functionState.cancel'),
+        type: 'warning',
+      }
   )
-    .then(() => {
-      //涓嬬嚎鎺ュ彛
-      request.post("/deviceInteraction/machine/updateMachineState",
-        {
-          "id": machineId,
-          "state": state
-        }).then((res) => { // 鏇挎崲涓轰綘鐨凙PI绔偣  
+      .then(() => {
+        //涓嬬嚎鎺ュ彛
+        request.post("/deviceInteraction/machine/updateMachineState",
+            {
+              "id": machineId,
+              "state": state
+            }).then((res) => { // 鏇挎崲涓轰綘鐨凙PI绔偣
           if (res.code === 200) {
             ElMessage.success(res.message);
           } else {
             ElMessage.warning(res.message)
           }
         })
-    })
-    .catch(() => {
-      ElMessage({
-        type: 'info',
-        message: t('functionState.cancel'),
       })
-    })
+      .catch(() => {
+        ElMessage({
+          type: 'info',
+          message: t('functionState.cancel'),
+        })
+      })
 }
 //瀹氬埗/鏍囧噯
 const machineMode = async (mode) => {
   ElMessageBox.confirm(
-    t('functionState.tips'),
-    t('delivery.prompt'),
-    {
-      confirmButtonText: t('functionState.sure'),
-      cancelButtonText: t('functionState.cancel'),
-      type: 'warning',
-    }
+      t('functionState.tips'),
+      t('delivery.prompt'),
+      {
+        confirmButtonText: t('functionState.sure'),
+        cancelButtonText: t('functionState.cancel'),
+        type: 'warning',
+      }
   )
-    .then(() => {
-      //涓嬬嚎鎺ュ彛
-      request.post("/deviceInteraction/machine/toggleModeMachine",
-        {
-          "id": machineId,
-          "mode": mode
-        }).then((res) => { // 鏇挎崲涓轰綘鐨凙PI绔偣  
+      .then(() => {
+        //涓嬬嚎鎺ュ彛
+        request.post("/deviceInteraction/machine/toggleModeMachine",
+            {
+              "id": machineId,
+              "mode": mode
+            }).then((res) => { // 鏇挎崲涓轰綘鐨凙PI绔偣
           if (res.code === 200) {
             ElMessage.success(res.message);
           } else {
             ElMessage.warning(res.message)
           }
         })
-    })
-    .catch(() => {
-      ElMessage({
-        type: 'info',
-        message: t('functionState.cancel'),
       })
-    })
+      .catch(() => {
+        ElMessage({
+          type: 'info',
+          message: t('functionState.cancel'),
+        })
+      })
 }
 //鐮存崯
 const damagedTask = async (row) => {
   ElMessageBox.confirm(
-    t('functionState.tips'),
-    t('delivery.prompt'),
-    {
-      confirmButtonText: t('functionState.sure'),
-      cancelButtonText: t('functionState.cancel'),
-      type: 'warning',
-    }
+      t('functionState.tips'),
+      t('delivery.prompt'),
+      {
+        confirmButtonText: t('functionState.sure'),
+        cancelButtonText: t('functionState.cancel'),
+        type: 'warning',
+      }
   )
-    .then(() => {
-      //涓嬬嚎鎺ュ彛
-      request.post("/deviceInteraction/tasking/damagedTask",
-        {
-          "glassId": row.glassId
-        }).then((res) => { // 鏇挎崲涓轰綘鐨凙PI绔偣  
+      .then(() => {
+        //涓嬬嚎鎺ュ彛
+        request.post("/deviceInteraction/tasking/damagedTask",
+            {
+              "glassId": row.glassId
+            }).then((res) => { // 鏇挎崲涓轰綘鐨凙PI绔偣
           if (res.code === 200) {
             ElMessage.success(res.message);
           } else {
             ElMessage.warning(res.message)
           }
         })
-    })
-    .catch(() => {
-      ElMessage({
-        type: 'info',
-        message: t('functionState.cancel'),
       })
-    })
+      .catch(() => {
+        ElMessage({
+          type: 'info',
+          message: t('functionState.cancel'),
+        })
+      })
 }
 //涓嬬嚎(鎷胯蛋)
 const glassDownLine = async (row) => {
   ElMessageBox.confirm(
-    t('functionState.tips'),
-    t('delivery.prompt'),
-    {
-      confirmButtonText: t('functionState.sure'),
-      cancelButtonText: t('functionState.cancel'),
-      type: 'warning',
-    }
+      t('functionState.tips'),
+      t('delivery.prompt'),
+      {
+        confirmButtonText: t('functionState.sure'),
+        cancelButtonText: t('functionState.cancel'),
+        type: 'warning',
+      }
   )
-    .then(() => {
-      //涓嬬嚎鎺ュ彛
-      request.post("/deviceInteraction/tasking/glassDownLine",
-        {
-          "glassId": row.glassId,
-        }).then((res) => { // 鏇挎崲涓轰綘鐨凙PI绔偣  
+      .then(() => {
+        //涓嬬嚎鎺ュ彛
+        request.post("/deviceInteraction/tasking/glassDownLine",
+            {
+              "glassId": row.glassId,
+            }).then((res) => { // 鏇挎崲涓轰綘鐨凙PI绔偣
           if (res.code === 200) {
             ElMessage.success(res.message);
           } else {
             ElMessage.warning(res.message)
           }
         })
-    })
-    .catch(() => {
-      ElMessage({
-        type: 'info',
-        message: t('functionState.cancel'),
       })
-    })
+      .catch(() => {
+        ElMessage({
+          type: 'info',
+          message: t('functionState.cancel'),
+        })
+      })
 }
 const topLineShow = async () => {
-  blind.value = true; 
+  blind.value = true;
 }
 //涓婄嚎
 const topLine = async (row) => {
   ElMessageBox.confirm(
-    t('functionState.tips'),
-    t('delivery.prompt'),
-    {
-      confirmButtonText: t('functionState.sure'),
-      cancelButtonText: t('functionState.cancel'),
-      type: 'warning',
-    }
+      t('functionState.tips'),
+      t('delivery.prompt'),
+      {
+        confirmButtonText: t('functionState.sure'),
+        cancelButtonText: t('functionState.cancel'),
+        type: 'warning',
+      }
   )
-    .then(() => {
-      //涓婄嚎鎺ュ彛
-      request.post("/deviceInteraction/tasking/glassTopLine",
-        {
-          "glassId": row.glassId,
-          "lineConfigurationId": machineId
-        }).then((res) => { // 鏇挎崲涓轰綘鐨凙PI绔偣  
+      .then(() => {
+        //涓婄嚎鎺ュ彛
+        request.post("/deviceInteraction/tasking/glassTopLine",
+            {
+              "glassId": row.glassId,
+              "lineConfigurationId": machineId
+            }).then((res) => { // 鏇挎崲涓轰綘鐨凙PI绔偣
           if (res.code === 200) {
             ElMessage.success(res.message);
           } else {n
             ElMessage.warning(res.message)
           }
         })
-    })
-    .catch(() => {
-      ElMessage({
-        type: 'info',
-        message: t('functionState.cancel'),
       })
-    })
+      .catch(() => {
+        ElMessage({
+          type: 'info',
+          message: t('functionState.cancel'),
+        })
+      })
 }
-const handleDialogClose = () => {  
-    
+const handleDialogClose = () => {
+
 }
 </script>
 <template>
@@ -316,7 +359,7 @@
       {{ $t('machine.flipSlice') }}
     </div>
     <el-dialog v-model="blind" top="30vh" style="text-align: center;" @close="handleDialogClose">
-        <!-- 绾夸笅鐜荤拑 -->
+      <!-- 绾夸笅鐜荤拑 -->
       <el-table :data="downLineTask" stripe :header-cell-style="{ background: '#F2F3F5 ', color: '#1D2129', textAlign: 'center' }" :cell-style="{ textAlign: 'center' }">
         <!-- <el-table-column type="selection" min-width="30" /> -->
         <el-table-column type="index" :label="$t('glassInfo.number')" min-width="30" />
@@ -330,7 +373,7 @@
         <el-table-column fixed="right" :label="$t('productStock.operate')" align="center" width="70">
           <template #default="scope">
             <el-button size="mini" link type="primary" plain @click="topLine(scope.row)">{{
-              $t('functionState.topLine') }}</el-button>
+                $t('functionState.topLine') }}</el-button>
           </template>
         </el-table-column>
       </el-table>
@@ -340,13 +383,13 @@
     <div id="search">
       <!-- 鍔熻兘 -->
       <el-button :type="(findMachine['state'] == '鏆傚仠' ? 'danger' : 'success')" id="ButtonMachineStatus"
-        @click="machineStatus((findMachine['state'] == '鏆傚仠' ? '寮�宸�' : '鏆傚仠'))">{{ findMachine['state'] == '寮�宸�' ?
+                 @click="machineStatus((findMachine['state'] == '鏆傚仠' ? '寮�宸�' : '鏆傚仠'))">{{ findMachine['state'] == '寮�宸�' ?
           $t('functionState.start') : $t('functionState.stop') }}</el-button>
 
       <el-button :type="(findMachine['mode'] == '瀹氬埗' ? 'danger' : 'success')" id="ButtonMachineMode"
-        @click="machineMode((findMachine['mode'] == 0 ? 1 : 0))">{{ findMachine['mode'] == 0 ?
-         $t('functionState.modeD') : $t('functionState.modeB') }}</el-button>
-         
+                 @click="machineMode((findMachine['mode'] == 0 ? 1 : 0))">{{ findMachine['mode'] == 0 ?
+          $t('functionState.modeD') : $t('functionState.modeB') }}</el-button>
+
       <el-button type="primary" id="ButtonTopLine" @click="topLineShow">{{ $t('functionState.topLine') }}</el-button>
       <!-- <el-button type="primary" id="searchButton" @click="downLine('涓嬬嚎')">涓嬬嚎</el-button>
       <el-button type="primary" id="searchButton" @click="workStatus('鐮存崯')">鐮存崯</el-button>
@@ -355,8 +398,8 @@
     <div id="main-body">
       <!-- 琛ㄦ牸鍐呭 -->
       <el-table :data="loadData" stripe style="height:260px"
-        :header-cell-style="{ background: '#F2F3F5 ', color: '#1D2129', textAlign: 'center' }"
-        :cell-style="{ textAlign: 'center' }">
+                :header-cell-style="{ background: '#F2F3F5 ', color: '#1D2129', textAlign: 'center' }"
+                :cell-style="{ textAlign: 'center' }">
         <!-- <el-table-column type="selection" min-width="30" /> -->
         <el-table-column type="index" :label="$t('glassInfo.number')" min-width="30" />
         <el-table-column prop="batchNumber" :label="$t('glassInfo.batchNumber')" />
@@ -371,13 +414,13 @@
         <el-table-column fixed="right" :label="$t('productStock.operate')" align="center" width="270">
           <template #default="scope">
             <el-button size="mini" link type="primary" plain @click="workStatus(scope.row, '閲嶅彂')">{{
-              $t('functionState.anew') }}</el-button>
+                $t('functionState.anew') }}</el-button>
             <el-button size="mini" link type="primary" plain @click="workStatus(scope.row, '瀹屽伐')">{{
-              $t('functionState.finish') }}</el-button>
+                $t('functionState.finish') }}</el-button>
             <el-button size="mini" link type="primary" plain @click="damagedTask(scope.row)">{{ $t('functionState.lose')
               }}</el-button>
             <el-button size="mini" link type="primary" plain @click="glassDownLine(scope.row)">{{
-              $t('functionState.downLine') }}</el-button>
+                $t('functionState.downLine') }}</el-button>
           </template>
         </el-table-column>
       </el-table>
@@ -391,8 +434,8 @@
       </div> -->
       <div style="width: 30%;min-width: 300px; height: 460px;float: left;padding: 20px;;">
         <el-table :data="sendRecords" stripe
-          :header-cell-style="{ background: '#F2F3F5 ', color: '#1D2129', textAlign: 'center', height: '30px' }"
-          :cell-style="{ textAlign: 'center' }">
+                  :header-cell-style="{ background: '#F2F3F5 ', color: '#1D2129', textAlign: 'center', height: '30px' }"
+                  :cell-style="{ textAlign: 'center' }">
           <el-table-column prop="sendContent" :label="$t('glassInfo.sendContent')" />
         </el-table>
       </div>
@@ -467,6 +510,6 @@
   height:10px;
   border-radius: 50%;
   display: block;
-  margin-left: 10px; 
+  margin-left: 10px;
 }
 </style>
\ No newline at end of file

--
Gitblit v1.8.0