Skip to content

Commit

Permalink
feat: replace usb-detection with usb
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Jan 23, 2022
1 parent 71f47cb commit 83e5ef0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 61 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"exit-hook": "^2.2.1",
"meow": "^9.0.0",
"sharp": "^0.28.3",
"usb-detection": "^4.10.0"
"usb": "^2.0.3"
},
"resolutions": {
"electron-builder/**/app-builder-bin": "3.5.13"
Expand Down
42 changes: 28 additions & 14 deletions src/devices.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CompanionSatelliteClient } from './client'
import { listStreamDecks, openStreamDeck, StreamDeck } from 'elgato-stream-deck'
import * as usbDetect from 'usb-detection'
import { usb } from 'usb'
import { ImageWriteQueue } from './writeQueue'
import sharp = require('sharp')
import EventEmitter = require('events')
Expand Down Expand Up @@ -29,9 +29,18 @@ export class DeviceManager {
this.deviceIdMap = new Map()
this.cardGenerator = new CardGenerator()

usbDetect.startMonitoring()
usbDetect.on('add:4057', (dev) => this.foundDevice(dev))
usbDetect.on('remove:4057', (dev) => this.removeDevice(dev))
usb.on('attach', (dev) => {
if (dev.deviceDescriptor.idVendor === 0x0fd9) {
this.foundDevice(dev)
}
})
usb.on('detach', (dev) => {
if (dev.deviceDescriptor.idVendor === 0x0fd9) {
this.removeDevice(dev)
}
})
// Don't block process exit with the watching
usb.unrefHotplugEvents()

this.statusString = 'Connecting'

Expand Down Expand Up @@ -102,7 +111,7 @@ export class DeviceManager {
}

public close(): void {
usbDetect.stopMonitoring()
// usbDetect.stopMonitoring()

for (const dev of this.devices.values()) {
try {
Expand All @@ -116,7 +125,7 @@ export class DeviceManager {
private clearIdMap(): void {
console.log('clear id map')
for (const dev of this.devices.values()) {
const deck = (dev.deck as unknown) as EventEmitter
const deck = dev.deck as unknown as EventEmitter
deck.removeAllListeners('down')
deck.removeAllListeners('up')
}
Expand All @@ -132,27 +141,31 @@ export class DeviceManager {
return [serial, sd]
}

private foundDevice(dev: usbDetect.Device): void {
console.log('Found a device', dev)
private foundDevice(dev: usb.Device): void {
console.log('Found a device', dev.deviceDescriptor)

// most of the time it is available now
this.scanDevices()
// sometimes it ends up delayed
setTimeout(() => this.scanDevices(), 1000)
}

private removeDevice(dev: usbDetect.Device): void {
console.log('Lost a device', dev)
const dev2 = this.devices.get(dev.serialNumber)
private removeDevice(_dev: usb.Device): void {
// Rescan after a short timeout
// setTimeout(() => this.scanDevices(), 100)
// console.log('Lost a device', dev.deviceDescriptor)
// this.cleanupDeviceById(dev.serialNumber)
}
private cleanupDeviceById(id: string): void {
const dev2 = this.devices.get(id)
if (dev2) {
// cleanup
this.devices.delete(dev.serialNumber)
const k = Array.from(this.deviceIdMap.entries()).find((e) => e[1] === dev.serialNumber)
this.devices.delete(id)
const k = Array.from(this.deviceIdMap.entries()).find((e) => e[1] === id)
if (k) {
this.deviceIdMap.delete(k[0])
this.client.removeDevice(k[0])
}

dev2.queue?.abort()
try {
dev2.deck.close()
Expand Down Expand Up @@ -232,6 +245,7 @@ export class DeviceManager {

sd.on('error', (e) => {
console.error('device error', e)
this.cleanupDeviceById(serial)
})
} catch (e) {
console.log(`Open "${path}" failed: ${e}`)
Expand Down
70 changes: 24 additions & 46 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@
dependencies:
"@types/node" "*"

"@types/w3c-web-usb@^1.0.4":
version "1.0.5"
resolved "https://registry.yarnpkg.com/@types/w3c-web-usb/-/w3c-web-usb-1.0.5.tgz#90284d17f35de981670c85d29053ae8b88fa5543"
integrity sha512-dYolx2XWesl1TMu+1BjtjU6eC6c2zZ2VDKhjU4f/mtR3+UBfMW6h1tPCQt7leY5Y8JBg0Fe/mMnoDMkPPNX9sw==

"@types/yargs-parser@*":
version "20.2.1"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-20.2.1.tgz#3b9ce2489919d9e4fea439b76916abc34b2df129"
Expand Down Expand Up @@ -577,7 +582,7 @@ binary@~0.3.0:
buffers "~0.1.1"
chainsaw "~0.1.0"

bindings@^1.3.0, bindings@^1.5.0:
bindings@^1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.5.0.tgz#10353c9e945334bc0511a6d90b38fbc7c9c504df"
integrity sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==
Expand Down Expand Up @@ -1748,11 +1753,6 @@ esutils@^2.0.2:
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64"
integrity sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==

eventemitter2@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/eventemitter2/-/eventemitter2-5.0.1.tgz#6197a095d5fb6b57e8942f6fd7eaad63a09c9452"
integrity sha1-YZegldX7a1folC9v1+qtY6CclFI=

eventemitter3@^4.0.7:
version "4.0.7"
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.7.tgz#2de9b68f6528d5644ef5c59526a1b4a07306169f"
Expand Down Expand Up @@ -3068,11 +3068,6 @@ ms@^2.1.1:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==

nan@^2.13.2:
version "2.15.0"
resolved "https://registry.yarnpkg.com/nan/-/nan-2.15.0.tgz#3f34a473ff18e15c1b5626b62903b5ad6e665fee"
integrity sha512-8ZtvEnA2c5aYCZYd1cvgdnU6cqwixRoYg70xPLWUws5ORTa/lnw+u4amixRS/Ac5U5mQVgp9pnlSUnbNWFaWZQ==

napi-build-utils@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/napi-build-utils/-/napi-build-utils-1.0.2.tgz#b1fddc0b2c46e380a0b7a76f984dd47c41a13806"
Expand All @@ -3088,7 +3083,7 @@ neo-async@^2.6.0:
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.2.tgz#b4aafb93e3aeb2d8174ca53cf163ab7d7308305f"
integrity sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==

node-abi@^2.21.0, node-abi@^2.7.0:
node-abi@^2.21.0:
version "2.30.0"
resolved "https://registry.yarnpkg.com/node-abi/-/node-abi-2.30.0.tgz#8be53bf3e7945a34eea10e0fc9a5982776cf550b"
integrity sha512-g6bZh3YCKQRdwuO/tSZZYJAw622SjsRfJ2X0Iy4sSOHZ34/sPPdVBn8fev2tj7njzLwuqPw9uMtGsGkO5kIQvg==
Expand All @@ -3100,6 +3095,16 @@ node-addon-api@^3.0.2, node-addon-api@^3.2.0, node-addon-api@^3.2.1:
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-3.2.1.tgz#81325e0a2117789c0128dab65e7e38f07ceba161"
integrity sha512-mmcei9JghVNDYydghQmeDX8KoAm0FAiYyIcUt/N4nhyAipB17pllZQDOJD2fotxABnt4Mdz+dKTO7eftLg4d0A==

node-addon-api@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-4.2.0.tgz#117cbb5a959dff0992e1c586ae0393573e4d2a87"
integrity sha512-eazsqzwG2lskuzBqCGPi7Ac2UgOoMz8JVOXVhTvvPDYhthvNpefx8jWD8Np7Gv+2Sz0FlPWZk0nJV0z598Wn8Q==

node-gyp-build@^4.3.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/node-gyp-build/-/node-gyp-build-4.3.0.tgz#9f256b03e5826150be39c764bf51e993946d71a3"
integrity sha512-iWjXZvmboq0ja1pUGULQBexmxq8CV4xBhX7VDOTbL7ZR4FOowwY/VOtRxBN/yKxmdGoIp4j5ysNT4u3S2pDQ3Q==

node-hid@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/node-hid/-/node-hid-2.1.1.tgz#f83c8aa0bb4e6758b5f7383542477da93f67359d"
Expand All @@ -3109,11 +3114,6 @@ node-hid@^2.1.1:
node-addon-api "^3.0.2"
prebuild-install "^6.0.0"

noop-logger@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/noop-logger/-/noop-logger-0.1.1.tgz#94a2b1633c4f1317553007d8966fd0e841b6a4c2"
integrity sha1-lKKxYzxPExdVMAfYlm/Q6EG2pMI=

nopt@^4.0.1:
version "4.0.3"
resolved "https://registry.yarnpkg.com/nopt/-/nopt-4.0.3.tgz#a375cad9d02fd921278d954c2254d5aa57e15e48"
Expand Down Expand Up @@ -3451,27 +3451,6 @@ please-upgrade-node@^3.2.0:
dependencies:
semver-compare "^1.0.0"

prebuild-install@^5.3.5:
version "5.3.6"
resolved "https://registry.yarnpkg.com/prebuild-install/-/prebuild-install-5.3.6.tgz#7c225568d864c71d89d07f8796042733a3f54291"
integrity sha512-s8Aai8++QQGi4sSbs/M1Qku62PFK49Jm1CbgXklGz4nmHveDq0wzJkg7Na5QbnO1uNH8K7iqx2EQ/mV0MZEmOg==
dependencies:
detect-libc "^1.0.3"
expand-template "^2.0.3"
github-from-package "0.0.0"
minimist "^1.2.3"
mkdirp-classic "^0.5.3"
napi-build-utils "^1.0.1"
node-abi "^2.7.0"
noop-logger "^0.1.1"
npmlog "^4.0.1"
pump "^3.0.0"
rc "^1.2.7"
simple-get "^3.0.3"
tar-fs "^2.0.0"
tunnel-agent "^0.6.0"
which-pm-runs "^1.0.0"

prebuild-install@^6.0.0, prebuild-install@^6.1.2, prebuild-install@^6.1.3:
version "6.1.4"
resolved "https://registry.yarnpkg.com/prebuild-install/-/prebuild-install-6.1.4.tgz#ae3c0142ad611d58570b89af4986088a4937e00f"
Expand Down Expand Up @@ -4601,15 +4580,14 @@ url-parse-lax@^3.0.0:
dependencies:
prepend-http "^2.0.0"

usb-detection@^4.10.0:
version "4.10.0"
resolved "https://registry.yarnpkg.com/usb-detection/-/usb-detection-4.10.0.tgz#0f8a3b8965a5e4e7fbee1667971ca97e455ed11f"
integrity sha512-YUzVWXwfSviE2pInXCKYXhR5heY9GUzlWsdZYxb/Br1Xela6P31A0KDHm7XW0Wsku1HwrokZx+/OD8cZSPHR3w==
usb@^2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/usb/-/usb-2.0.3.tgz#c8a420f9a02bcdf644790eb4fa46506065c16a0d"
integrity sha512-UnUtZK5ebhDdNTjcO+cHFnpo25iifNm+E7F4f5g4pCUH+0M8b4SiVqILNzqz9iqQNK326PGawOYdkTJbplqsIg==
dependencies:
bindings "^1.3.0"
eventemitter2 "^5.0.1"
nan "^2.13.2"
prebuild-install "^5.3.5"
"@types/w3c-web-usb" "^1.0.4"
node-addon-api "^4.2.0"
node-gyp-build "^4.3.0"

utf8-byte-length@^1.0.1:
version "1.0.4"
Expand Down

0 comments on commit 83e5ef0

Please sign in to comment.