fix: added a workflow to properly disconnect the mouse when the driver stops

This commit is contained in:
Kevin
2025-06-06 15:00:40 -05:00
parent e6c31a8aea
commit 5fda3093bb
3 changed files with 118 additions and 47 deletions

View File

@ -228,6 +228,7 @@ class _3DMouseThreeJSWindows implements _3DconnexionMiddleware {
_camera: PerspectiveCamera | OrthographicCamera | null
appName: string
debug: boolean
disconnectionCallback: () => void
gl: {
fov: number
near: number
@ -251,14 +252,23 @@ class _3DMouseThreeJSWindows implements _3DconnexionMiddleware {
console.log(`prefix ${message} data:${data}`)
}
constructor({ camera, canvasId, appName, debug = false, TRACE_MESSAGES = false }:{
camera: PerspectiveCamera | OrthographicCamera,
canvasId: string,
appName: string,
debug: boolean,
constructor({
camera,
canvasId,
appName,
debug = false,
TRACE_MESSAGES = false,
disconnectCallback,
}: {
camera: PerspectiveCamera | OrthographicCamera
canvasId: string
appName: string
debug: boolean
TRACE_MESSAGES: boolean
disconnectCallback: () => void
}) {
this._camera = null
this.disconnectionCallback = disconnectCallback
const canvas: HTMLElement | null = document.getElementById(canvasId)
if (canvas instanceof HTMLCanvasElement === false) {
@ -273,8 +283,14 @@ class _3DMouseThreeJSWindows implements _3DconnexionMiddleware {
throw new Error(message)
}
if (!(camera instanceof PerspectiveCamera === true || camera instanceof OrthographicCamera === true)) {
const message = 'camera is not a perpsective camera or a orthographic camera'
if (
!(
camera instanceof PerspectiveCamera === true ||
camera instanceof OrthographicCamera === true
)
) {
const message =
'camera is not a perpsective camera or a orthographic camera'
this.log(message)
throw new Error(message)
}
@ -330,7 +346,11 @@ class _3DMouseThreeJSWindows implements _3DconnexionMiddleware {
}
set camera(newCamera) {
this._camera = newCamera instanceof PerspectiveCamera || newCamera instanceof OrthographicCamera ? newCamera.clone() : newCamera
this._camera =
newCamera instanceof PerspectiveCamera ||
newCamera instanceof OrthographicCamera
? newCamera.clone()
: newCamera
}
render(now: number) {
@ -411,8 +431,7 @@ class _3DMouseThreeJSWindows implements _3DconnexionMiddleware {
}
if (this.camera instanceof PerspectiveCamera === false) {
const message =
'Camear is not perspective, unable to getFov, brick!'
const message = 'Camear is not perspective, unable to getFov, brick!'
this.log(message)
throw new Error(message)
}
@ -421,7 +440,6 @@ class _3DMouseThreeJSWindows implements _3DconnexionMiddleware {
2 *
Math.atan(
Math.atan2(this.camera.fov * Math.PI, 360.0) *
Math.sqrt(1 + this.camera.aspect * this.camera.aspect)
)
if (this.TRACE_MESSAGES) console.log('fov=' + (fov * 180.0) / Math.PI)
@ -458,7 +476,14 @@ class _3DMouseThreeJSWindows implements _3DconnexionMiddleware {
// Required for the pivot and zoom algorithms.
const unit = 100
const a : BoundingBox = [-unit / 2, -unit / 2, -unit / 2, unit / 2, unit / 2, unit / 2]
const a: BoundingBox = [
-unit / 2,
-unit / 2,
-unit / 2,
unit / 2,
unit / 2,
unit / 2,
]
this.log('getModelExtents', a)
return a
}
@ -510,7 +535,8 @@ class _3DMouseThreeJSWindows implements _3DconnexionMiddleware {
// coordinates as min = [left, bottom, -far] and max = [right, top, far].
// Required for orthographic projections.
if (this.camera instanceof PerspectiveCamera) {
const message = 'State is mismatched, camera says perspective but the navigation library says orthographc, in getViewExtents'
const message =
'State is mismatched, camera says perspective but the navigation library says orthographc, in getViewExtents'
this.log(message)
throw new Error(message)
}
@ -552,7 +578,8 @@ class _3DMouseThreeJSWindows implements _3DconnexionMiddleware {
}
if (this.camera instanceof PerspectiveCamera === false) {
const message = 'Camear is not perspective, unable to getViewFrustum, brick!'
const message =
'Camear is not perspective, unable to getViewFrustum, brick!'
this.log(message)
throw new Error(message)
}
@ -576,7 +603,14 @@ class _3DMouseThreeJSWindows implements _3DconnexionMiddleware {
this.camera.far +
']'
)
const a : ViewExtents = [left, -left, bottom, -bottom, this.camera.near, this.camera.far]
const a: ViewExtents = [
left,
-left,
bottom,
-bottom,
this.camera.near,
this.camera.far,
]
this.log('getViewFrustum', a)
return a
}
@ -641,7 +675,8 @@ class _3DMouseThreeJSWindows implements _3DconnexionMiddleware {
}
if (this.camera instanceof PerspectiveCamera) {
const message = 'State is mismatched, camera says perspective but the navigation library says orthographc, in setViewExtents'
const message =
'State is mismatched, camera says perspective but the navigation library says orthographc, in setViewExtents'
this.log(message)
throw new Error(message)
}
@ -778,9 +813,10 @@ class _3DMouseThreeJSWindows implements _3DconnexionMiddleware {
if (this.TRACE_MESSAGES) {
console.log('3Dconnexion NL-Proxy disconnected ' + reason)
}
this.disconnectionCallback()
}
init3DMouse(timeout?: number) : Promise<{value: boolean, message: string}> {
init3DMouse(timeout?: number): Promise<{ value: boolean; message: string }> {
this.log('init3DMouse')
this.spaceMouse = new _3Dconnexion(this)
if (!this.spaceMouse) {
@ -797,7 +833,10 @@ class _3DMouseThreeJSWindows implements _3DconnexionMiddleware {
if (this.spaceMouse) {
setTimeout(() => {
if (!this.spaceMouse) {
return reject({value: false, message: 'spaceMouse is missing, this is bad.'})
return reject({
value: false,
message: 'spaceMouse is missing, this is bad.',
})
}
let message = '3DConnexion mouse is connected'
let success = true

View File

@ -28,6 +28,7 @@ export const _3DMouseMachine = setup({
canvasId: string
/** Allow null because of internal retry, it will fail if this is null, we cannot have a default camera*/
camera: PerspectiveCamera | OrthographicCamera | null
onDisconnect : () => void
}
}
| {
@ -36,6 +37,9 @@ export const _3DMouseMachine = setup({
}
| {
type: _3DMouseMachineEvents.error_connect
}
| {
type: _3DMouseMachineEvents.disconnect
},
},
actions: {},
@ -50,6 +54,7 @@ export const _3DMouseMachine = setup({
debug: boolean
canvasId: string
camera: PerspectiveCamera | OrthographicCamera | null
onDisconnect: () => void
}
}): Promise<_3DMouseThreeJSWindows> => {
console.error('I AM CONNECTING TOO MANY TIMES')
@ -85,12 +90,20 @@ export const _3DMouseMachine = setup({
// camera: input.camera.clone(),
// })
// delete old mouse before creating a new one!
// This is important when someone disconnects and we reconnect
if (input.context._3dMouse) {
input.context._3dMouse.destroy()
}
const the3DMouse = new _3DMouseThreeJSWindows({
// Name needs to be registered in the python proxy server!
appName: input.name,
debug: input.debug,
canvasId: input.canvasId,
camera: input.camera.clone(),
TRACE_MESSAGES: true,
disconnectCallback: input.onDisconnect
})
/**
@ -119,6 +132,12 @@ export const _3DMouseMachine = setup({
/** retry 3 times before the user needs to manually click a connect button to retry */
maxRetries: 3,
}),
on: {
[_3DMouseMachineEvents.disconnect]: {
// root state
target: '.' + _3DMouseMachineStates.waitingToConnect
}
},
states: {
[_3DMouseMachineStates.waitingToConnect]: {
on: {
@ -139,14 +158,20 @@ export const _3DMouseMachine = setup({
invoke: {
id: _3DMouseMachineActors.connect,
src: _3DMouseMachineActors.connect,
input: ({ context, event }) => {
input: ({ context, event, self }) => {
assertEvent(event, _3DMouseMachineEvents.connect)
const onDisconnectHelperFunction = () => {
self.send({type: _3DMouseMachineEvents.disconnect})
}
return {
context,
name: event.data.name,
debug: event.data.debug,
canvasId: event.data.canvasId,
camera: event.data.camera,
onDisconnect: onDisconnectHelperFunction
}
},
onDone: {
@ -195,8 +220,13 @@ export const _3DMouseMachine = setup({
invoke: {
id: _3DMouseMachineActors.connect,
src: _3DMouseMachineActors.connect,
input: ({ context, event }) => {
input: ({ context, event, self }) => {
assertEvent(event, _3DMouseMachineEvents.error_connect)
const onDisconnectHelperFunction = () => {
self.send({type: _3DMouseMachineEvents.disconnect})
}
let { name, debug, canvasId, camera } =
context.lastConfigurationForConnection || {
name: '',
@ -213,6 +243,7 @@ export const _3DMouseMachine = setup({
debug,
canvasId,
camera,
onDisconnect: onDisconnectHelperFunction
}
},
onDone: {

View File

@ -32,6 +32,7 @@ export enum _3DMouseMachineEvents {
connect = 'connect',
done_connect = donePrefix + 'connect',
error_connect = errorPrefix + 'connect',
disconnect = 'disconnect'
}
export enum _3DMouseMachineActors {