From 5bf14aed888cd0e258e325c65f14022dad02985b Mon Sep 17 00:00:00 2001 From: baoshiwei <baoshiwei@shlanbao.cn> Date: 星期四, 17 七月 2025 15:32:01 +0800 Subject: [PATCH] 更改为通过websocket获取数据 --- src/components/ForceChart.vue | 77 ++++++++++++-------------------------- 1 files changed, 24 insertions(+), 53 deletions(-) diff --git a/src/components/ForceChart.vue b/src/components/ForceChart.vue index 138a1c3..bc3c801 100644 --- a/src/components/ForceChart.vue +++ b/src/components/ForceChart.vue @@ -1,11 +1,7 @@ <script setup lang="ts"> -import { ref, onMounted, onUnmounted} from 'vue'; +import { ref, onMounted, onUnmounted } from 'vue'; import * as echarts from 'echarts'; -import { sendToPipe } from '../pipe_client'; - -const props = defineProps<{ - pipeName: string; -}>(); +import { createDataReceiver } from '../utils/dataFetcher'; const chartContainer = ref<HTMLElement | null>(null); let chart: echarts.ECharts | null = null; @@ -64,37 +60,43 @@ name: 'Fx', type: 'line', data: dataHistory.value[0], - smooth: true + smooth: true, + showSymbol: false }, { name: 'Fy', type: 'line', data: dataHistory.value[1], - smooth: true + smooth: true, + showSymbol: false }, { name: 'Fz', type: 'line', data: dataHistory.value[2], - smooth: true + smooth: true, + showSymbol: false }, { name: 'Mx', type: 'line', data: dataHistory.value[3], - smooth: true + smooth: true, + showSymbol: false }, { name: 'My', type: 'line', data: dataHistory.value[4], - smooth: true + smooth: true, + showSymbol: false }, { name: 'Mz', type: 'line', data: dataHistory.value[5], - smooth: true + smooth: true, + showSymbol: false } ] }; @@ -150,53 +152,22 @@ chart?.resize(); } -// 鐘舵�佸彉閲忥紝鐢ㄤ簬鎺у埗閿欒鏄剧ず棰戠巼 -const errorCount = ref(0); -const maxConsecutiveErrors = 5; -const showingError = ref(false); - -// 鎺ユ敹绠¢亾鏁版嵁鐨勫嚱鏁� -async function receiveForceData() { - try { - // 浠庣閬撴帴鏀舵暟鎹� - const response = await sendToPipe(props.pipeName, 'GET_FORCE_DATA'); - - // 鎴愬姛鎺ユ敹鏁版嵁锛岄噸缃敊璇鏁� - errorCount.value = 0; - if (showingError.value) { - showingError.value = false; - console.log('绠¢亾閫氫俊宸叉仮澶�'); - } - - // 瑙f瀽鎺ユ敹鍒扮殑鏁版嵁 - try { - const forceData = JSON.parse(response); - if (Array.isArray(forceData) && forceData.length === 6) { - updateChart(forceData); - } - } catch (e) { - console.warn('瑙f瀽鏁版嵁澶辫触:', e); - } - } catch (err) { - // 澧炲姞閿欒璁℃暟 - errorCount.value++; - - // 鍙湪杩炵画閿欒杈惧埌闃堝�兼椂鏄剧ず閿欒淇℃伅锛岄伩鍏嶆棩蹇楀埛灞� - if (errorCount.value >= maxConsecutiveErrors && !showingError.value) { - showingError.value = true; - console.error('绠¢亾閫氫俊鎸佺画澶辫触锛岃妫�鏌ユ湇鍔$鐘舵��:', err); - } - } -} - // 瀹氭椂鑾峰彇鏁版嵁 let dataTimer: number | null = null; + onMounted(() => { initChart(); - // 姣忕鑾峰彇涓�娆℃暟鎹� - dataTimer = window.setInterval(receiveForceData, 1000); + const receiveData = createDataReceiver( (forceData) => { + updateChart(forceData); + }); + + // 姣�100ms鑾峰彇涓�娆℃暟鎹� + dataTimer = window.setInterval(receiveData, 100); + + // 鐩戝惉绐楀彛澶у皬鍙樺寲锛岃皟鏁村浘琛ㄥぇ灏� + window.addEventListener('resize', handleResize); }); onUnmounted(() => { -- Gitblit v1.9.3