-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
223 lines (194 loc) · 5.01 KB
/
index.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
'use strict';
const iconRenderer = require('./icon-renderer.js')
const StreamDeck = require('elgato-stream-deck')
function connectStreamDeck() {
try {
return StreamDeck.openStreamDeck()
} catch (error) {
console.error(error, error.stack)
return null
}
}
const streamDeck = connectStreamDeck()
const NUM_KEYS = streamDeck?.NUM_KEYS || 15
const ICON_SIZE = streamDeck?.ICON_SIZE || 72
const EventEmitter = require('events')
const eventEmitter = new EventEmitter()
const currentIcons = Array(NUM_KEYS).fill({})
const currentIconPngBuffers = Array(NUM_KEYS).fill(Buffer.from("", "utf-8"))
streamDeck?.clearAllKeys()
var webSocketServer
function setIcon(index, icon) {
if (icon.type === "iconFunction") {
setIcon(index, icon.iconFunction())
} else if (JSON.stringify(icon) != JSON.stringify(currentIcons[index])) {
currentIcons[index] = icon
setIconInternal(index, icon).catch(reason => eventEmitter.emit('error', new Error("error while setting icon: "+reason)))
}
}
async function setIconInternal(index, icon) {
try {
const iconBuffer = await iconRenderer.getIconRawBuffer(icon, ICON_SIZE)
currentIconPngBuffers[index] = await (await iconRenderer.getIconSharp(icon, ICON_SIZE)).png().toBuffer()
sendIconOverWebSocket(index)
streamDeck?.fillImage(index, iconBuffer);
} catch (error) {
eventEmitter.emit('error', new Error("error while drawing icon: "+error))
streamDeck?.fillImage(index, await iconRenderer.getIconBufferBlank(ICON_SIZE));
}
}
function sendAllIconsOverWebSocket() {
for (let i = 0; i < NUM_KEYS; i++) {
sendIconOverWebSocket(i)
}
}
function sendIconOverWebSocket(index) {
sendMessageOverWebSocket({
type: "icon",
index,
pngBase64: currentIconPngBuffers[index].toString('base64'),
})
}
function sendMessageOverWebSocket(messageObject) {
const message = JSON.stringify(messageObject)
webSocketServer.clients.forEach(webSocket => {
try {
webSocket.send(message)
} catch(e) {
console.error("error while sending to all clients via websocket", e)
}
})
}
streamDeck?.on('up', keyIndex => {
onButtonUp(keyIndex)
});
streamDeck?.on('down', keyIndex => {
onButtonDown(keyIndex)
});
function onButtonUp(keyIndex) {
if (currentPage.buttons.length >= keyIndex) {
const button = currentPage.buttons[keyIndex]
if (button && button.hasOwnProperty("onUp")) {
button.onUp()
}
}
}
function onButtonDown(keyIndex) {
if (currentPage.buttons.length >= keyIndex) {
const button = currentPage.buttons[keyIndex]
if (button && button.hasOwnProperty("onClick")) {
button.onClick()
}
if (button && button.hasOwnProperty("onDown")) {
button.onDown()
}
}
}
streamDeck?.on('error', error => {
eventEmitter.emit('error', error)
});
let currentPage = {
name: undefined,
buttons: [],
};
function showPage(page) {
currentPage = page
redraw()
}
function redraw() {
for (let i = 0; i < NUM_KEYS; i++) {
if (currentPage.buttons.length >= i+1) {
const button = currentPage.buttons[i]
if (button && button.hasOwnProperty("icon")) {
setIcon(i,button.icon)
} else {
setIcon(i,{type:"blank"})
}
} else {
setIcon(i,{type:"blank"})
}
}
}
function getKeyRowCount() {
if (NUM_KEYS == 6) {
return 2
}
if (NUM_KEYS == 15) {
return 3
}
if (NUM_KEYS == 32) {
return 4
}
throw "unknown streamdeck type"
}
function getKeyColumnCount() {
if (NUM_KEYS == 6) {
return 3
}
if (NUM_KEYS == 15) {
return 5
}
if (NUM_KEYS == 32) {
return 8
}
throw "unknown streamdeck type"
}
function setWebSocketServer(wss) {
webSocketServer = wss
}
function registerExpressWebview(expressApp, webSocketServer) {
require('./webview.js')({
expressApp,
simulateKeyDown: (keyIndex) => onButtonDown(keyIndex),
simulateKeyUp: (keyIndex) => onButtonUp(keyIndex),
getCurrentIconPngBuffer: (keyIndex) => currentIconPngBuffers[keyIndex],
getKeyRowCount,
getKeyColumnCount,
})
setWebSocketServer(webSocketServer)
setInterval(() => {
sendMessageOverWebSocket({
type: "keepalive",
})
}, 20000)
webSocketServer.on('connection', (webSocket) => {
webSocket.on('message', (message) => {
if (JSON.parse(message).type === "resend-all") {
sendAllIconsOverWebSocket()
}
})
})
}
setInterval(() => redraw(), 200)
const reactiveStreamdeck = {
showPage,
setBrightness: ((percentage) => streamDeck?.setBrightness(percentage)),
unstableApiUseAtYourOwnRisk: {
streamDeck,
},
NUM_KEYS: NUM_KEYS,
MAX_KEYS: 32,
onError: ((listener) => eventEmitter.on('error', listener)),
registerExpressWebview,
}
const pages = {}
const pageStack = []
const pagedStreamDeck = {
...reactiveStreamdeck,
addToPageStack: (name) => {
pageStack.push(name)
},
changePage: (name) => {
pageStack.push(name)
reactiveStreamdeck.showPage(pages[name])
},
getCurrentPageName: () => pageStack[pageStack.length-1],
getPageNames: () => Object.keys(pages),
addPage: page => pages[page.name] = page,
goBackToLastPage: () => {
pageStack.pop()
pagedStreamDeck.changePage(pageStack.pop())
},
}
reactiveStreamdeck.getPagedStreamdeck = () => pagedStreamDeck
module.exports = reactiveStreamdeck