-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
65 lines (54 loc) · 1.75 KB
/
index.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
54
55
56
57
58
59
60
61
62
63
64
65
import { DeviceController, DeviceMessageListener } from '@remote-mixer/types'
import { logger } from '@remote-mixer/utils'
import { delay } from '../../util/time'
import { connect, sendMessage, X32Options } from './connection'
import { deviceConfig } from './device-config'
import {
getChangeMessage,
getMeterRequest,
interpretIncomingMessage,
} from './protocol'
import { sync } from './sync'
// The X32 sends its meters 20 times a second, we only want to use half of them
let skipNextMeterMessage = false
export default class BehringerX32DeviceController implements DeviceController {
deviceConfig = deviceConfig
constructor(
private listener: DeviceMessageListener,
options: X32Options
) {
connect(message => {
const internalMessage = interpretIncomingMessage(message)
if (internalMessage && typeof internalMessage === 'object') {
if (internalMessage.type === 'meters') {
skipNextMeterMessage = !skipNextMeterMessage
if (!skipNextMeterMessage) this.listener(internalMessage)
} else {
this.listener(internalMessage)
}
}
if (message === null) {
logger.debug('<== unhandled message', message)
}
}, options).then(async () => {
await sync()
await delay(200)
sendMessage(getMeterRequest())
setInterval(() => {
sendMessage(getMeterRequest())
}, 9000)
})
}
change(category: string, id: string, property: string, value: unknown): void {
const message = getChangeMessage(category, id, property, value)
logger.debug(
`==> change ${category} ${id} ${property} ${value} =>`,
message
)
if (message) sendMessage(message)
}
sync(): void {
logger.debug('==> synchronizing...')
sync()
}
}