-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlyncConnector.ts
53 lines (48 loc) · 1.77 KB
/
BlyncConnector.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import hid from 'node-hid'
import { BlyncLightProductsEnum } from '../enums'
import { BlyncProduct } from './BlyncProduct'
import { BlyncDevice } from './BlyncDevice'
export class BlyncConnector {
// Blync Light info obtained from DeviceAccess.cs
blyncProducts: BlyncProduct[] = [
// eslint-disable-next-line prettier/prettier
new BlyncProduct('2c0d', '0009', BlyncLightProductsEnum.BLYNCLIGHT_EMBRAVA_EMBEDDED, 12),
new BlyncProduct('0e53', '2519', BlyncLightProductsEnum.BLYNCLIGHT_MINI, 7),
new BlyncProduct('2c0d', '0003', BlyncLightProductsEnum.BLYNCLIGHT_MINI, 7),
// eslint-disable-next-line prettier/prettier
new BlyncProduct('2c0d', '000a', BlyncLightProductsEnum.BLYNCLIGHT_MINI, 13),
]
getDevices(): BlyncDevice[] {
const hidDevices = hid.devices() || []
return hidDevices
.filter((hidDevice: hid.Device) => {
// Check if hid device matches any blync devices
return (
hidDevice.path &&
this.blyncProducts.some((blyncProduct: BlyncProduct) => {
return blyncProduct.isBlyncMatch(
hidDevice.vendorId,
hidDevice.productId
)
})
)
// TODO: Test on Linux and OSX
// on macOS/Windows, dev.interface === -1, but on Raspbian shows as 0, so removing for now:
// && dev.interface === -1;
})
.map((blyncDevice: hid.Device) => {
return new BlyncDevice(new hid.HID(blyncDevice.path || ''))
})
}
getDevice(index: number): BlyncDevice {
index = +index || 0
const devices = this.getDevices()
if (index < 0) {
throw new Error('Invalid device index')
}
if (index >= devices.length) {
throw new Error('Device index #' + index + ' not found')
}
return devices[index]
}
}