From 545d02e57bc316e08bc9d58beed48d706dce5daa Mon Sep 17 00:00:00 2001 From: kerms Date: Sat, 20 Jul 2024 11:33:54 +0800 Subject: [PATCH] fix(uart) loopSend can't be cleared when change pages and back to uart page --- src/stores/dataViewerStore.ts | 58 +++++++++ src/views/text-data-viewer/textDataViewer.vue | 115 +++++------------- 2 files changed, 87 insertions(+), 86 deletions(-) diff --git a/src/stores/dataViewerStore.ts b/src/stores/dataViewerStore.ts index a1345b1..6b577da 100644 --- a/src/stores/dataViewerStore.ts +++ b/src/stores/dataViewerStore.ts @@ -753,6 +753,63 @@ export const useDataViewerStore = defineStore('text-viewer', () => { uartBaud.value = baud; } + + const enableLoopSend = ref(false); + const loopSendFreq = ref(1000); + let loopSendIntervalID: number = -1; + const uartInputTextBox = ref(""); + const isSendTextFormat = ref(true); + const isHexStringValid = ref(false); + + function loopSend() { + if (!acceptIncomingData.value) { + enableLoopSend.value = false; + } + + if (isSendTextFormat.value) { + addString(uartInputTextBox.value, false, true); + } else { + if (!isHexStringValid.value) { + addString("HEX格式错误", false, false, 1); + addHexString(uartInputTextBox.value, false, false, 1); + } else { + addHexString(uartInputTextBox.value, false, true); + } + } + } + + watch(enableLoopSend, (newValue) => { + if (newValue) { + if (loopSendIntervalID !== -1) { + clearInterval(loopSendIntervalID); + } + loopSendIntervalID = setInterval(loopSend, loopSendFreq.value); + } else { + clearInterval(loopSendIntervalID); + loopSendIntervalID = -1; + } + }); + + watch(loopSendFreq, (value) => { + if (enableLoopSend.value && value) { + /* update interval with new value */ + if (loopSendIntervalID !== -1) { + clearInterval(loopSendIntervalID); + } + loopSendIntervalID = setInterval(loopSend, loopSendFreq.value); + } + }) + + const loopSendRet = { + enableLoopSend, + loopSendFreq, + loopSendIntervalID, + isSendTextFormat, + uartInputTextBox, + isHexStringValid, + } + + return { addItem, addString, @@ -804,5 +861,6 @@ export const useDataViewerStore = defineStore('text-viewer', () => { uartConfig, uartBaudReal, setUartBaud, + ...loopSendRet, } }); diff --git a/src/views/text-data-viewer/textDataViewer.vue b/src/views/text-data-viewer/textDataViewer.vue index 11943f5..eb23a4b 100644 --- a/src/views/text-data-viewer/textDataViewer.vue +++ b/src/views/text-data-viewer/textDataViewer.vue @@ -58,39 +58,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -188,11 +155,11 @@
- + 循环发送(ms) - - 发送格式:{{ isSendTextFormat ? "文本" : "HEX" }} + + 发送格式:{{ store.isSendTextFormat ? "文本" : "HEX" }}
@@ -228,8 +195,8 @@
- - {{ (isSendTextFormat || isHexStringValid) ? "发送" : "格式化" }} + {{ (store.isSendTextFormat || store.isHexStringValid) ? "发送" : "格式化" }}
@@ -255,14 +222,6 @@ const count = ref(0); const vuetifyVirtualScrollBarRef = ref(document.body); const vuetifyVirtualScrollContainerRef = ref(document.body); -const enableLoopSend = ref(false); -const loopSendFreq = ref(1000); -let loopSendIntervalID: number = -1; - -const isSendTextFormat = ref(true) -const isHexStringValid = ref(false); - -const uartInputTextBox = ref("") const store = useDataViewerStore(); const RxHexDumpRef = ref(document.body); @@ -303,6 +262,9 @@ onMounted(() => { onUnmounted(() => { mutationObserver.disconnect(); + if (!store.isHexStringValid) { + store.uartInputTextBox = formatHexInput(store.uartInputTextBox); + } }); debouncedWatch(() => store.showVirtualScroll, () => { @@ -381,43 +343,24 @@ function formatHexInput(input: string) { } function checkHexTextValid() { - isHexStringValid.value = uartInputTextBox.value.toUpperCase() === formatHexInput(uartInputTextBox.value); + store.isHexStringValid = store.uartInputTextBox.toUpperCase() === formatHexInput(store.uartInputTextBox); + if (!store.isHexStringValid) { + store.enableLoopSend = false; + } } -watch(isSendTextFormat, (value) => { +watch(() => store.isSendTextFormat, (value) => { if (!value) { checkHexTextValid() } }); -watch(() => uartInputTextBox.value, () => { - if (!isSendTextFormat.value) { +watch(() => store.uartInputTextBox, () => { + if (!store.isSendTextFormat) { checkHexTextValid() } }) -watch(enableLoopSend, (newValue) => { - if (newValue) { - if (loopSendIntervalID !== -1) { - clearInterval(loopSendIntervalID); - } - loopSendIntervalID = setInterval(onSendClick, loopSendFreq.value); - } else { - clearInterval(loopSendIntervalID); - loopSendIntervalID = -1; - } -}); - -watch(loopSendFreq, (value) => { - if (enableLoopSend.value && value) { - /* update interval with new value */ - if (loopSendIntervalID !== -1) { - clearInterval(loopSendIntervalID); - } - loopSendIntervalID = setInterval(onSendClick, loopSendFreq.value); - } -}) - /* patch scroll container does not update clear filter */ watch(() => store.filterChanged, (value) => { if (value && store.forceToBottom) { @@ -459,7 +402,7 @@ watch(() => store.forceToBottom, value => { }); function clearSendInput() { - uartInputTextBox.value = "" + store.uartInputTextBox = "" } function handleTextboxKeydown(ev: KeyboardEvent) { @@ -469,26 +412,26 @@ function handleTextboxKeydown(ev: KeyboardEvent) { } function onSendClick() { - if (!uartInputTextBox.value && !store.hasAddedText) { + if (!store.uartInputTextBox && !store.hasAddedText) { globalNotify("无帧头帧尾、发送框无数据发送") return; } if (store.acceptIncomingData) { - if (isSendTextFormat.value) { - store.addString(uartInputTextBox.value, false, true); - } else if (!isHexStringValid.value) { - uartInputTextBox.value = formatHexInput(uartInputTextBox.value); + if (store.isSendTextFormat) { + store.addString(store.uartInputTextBox, false, true); + } else if (!store.isHexStringValid) { + store.uartInputTextBox = formatHexInput(store.uartInputTextBox); } else { - store.addHexString(uartInputTextBox.value, false, true); + store.addHexString(store.uartInputTextBox, false, true); } } else { - if (isSendTextFormat.value) { - store.addString(uartInputTextBox.value, false, true, 1); - } else if (!isHexStringValid.value) { - uartInputTextBox.value = formatHexInput(uartInputTextBox.value); + if (store.isSendTextFormat) { + store.addString(store.uartInputTextBox, false, true, 1); + } else if (!store.isHexStringValid) { + store.uartInputTextBox = formatHexInput(store.uartInputTextBox); } else { - store.addHexString(uartInputTextBox.value, false, true, 1); + store.addHexString(store.uartInputTextBox, false, true, 1); } } }