From 093615ab71eebddeea28a74a6c21898fc929450f Mon Sep 17 00:00:00 2001
From: yaowanxin <3588231647@qq.com>
Date: 星期一, 04 八月 2025 14:43:22 +0800
Subject: [PATCH] 判断检测项,调整请求

---
 src/utils/connect.js |  109 ++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 73 insertions(+), 36 deletions(-)

diff --git a/src/utils/connect.js b/src/utils/connect.js
index 39edfd6..f5899fb 100644
--- a/src/utils/connect.js
+++ b/src/utils/connect.js
@@ -1,10 +1,18 @@
+import request from '@/utils/request';
 // 鏂板锛氬瓨鍌ㄥ叏灞�涓插彛鍜岃鍙栧櫒瀹炰緥
     let port = null;
     let reader = null;
     let accumulatedData = ""; // 绉诲埌鍏ㄥ眬锛屼究浜庡叧闂椂閲嶇疆
+    let weightList = []; // 鐢ㄤ簬瀛樺偍閲嶉噺鍊肩殑鍒楄〃
+    export function sendWeightsToBackend(weights) {
+      return request({
+        url: "/weight/handleWeights",
+        method: "POST",
+        data: weights,
+      });
+    }
 
     async function connect() {
-      if ("serial" in navigator) {
         try {
           port = await navigator.serial.requestPort(); // 淇敼涓哄叏灞�鍙橀噺
           await port.open({ baudRate: 9600 });
@@ -28,46 +36,75 @@
         } catch (error) {
           console.error("涓插彛閿欒:", error);
         }
-      }
+    }
+    function processAccumulatedData(data) {
 
-      function processAccumulatedData(data) {
-        if (data.includes("\n")) {
-          const messages = data.split("\n");
+      if (data.includes("\n")) {
+        const messages = data.split("\n");
 
-          for (let i = 0; i < messages.length - 1; i++) {
-            const completeMessage = messages[i];
-            console.log("瀹屾暣娑堟伅:", completeMessage);
-            handleMessage(completeMessage);
+        for (let i = 0; i < messages.length - 1; i++) {
+          const completeMessage = messages[i];
+          console.log("瀹屾暣娑堟伅:", completeMessage);
+          // 鎻愬彇鏁板��
+          const weightValue = extractWeightValue(completeMessage);
+          if (!isNaN(weightValue)) {
+            console.log("鎻愬彇鐨勯噸閲忓��:", weightValue); 
+            // 灏嗘湁鏁堥噸閲忓�兼坊鍔犲埌鍒楄〃
+            weightList.push(weightValue);
+
+            // 褰撳垪琛ㄩ暱搴﹁揪鍒� 6 鏃讹紝鍙戦�佹暟鎹埌鍚庣骞舵竻绌哄垪琛�
+            if (weightList.length === 6) { 
+              sendWeightsToBackend({
+                weights: weightList
+              })
+              .then(() => {
+                console.log('鏁版嵁鍙戦�佹垚鍔�');
+                weightList = [];
+              })
+              .catch(() => {
+                console.log('鍙戦�佸け璐ワ紝淇濈暀鏁版嵁寰呴噸璇�');
+              });
+            }
           }
-
-          accumulatedData = messages[messages.length - 1];
         }
 
-        // 鍏朵粬娑堟伅鎷嗗垎绀轰緥淇濇寔涓嶅彉
-        /*
-  while (accumulatedData.length >= 32) {
-    const message = accumulatedData.substring(0, 32);
-    accumulatedData = accumulatedData.substring(32);
-    handleMessage(message);
-  }
-  */
-
-        /*
-  let startIndex = 0;
-  while (true) {
-    const start = accumulatedData.indexOf(0xAA, startIndex);
-    if (start === -1) break;
-    
-    const end = accumulatedData.indexOf(0x55, start + 1);
-    if (end === -1) break;
-    
-    const message = accumulatedData.substring(start, end + 1);
-    accumulatedData = accumulatedData.substring(end + 1);
-    handleMessage(message);
-    startIndex = start;
-  }
-  */
+        accumulatedData = messages[messages.length - 1];
       }
+
+      // 鍏朵粬娑堟伅鎷嗗垎绀轰緥淇濇寔涓嶅彉
+      /*
+      while (accumulatedData.length >= 32) {
+        const message = accumulatedData.substring(0, 32);
+        accumulatedData = accumulatedData.substring(32);
+        handleMessage(message);
+      }
+      */
+
+      /*
+      let startIndex = 0;
+      while (true) {
+        const start = accumulatedData.indexOf(0xAA, startIndex);
+        if (start === -1) break;
+        
+        const end = accumulatedData.indexOf(0x55, start + 1);
+        if (end === -1) break;
+        
+        const message = accumulatedData.substring(start, end + 1);
+        accumulatedData = accumulatedData.substring(end + 1);
+        handleMessage(message);
+        startIndex = start;
+      }
+      */
+    }
+    
+    // 鏂板鎻愬彇閲嶉噺鏁板�肩殑鏂规硶
+    function extractWeightValue(message) {
+      // 鍖归厤鏁板�奸儴鍒嗭紝鍙鐞嗘璐熷彿
+      const match = message.match(/-?\d+\.?\d*/); 
+      if (match) {
+        return parseFloat(match[0]);
+      }
+      return NaN;
     }
 
     // 鏂板锛氬叧闂繛鎺ュ嚱鏁�
@@ -93,4 +130,4 @@
       }
     }
 
-    export { connect, disconnect };
\ No newline at end of file
+    export { connect, disconnect,weightList };
\ No newline at end of file

--
Gitblit v1.9.3