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 | 126 +++++++++++++++++++----------------------- 1 files changed, 57 insertions(+), 69 deletions(-) diff --git a/src/utils/connect.js b/src/utils/connect.js index 251a15e..f5899fb 100644 --- a/src/utils/connect.js +++ b/src/utils/connect.js @@ -4,24 +4,12 @@ let reader = null; let accumulatedData = ""; // 绉诲埌鍏ㄥ眬锛屼究浜庡叧闂椂閲嶇疆 let weightList = []; // 鐢ㄤ簬瀛樺偍閲嶉噺鍊肩殑鍒楄〃 - - async function sendWeightsToBackend(weights) { - try { - // 浣跨敤 await 绛夊緟 request 鍑芥暟杩斿洖缁撴灉 - const response = await request({ - url: "/weight/handleWeights", - method: "POST", - data: weights, // 閫氬父 request 鍑芥暟浣跨敤 data 瀛楁浼犻�掓暟鎹� - }); - // 鏍规嵁 request 鍑芥暟鐨勫疄鐜板垽鏂搷搴旀槸鍚︽垚鍔� - if (response.code !== 200) { // 鍋囪 response 鏈� code 瀛楁锛�200 琛ㄧず鎴愬姛 - throw new Error(`Backend response error: ${response.message}`); - } - return response.data; // 鍋囪鍝嶅簲鏁版嵁鍦� data 瀛楁涓� - } catch (error) { - console.error('鍙戦�侀噸閲忔暟鎹埌鍚庣澶辫触:', error); - throw error; - } + export function sendWeightsToBackend(weights) { + return request({ + url: "/weight/handleWeights", + method: "POST", + data: weights, + }); } async function connect() { @@ -49,66 +37,66 @@ 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); - // 鎻愬彇鏁板�� - const weightValue = extractWeightValue(completeMessage); - if (!isNaN(weightValue)) { - console.log("鎻愬彇鐨勯噸閲忓��:", weightValue); - // 灏嗘湁鏁堥噸閲忓�兼坊鍔犲埌鍒楄〃 - weightList.push(weightValue); + 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('鍙戦�佸け璐ワ紝淇濈暀鏁版嵁寰呴噸璇�'); - }); - } + // 褰撳垪琛ㄩ暱搴﹁揪鍒� 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) { // 鍖归厤鏁板�奸儴鍒嗭紝鍙鐞嗘璐熷彿 -- Gitblit v1.9.3