Skip to content

Commit df33aa9

Browse files
author
杨帆
committed
refactor: webChannel
1 parent 40c7092 commit df33aa9

File tree

2 files changed

+93
-39
lines changed

2 files changed

+93
-39
lines changed

app/composables/webChannel.ts

+57-39
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
11
/* eslint-disable no-new */
2+
import type { QTResponseType } from '~/types/qt'
23
import QWebChannel, { isQtClient } from '~/utils/web-channel.js'
34

45
interface QtWebSocketOptions {
5-
onDataUpdated?: (data: any) => void
6+
onDataUpdated?: (data: QTResponseType) => void
67
onConnected?: () => void
78
onDisconnected?: () => void
89
onError?: (error: any) => void
910
}
1011

11-
export function useWebChannel(options: QtWebSocketOptions = {}) {
12-
const { onDataUpdated } = options
12+
type CallbackId = string
13+
type Callback = (data: QTResponseType) => void
1314

14-
const qtObject = ref<any>(null)
15-
// const isConnected = ref(false)
15+
let cacheQtObject: any = null
16+
const globalCallbackMap = new Map<CallbackId, Callback>()
1617

17-
// const { status: wsStatus, send: wsSend, open, close } = useWebSocket('ws://your-websocket-url', {
18-
// autoReconnect: true,
19-
// onConnected: () => {
20-
// isConnected.value = true
21-
// onConnected?.()
22-
// },
23-
// onDisconnected: () => {
24-
// isConnected.value = false
25-
// onDisconnected?.()
26-
// },
27-
// onError,
28-
// })
18+
export function useWebChannel(options: QtWebSocketOptions = {}) {
19+
const qtObject = ref<any>(null)
2920

3021
const sendMessageToCpp = (message: any) => {
3122
if (qtObject.value && typeof qtObject.value.receiveMessage === 'function') {
@@ -36,20 +27,37 @@ export function useWebChannel(options: QtWebSocketOptions = {}) {
3627
}
3728
}
3829

30+
const connect = (callback: Callback): CallbackId => {
31+
const id = `${Date.now()}-${Math.random().toString(36).substring(2)}`
32+
globalCallbackMap.set(id, callback)
33+
return id
34+
}
35+
36+
const disconnect = (id: CallbackId): boolean => {
37+
return globalCallbackMap.delete(id)
38+
}
39+
40+
const handleDataUpdated = (data: QTResponseType) => {
41+
window.console.log('DEBUG:未处理的QT原始消息', data)
42+
if (data) {
43+
globalCallbackMap.forEach((cb) => {
44+
cb(data)
45+
})
46+
}
47+
else {
48+
console.error('data is null')
49+
}
50+
}
51+
3952
const initQtWebChannel = () => {
4053
if (import.meta.env.DEV || !isQtClient) {
4154
window.qt = {
4255
webChannelTransport: {
4356
send() {
44-
window.console.log(`
45-
QWebChannel simulator activated !
46-
`)
57+
window.console.log('QWebChannel simulator activated !')
4758
},
48-
4959
onmessage: () => {
50-
window.console.log(`
51-
QWebChannel simulator activated !
52-
`)
60+
window.console.log('QWebChannel simulator activated !')
5361
},
5462
},
5563
}
@@ -59,14 +67,14 @@ export function useWebChannel(options: QtWebSocketOptions = {}) {
5967
if (channel.objects.myObject) {
6068
window.console.log('myObject is available')
6169
qtObject.value = channel.objects.myObject
62-
qtObject.value.dataUpdated.connect((data: any) => {
63-
if (data) {
64-
onDataUpdated?.(data)
65-
}
66-
else {
67-
console.error('data is null')
68-
}
69-
})
70+
cacheQtObject = channel.objects.myObject
71+
72+
if (typeof qtObject.value.dataUpdated?.connect === 'function') {
73+
qtObject.value.dataUpdated.connect(handleDataUpdated)
74+
}
75+
else {
76+
console.error('myObject dataUpdated connect function not available')
77+
}
7078
}
7179
else {
7280
console.error('myObject is not available')
@@ -75,18 +83,28 @@ export function useWebChannel(options: QtWebSocketOptions = {}) {
7583
}
7684

7785
onMounted(() => {
78-
initQtWebChannel()
86+
if (!cacheQtObject) {
87+
initQtWebChannel()
88+
}
89+
else {
90+
qtObject.value = cacheQtObject
91+
// if (typeof qtObject.value.dataUpdated?.connect === 'function') {
92+
// qtObject.value.dataUpdated.connect(handleDataUpdated)
93+
// }
94+
}
7995
})
8096

81-
// onUnmounted(() => {
82-
// close()
83-
// })
97+
// Watch for changes in the options.onDataUpdated callback
98+
watch(() => options.onDataUpdated, (newCallback) => {
99+
if (newCallback) {
100+
connect(newCallback)
101+
}
102+
}, { immediate: true })
84103

85104
return {
86-
// isConnected,
87-
// wsStatus,
88-
// sendWebSocketMessage: wsSend,
89105
sendMessageToCpp,
106+
connect,
107+
disconnect,
90108
qtObject,
91109
}
92110
}

app/types/qt.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export enum DataType {
2+
TASK_INFO = '3',
3+
VIDEO_FRAME = '101',
4+
AI_ASSISTANT = '102',
5+
SIGN_IN = '111',
6+
}
7+
8+
export enum CameraId {
9+
FACE = '0',
10+
PTZ = '1',
11+
}
12+
13+
export enum ReturnCode {
14+
RECORDING_STATUS = '0',
15+
VOICE_TO_TEXT = '1',
16+
AI_INFERENCE = '2',
17+
}
18+
19+
export enum State {
20+
RECORDING = '2',
21+
COMPLETED = '3',
22+
}
23+
24+
export interface QTResponseType {
25+
type: string
26+
cam_id?: string
27+
img?: string
28+
payload?: any
29+
ret?: string
30+
state?: string
31+
text?: string
32+
line1?: string
33+
line2?: string
34+
line3?: string
35+
err?: string
36+
}

0 commit comments

Comments
 (0)