From 893f3c2b2eaf44f12859ac3ad3e0f8cffa8a1a06 Mon Sep 17 00:00:00 2001 From: sige Date: Fri, 17 May 2024 16:00:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=8F=98=E6=9B=B4=E6=97=B6?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=E5=88=B0=E5=90=8E=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/info/ExtDeviceInfo.vue | 22 +++++++++++++----- src/store/modules/websocket.js | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/components/info/ExtDeviceInfo.vue b/src/components/info/ExtDeviceInfo.vue index b86882f..dd720b1 100644 --- a/src/components/info/ExtDeviceInfo.vue +++ b/src/components/info/ExtDeviceInfo.vue @@ -10,7 +10,7 @@

{{dehumidifyBeforeDisinfectionThreshold}}%

{{ dehumidifyBeforeDisinfectionEnable ? '使能' : '未使能'}}
@@ -19,14 +19,14 @@

{{dehumidifyAfterDisinfectionThreshold}}%

{{ dehumidifyAfterDisinfectionEnable ? '使能' : '未使能' }}

消毒后降解

{{ degradeAfterDisinfectionEnable ? '使能' : '未使能' }}
@@ -70,9 +70,13 @@ diff --git a/src/store/modules/websocket.js b/src/store/modules/websocket.js index d324ff8..3068714 100644 --- a/src/store/modules/websocket.js +++ b/src/store/modules/websocket.js @@ -24,6 +24,11 @@ export const useWebSocketStore = defineStore({ socketCommandInstance: null, // 事件上报websocket 实例 socketEventInstance: null, + + // Call ID Counter + callIdCounter : 0, + // Call Resolve Handler Map + callPromiseHandlers : {}, } }, // actions @@ -43,8 +48,23 @@ export const useWebSocketStore = defineStore({ const historyStore = useHistoryStore() const sealStore = useSealStore() init.connect() + + let $this = this; init.ws.onmessage = function (ev) { const { messageId, timeStamp } = JSON.parse(ev.data) + 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); + if ( 0 === response.ackcode ) { + handler.reject(response.reason); + } else { + handler.resolve(response.result); + } + return; + } + console.log(JSON.parse(ev.data)) switch (messageId) { case 'getState': @@ -326,6 +346,28 @@ export const useWebSocketStore = defineStore({ sendCommandMsg(message) { this.socketCommandInstance?.msg(message) }, + + // call and wait for response + call( command, params=null ) { + this.callIdCounter += 1; + if ( this.callIdCounter > 1000000 ) { + this.callIdCounter = 0; + } + const callId = `call-${this.callIdCounter}`; + return new Promise(( resolve, reject ) => { + let message = {}; + message.command = command; + message.messageId = callId; + if ( null !== params ) { + message.params = params; + } + this.callPromiseHandlers[callId] = { resolve, reject, message }; + console.log('call-request', message); + this.sendCommandMsg(message); + }); + }, + + initEventSocket() { const url = import.meta.env.VITE_BASE_WS2_URL const init = new Socket(url)