Skip to content

Commit

Permalink
refactor: webChannel
Browse files Browse the repository at this point in the history
  • Loading branch information
杨帆 committed Sep 20, 2024
1 parent 40c7092 commit df33aa9
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 39 deletions.
96 changes: 57 additions & 39 deletions app/composables/webChannel.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
/* eslint-disable no-new */
import type { QTResponseType } from '~/types/qt'
import QWebChannel, { isQtClient } from '~/utils/web-channel.js'

interface QtWebSocketOptions {
onDataUpdated?: (data: any) => void
onDataUpdated?: (data: QTResponseType) => void
onConnected?: () => void
onDisconnected?: () => void
onError?: (error: any) => void
}

export function useWebChannel(options: QtWebSocketOptions = {}) {
const { onDataUpdated } = options
type CallbackId = string
type Callback = (data: QTResponseType) => void

const qtObject = ref<any>(null)
// const isConnected = ref(false)
let cacheQtObject: any = null
const globalCallbackMap = new Map<CallbackId, Callback>()

// const { status: wsStatus, send: wsSend, open, close } = useWebSocket('ws://your-websocket-url', {
// autoReconnect: true,
// onConnected: () => {
// isConnected.value = true
// onConnected?.()
// },
// onDisconnected: () => {
// isConnected.value = false
// onDisconnected?.()
// },
// onError,
// })
export function useWebChannel(options: QtWebSocketOptions = {}) {
const qtObject = ref<any>(null)

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

const connect = (callback: Callback): CallbackId => {
const id = `${Date.now()}-${Math.random().toString(36).substring(2)}`
globalCallbackMap.set(id, callback)
return id
}

const disconnect = (id: CallbackId): boolean => {
return globalCallbackMap.delete(id)
}

const handleDataUpdated = (data: QTResponseType) => {
window.console.log('DEBUG:未处理的QT原始消息', data)
if (data) {
globalCallbackMap.forEach((cb) => {
cb(data)
})
}
else {
console.error('data is null')
}
}

const initQtWebChannel = () => {
if (import.meta.env.DEV || !isQtClient) {
window.qt = {
webChannelTransport: {
send() {
window.console.log(`
QWebChannel simulator activated !
`)
window.console.log('QWebChannel simulator activated !')
},

onmessage: () => {
window.console.log(`
QWebChannel simulator activated !
`)
window.console.log('QWebChannel simulator activated !')
},
},
}
Expand All @@ -59,14 +67,14 @@ export function useWebChannel(options: QtWebSocketOptions = {}) {
if (channel.objects.myObject) {
window.console.log('myObject is available')
qtObject.value = channel.objects.myObject
qtObject.value.dataUpdated.connect((data: any) => {
if (data) {
onDataUpdated?.(data)
}
else {
console.error('data is null')
}
})
cacheQtObject = channel.objects.myObject

if (typeof qtObject.value.dataUpdated?.connect === 'function') {
qtObject.value.dataUpdated.connect(handleDataUpdated)
}
else {
console.error('myObject dataUpdated connect function not available')
}
}
else {
console.error('myObject is not available')
Expand All @@ -75,18 +83,28 @@ export function useWebChannel(options: QtWebSocketOptions = {}) {
}

onMounted(() => {
initQtWebChannel()
if (!cacheQtObject) {
initQtWebChannel()
}
else {
qtObject.value = cacheQtObject
// if (typeof qtObject.value.dataUpdated?.connect === 'function') {
// qtObject.value.dataUpdated.connect(handleDataUpdated)
// }
}
})

// onUnmounted(() => {
// close()
// })
// Watch for changes in the options.onDataUpdated callback
watch(() => options.onDataUpdated, (newCallback) => {
if (newCallback) {
connect(newCallback)
}
}, { immediate: true })

return {
// isConnected,
// wsStatus,
// sendWebSocketMessage: wsSend,
sendMessageToCpp,
connect,
disconnect,
qtObject,
}
}
36 changes: 36 additions & 0 deletions app/types/qt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export enum DataType {
TASK_INFO = '3',
VIDEO_FRAME = '101',
AI_ASSISTANT = '102',
SIGN_IN = '111',
}

export enum CameraId {
FACE = '0',
PTZ = '1',
}

export enum ReturnCode {
RECORDING_STATUS = '0',
VOICE_TO_TEXT = '1',
AI_INFERENCE = '2',
}

export enum State {
RECORDING = '2',
COMPLETED = '3',
}

export interface QTResponseType {
type: string
cam_id?: string
img?: string
payload?: any
ret?: string
state?: string
text?: string
line1?: string
line2?: string
line3?: string
err?: string
}

0 comments on commit df33aa9

Please sign in to comment.