-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (76 loc) · 2.04 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
'use strict';
var DMX = require('DMX');
let ndDMX = require('./lib/ndDMX');
let dmxTypes = require('./lib/ndDMXTypes');
let ws = require('nodejs-websocket');
let port = 1337;
let debug = true;
/*
* Configure the universe
*/
let universe = {
name : 'dotjs',
master : {
driver : 'enttec-usb-dmx-pro',
usb_device : '/dev/cu.usbserial-EN193448'
},
slaves : {
light1 : {
type : dmxTypes.TYPE_STAIRVILLE_LED_PAR,
address : 1
},
light2 : {
type : dmxTypes.TYPE_STAIRVILLE_LED_PAR,
address : 4
},
fogMaschine : {
type : dmxTypes.TYPE_STAIRVILLE_S_150,
address : 16
}
}
};
// Initialize DMX
let dmx_connector = new DMX();
// Add universe for dotJS
dmx_connector.addUniverse(
universe.name,
universe.master.driver,
universe.master.usb_device
);
// Reset every device at startup
dmx_connector.updateAll(universe.name, 0);
// Initialize NERDDISCO DMX helper
let NERDDISCO_dmx = new ndDMX({ devices : universe.slaves });
/**
* Create a WebSocket server
*/
let server = ws.createServer(function (connection) {
console.log('New connection');
// Receive data
connection.on('text', function (data) {
if (debug) {
console.log(data);
}
data = JSON.parse(data);
/*
* Update DMX devices
*/
NERDDISCO_dmx.updateDevice(universe.slaves.light1, data.slice(0, 3));
NERDDISCO_dmx.updateDevice(universe.slaves.light2, data.slice(3, 6));
NERDDISCO_dmx.updateDevice(universe.slaves.fogMaschine, data.slice(6, 7));
if (debug) {
console.log(NERDDISCO_dmx.data);
}
// Update the universe
dmx_connector.update(universe.name, NERDDISCO_dmx.data);
});
connection.on('close', function (code, reason) {
console.log('Connection closed');
});
connection.on('error', function (error) {
if (error.code !== 'ECONNRESET') {
// Ignore ECONNRESET and re throw anything else
throw err;
}
});
}).listen(port);