-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (75 loc) · 2.57 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
const ws = require('nodejs-websocket')
var options = {
host: '192.168.1.132'
}
var artnet = require('artnet')(options);
const [, , _port] = process.argv
const port = _port || 3000
console.log('modv-integration', '|', 'WebSocket server on port', port)
/*
* Find the color in the "middle" which can be mapped to the cyberpunk goggles regardless how big
* the selected area in grab-canvas is
*/
const centerColor = dmxData => {
const { colors } = dmxData
let { selectionX } = dmxData
let centerIndex = 0
selectionX = parseInt(selectionX, 10)
// Even number
if (selectionX % 2 === 0) {
centerIndex = Math.floor((colors.length / 3 + selectionX) / 2)
} else {
centerIndex = Math.floor(colors.length / 3 / 2)
}
centerIndex *= 3
const color = [colors[centerIndex], colors[centerIndex + 1], colors[centerIndex + 2]]
return color
}
/*
* Create a WebSocket server
*/
const server = ws.createServer(connection => {
console.log('modv-integration', '|', 'New connection by', connection.path)
// Receive data
connection.on('text', dmxData => {
dmxData = JSON.parse(dmxData)
const colorsLength = dmxData.colors.length;
for(let i=0; i < colorsLength; i+=3) {
const r = dmxData.colors[i];
const g = dmxData.colors[i+1];
const b = dmxData.colors[i+2];
artnet.set(0, i+1, [r,g,b]);
}
// Broadcast to all connected clients
server.connections.forEach(con => {
if (con.readyState !== con.OPEN) {
return
}
// The client connection came from luminave
if (con.path === '/luminave') {
con.sendText(JSON.stringify(dmxData))
}
// The client connection came from cyberpunk goggles
if (con.path === '/cyberpunk') {
// con.sendBinary(Uint8Array.from(centerColor(dmxData)))
// artnet.set(0, 0, ...centerColor(dmxData));
}
})
})
/*
* WebSocket Close Codes: https://www.iana.org/assignments/websocket/websocket.xml#close-code-number
*/
connection.on('close', code => {
console.log('modv-integration', '|', 'Connection closed by', connection.path, 'with code', code)
})
connection.on('error', error => {
console.log('modv-integration', '|', 'Error by', connection.path, 'with code', error.code)
if (error.code === 'EHOSTDOWN' || error.code === 'ETIMEDOUT' || error.code === 'EPIPE') {
return
}
// Ignore ECONNRESET and re throw anything else
if (error.code !== 'ECONNRESET') {
throw error
}
})
}).listen(port, '0.0.0.0')