From 28ba709adf380a0a325bbf5e1f487239f4afc74f Mon Sep 17 00:00:00 2001 From: SikongJueluo <1822250894@qq.com> Date: Fri, 22 Aug 2025 05:03:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=95=B0=E7=A0=81?= =?UTF-8?q?=E7=AE=A1=E6=97=B6=E5=B8=B8=E6=97=A0=E6=B3=95=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SevenSegmentDisplayUltimate.vue | 4 +- src/stores/equipments.ts | 101 ++++++++++-------- 2 files changed, 57 insertions(+), 48 deletions(-) diff --git a/src/components/equipments/SevenSegmentDisplayUltimate.vue b/src/components/equipments/SevenSegmentDisplayUltimate.vue index ce39a80..1360d9d 100644 --- a/src/components/equipments/SevenSegmentDisplayUltimate.vue +++ b/src/components/equipments/SevenSegmentDisplayUltimate.vue @@ -184,7 +184,7 @@ async function initDigitalTwin() { if ( !eqps.enableSevenSegmentDisplay || props.digitalTwinNum <= 0 || - props.digitalTwinNum > 31 + props.digitalTwinNum > 32 ) return; @@ -205,7 +205,7 @@ watch( if ( !eqps.sevenSegmentDisplayData || props.digitalTwinNum <= 0 || - props.digitalTwinNum > 31 + props.digitalTwinNum > 32 ) return; diff --git a/src/stores/equipments.ts b/src/stores/equipments.ts index 54ba9ed..0d6e8da 100644 --- a/src/stores/equipments.ts +++ b/src/stores/equipments.ts @@ -1,4 +1,4 @@ -import { ref, reactive, watchPostEffect, onMounted, onUnmounted } from "vue"; +import { ref, reactive, shallowRef, onMounted, onUnmounted } from "vue"; import { defineStore } from "pinia"; import { useLocalStorage } from "@vueuse/core"; import { isString, toNumber, isUndefined, type Dictionary } from "lodash"; @@ -302,40 +302,74 @@ export const useEquipments = defineStore("equipments", () => { const enableSevenSegmentDisplay = ref(false); const sevenSegmentDisplayFrequency = ref(100); const sevenSegmentDisplayData = ref(); - const sevenSegmentDisplayHub = ref(); - const sevenSegmentDisplayHubProxy = ref(); + + const sevenSegmentDisplayHub = shallowRef<{ + connection: HubConnection; + proxy: IDigitalTubesHub; + } | null>(null); + + async function initSevenDigitalTubesHub() { + // 每次挂载都重新创建连接 + if (sevenSegmentDisplayHub.value) return; + const connection = AuthManager.createHubConnection("DigitalTubesHub"); + const proxy = + getHubProxyFactory("IDigitalTubesHub").createHubProxy(connection); + + getReceiverRegister("IDigitalTubesReceiver").register(connection, { + onReceive: handleSevenSegmentDisplayOnReceive, + }); + await connection.start(); + sevenSegmentDisplayHub.value = { connection, proxy }; + } + + async function clearSevenDigitalTubesHub() { + if (!sevenSegmentDisplayHub.value) return; + sevenSegmentDisplayHub.value.connection.stop(); + sevenSegmentDisplayHub.value = null; + } + + async function reinitializeSevenDigitalTubesHub() { + await clearSevenDigitalTubesHub(); + await initSevenDigitalTubesHub(); + } + + function getSevenDigitalTubesHubProxy() { + if (!sevenSegmentDisplayHub.value) { + reinitializeSevenDigitalTubesHub(); + throw new Error("Hub not initialized"); + } + return sevenSegmentDisplayHub.value.proxy; + } + + onMounted(async () => { + await initSevenDigitalTubesHub(); + }); + + onUnmounted(async () => { + // 断开连接,清理资源 + await clearSevenDigitalTubesHub(); + }); async function sevenSegmentDisplaySetOnOff(enable: boolean) { - if (!sevenSegmentDisplayHub.value || !sevenSegmentDisplayHubProxy.value) - return; - if (sevenSegmentDisplayHub.value.state === HubConnectionState.Disconnected) - await sevenSegmentDisplayHub.value.start(); + const proxy = getSevenDigitalTubesHubProxy(); if (enable) { - await sevenSegmentDisplayHubProxy.value.startScan(); + await proxy.startScan(); enableSevenSegmentDisplay.value = true; } else { - await sevenSegmentDisplayHubProxy.value.stopScan(); + await proxy.stopScan(); enableSevenSegmentDisplay.value = false; } } async function sevenSegmentDisplaySetFrequency(frequency: number) { - if (!sevenSegmentDisplayHub.value || !sevenSegmentDisplayHubProxy.value) - return; - if (sevenSegmentDisplayHub.value.state === HubConnectionState.Disconnected) - await sevenSegmentDisplayHub.value.start(); - - await sevenSegmentDisplayHubProxy.value.setFrequency(frequency); + const proxy = getSevenDigitalTubesHubProxy(); + return await proxy.setFrequency(frequency); } async function sevenSegmentDisplayGetStatus() { - if (!sevenSegmentDisplayHub.value || !sevenSegmentDisplayHubProxy.value) - return; - if (sevenSegmentDisplayHub.value.state === HubConnectionState.Disconnected) - await sevenSegmentDisplayHub.value.start(); - - return await sevenSegmentDisplayHubProxy.value.getStatus(); + const proxy = getSevenDigitalTubesHubProxy(); + return await proxy.getStatus(); } async function handleSevenSegmentDisplayOnReceive(msg: string) { @@ -343,31 +377,6 @@ export const useEquipments = defineStore("equipments", () => { sevenSegmentDisplayData.value = new Uint8Array(bytes); } - onMounted(async () => { - // 每次挂载都重新创建连接 - sevenSegmentDisplayHub.value = - AuthManager.createHubConnection("DigitalTubesHub"); - sevenSegmentDisplayHubProxy.value = getHubProxyFactory( - "IDigitalTubesHub", - ).createHubProxy(sevenSegmentDisplayHub.value); - - getReceiverRegister("IDigitalTubesReceiver").register( - sevenSegmentDisplayHub.value, - { - onReceive: handleSevenSegmentDisplayOnReceive, - }, - ); - }); - - onUnmounted(() => { - // 断开连接,清理资源 - if (sevenSegmentDisplayHub.value) { - sevenSegmentDisplayHub.value.stop(); - sevenSegmentDisplayHub.value = undefined; - sevenSegmentDisplayHubProxy.value = undefined; - } - }); - return { boardAddr, boardPort,