From b4e48f21cf68e45365e812a3dc1c5545fcde78b7 Mon Sep 17 00:00:00 2001 From: sige Date: Fri, 24 May 2024 20:13:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B6=88=E6=AF=92=E5=BC=80=E5=A7=8B=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E8=B0=83=E7=94=A8=E6=B6=88=E6=AF=92=E5=BC=80=E5=A7=8B?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E9=9C=80=E8=A6=81=E5=BC=B9=E6=A1=86=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E9=94=99=E8=AF=AF=E4=BF=A1=E6=81=AF=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E4=B8=94=E4=B8=8D=E8=B7=B3=E8=BD=AC=E5=88=B0=E6=B6=88=E6=AF=92?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Operator.vue | 46 ++++++++++++++++++++++-------------------- src/store/modules/websocket.js | 46 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 65 insertions(+), 27 deletions(-) diff --git a/src/components/Operator.vue b/src/components/Operator.vue index 7cb1774..71b822c 100644 --- a/src/components/Operator.vue +++ b/src/components/Operator.vue @@ -367,8 +367,7 @@ import { import { startDisinfectionJSON, getStateJSON } from '@/mock/command' import { showSuccessToast, showFailToast } from 'vant' import { time_To_hhmmss } from '@/utils' - - +import MyModal from '../utils/MyModal' const operatorStore = useOperatorStore() const webSocketStore = useWebSocketStore() @@ -468,26 +467,29 @@ const startDisinfect = () => { // envirVisible.value = true } -const realStart = () => { - localStorage.setItem('logVal', logVal.value) - if ( - operatorStore.disinfectStatus == 0 || - operatorStore.disinfectStatus == 5 - ) { - localStorage.removeItem('bin') - localStorage.removeItem('envir1') - localStorage.removeItem('envir2') - webSocketStore.sendCommandMsg( - startDisinfectionJSON( - parseInt(logVal.value), - parseInt(roomSize.value), - parseInt(envirVal.value), - sealStore.airInletProportionalValue, - sealStore.airOutletProportionalValue, - ), - ) - props.changeShowOperator(false) - } +// 开始消毒 +async function realStart() { + operatorStore.updateShowStartReady(true) + localStorage.setItem('logVal', logVal.value) + if ( operatorStore.disinfectStatus == 0 || operatorStore.disinfectStatus == 5 ) { + localStorage.removeItem('bin') + localStorage.removeItem('envir1') + localStorage.removeItem('envir2') + + try { + await webSocketStore.call('startDisinfection', { + loglevel : parseInt(logVal.value), + roomVolume : parseInt(roomSize.value), + }); + } catch ( e ) { + operatorStore.updateShowStartReady(false) + await MyModal.error(`无法开始消毒 : ${e.message || e}`); + return ; + } + + operatorStore.updateShowStartReady(false) + props.changeShowOperator(false) + } } const showLogPicker = () => { diff --git a/src/store/modules/websocket.js b/src/store/modules/websocket.js index fba6896..5918b30 100644 --- a/src/store/modules/websocket.js +++ b/src/store/modules/websocket.js @@ -24,15 +24,31 @@ export const useWebSocketStore = defineStore({ socketCommandInstance: null, // 事件上报websocket 实例 socketEventInstance: null, - + // Event Handlers + eventHandlers : {}, + // Call ID Counter callIdCounter : 0, // Call Resolve Handler Map callPromiseHandlers : {}, + // Call Param Merge Commands + callparamMergeCmds : [ + 'cleanDisinfectionRecord', 'startDisinfection', 'changeDisinfectionParameter','setSettingVal' + ], } }, // actions actions: { + // register event handler + registerEventHandler( event, handler ) { + this.eventHandlers[event] = this.eventHandlers[event] || []; + this.eventHandlers[event].push(handler); + }, + + + + + initCommandSocket() { const url = import.meta.env.VITE_BASE_WS1_URL const init = new Socket(url) @@ -52,19 +68,23 @@ export const useWebSocketStore = defineStore({ let $this = this; init.ws.onmessage = function (ev) { const { messageId, timeStamp } = JSON.parse(ev.data) + + // 优先处理call-response if ( undefined !== $this.callPromiseHandlers[messageId] ) { let response = JSON.parse(ev.data); const handler = $this.callPromiseHandlers[messageId]; delete $this.callPromiseHandlers[messageId]; - console.log('call-response', response); + console.log(`[Call Response : ${messageId}] ${handler.message.command} => ${JSON.stringify(response)}`); if ( 0 === response.ackcode ) { handler.resolve(response); } else { - handler.reject(response.reason); + handler.reject(response.ackDisplayInfo); } return; } + + console.log(JSON.parse(ev.data)) switch (messageId) { case 'getState': @@ -359,10 +379,14 @@ export const useWebSocketStore = defineStore({ message.command = command; message.messageId = callId; if ( null !== params ) { - message.params = params; + if ( this.callparamMergeCmds.includes(command) ) { + message = { ...message, ...params }; + } else { + message.params = params; + } } this.callPromiseHandlers[callId] = { resolve, reject, message }; - console.log('[Call Request]', `${command}(${JSON.stringify(params)})`); + console.log(`[Call Request : ${callId}] ${command}(${JSON.stringify(params)})`); this.sendCommandMsg(message); }); }, @@ -377,9 +401,21 @@ export const useWebSocketStore = defineStore({ const settingStore = useSettingStore() const operatorStore = useOperatorStore() const echartsStore = useEchartsStore() + + let $this = this; init.ws.onmessage = function (ev) { // console.log(JSON.parse(ev.data)) const { command, timeStamp } = JSON.parse(ev.data) + if ( undefined !== $this.eventHandlers[command] ) { + let data = JSON.parse(ev.data); + data = data.data; + for ( const handler of $this.eventHandlers[command] ) { + handler(data); + } + return ; + } + + switch (command) { case 'RealtimeSensorDataReport': const { sensor_data } = JSON.parse(ev.data);