|
|
@ -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); |
|
|
|