forked from sinedied/dmx-hue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
280 lines (253 loc) · 9.7 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
'use strict';
const minimist = require('minimist');
const inquirer = require('inquirer');
const chalk = require('chalk');
const Util = require('./lib/util');
const Hue = require('./lib/hue');
const ArtNet = require('./lib/artnet');
const pkg = require('./package.json');
const help =
`${chalk.bold('Usage:')} dmx-hue [setup] [options]
Create an ArtNet DMX<>Hue bridge.
${chalk.bold('Options:')}
-h, --host Host address to listen on [default: '0.0.0.0']
-a, --address Set DMX address (range 1-511) [default: 1]
-u, --universe Art-Net universe [default: 0]
-t, --transition Set transition time in ms [default: 100]
Can also be set to 'channel' to enable a dedicated DMX
channel on which 1 step equals 100ms.
-c, --colorloop Enable colorloop feature
When enabled, setting all RGB channels of a light to 1 will
enable colorloop mode.
-n, --no-limit Disable safety rate limiting
Warning: when this option is enabled, make sure to not send
more than <number_of_lights>/10 updates per second, or you
might overload your Hue bridge.
Note: options overrides settings saved during setup.
${chalk.bold('Commands:')}
setup Configure hue bridge and DMX options
-l, --list List bridges on the network
-i, --ip Set bridge IP (use first bridge if not specified)
--force Force bridge setup if already configured
`;
class DmxHue {
constructor(args) {
this._args = minimist(args, {
boolean: ['list', 'force', 'help', 'version', 'colorloop', 'no-limit'],
string: ['ip', 'host', 'transition'],
number: ['address', 'universe'],
alias: {
l: 'list',
i: 'ip',
h: 'host',
a: 'address',
t: 'transition',
c: 'colorloop',
n: 'no-limit',
u: 'universe'
}
});
this._hue = new Hue();
this._lastUpdate = 0;
this._delayedUpdate = null;
}
start(options) {
if (options.address <= 0 || options.address > 511) {
Util.exit('Invalid DMX address');
}
options = Object.assign({}, options);
this._hue
.getLights()
.then(lights => {
const ordered = options.order.reduce((r, id) => {
const light = lights.find(light => light.id === id.toString());
if (light && !options.disabled[id]) {
r.push(light);
}
return r;
}, []);
const remaining = lights.filter(light => !options.disabled[light.id] && !ordered.find(o => light.id === o.id));
options.lights = ordered.concat(remaining);
options.transitionChannel = options.transition === 'channel';
options.colors = {};
const dmxChannelCount = (3 * options.lights.length) + (options.transitionChannel ? 1 : 0);
const extraDmxAddress = options.address + dmxChannelCount - 512;
if (extraDmxAddress >= 0) {
console.warn(chalk.yellow('Warning: not enough DMX channels, some lights will be unavailable'));
const lightsToRemove = Math.ceil(extraDmxAddress / 3.0);
options.lights = options.lights.slice(0, -lightsToRemove);
}
return ArtNet.listen(options.host, data => {
if (data.universe === options.universe) {
this._updateLights(data.dmx, options);
}
});
})
.then(() => {
let currentAddress = options.address;
if (options.noLimit) {
console.warn(chalk.yellow('Warning, safety rate limiting is disabled!\n'));
}
console.log(chalk.bold(`DMX addresses on universe ${options.universe}:`));
if (options.transitionChannel) {
console.log(` ${currentAddress++}: transition time`);
}
options.lights.forEach(light => {
console.log(` ${chalk.cyan(`${currentAddress}:`)} ${light.name} ${chalk.grey(`(Hue ID: ${light.id})`)}`);
currentAddress += 3;
});
console.log('\nArtNet node started (CTRL+C to quit)');
});
}
setup(ip, force) {
this._hue.setupBridge(ip, force).then(() => this._setupOptions());
}
_updateLights(dmxData, options) {
if (this._delayedUpdate) {
clearTimeout(this._delayedUpdate);
this._delayedUpdate = null;
}
let address = options.address - 1;
if (options.transitionChannel) {
options.transition = dmxData[address] * 100;
address++;
}
const dmx = dmxData.slice(address, address + (3 * options.lights.length));
let j = 0;
const length = options.lights.length;
let indices = Array.from({length}, (_, i) => i);
indices = this._shuffle(indices);
let i;
for (i of indices) {
const lightId = options.lights[i].id;
j = i * 3;
const color = dmx.slice(j, j + 3);
const previous = options.colors[lightId];
// Update light only if color changed
if (!previous || color[0] !== previous[0] || color[1] !== previous[1] || color[2] !== previous[2]) {
// Rate limit Hue API to 0,1s between calls
const now = new Date().getTime();
if (options.noLimit || now - this._lastUpdate >= 100) {
const state = this._hue.createLightState(color[0], color[1], color[2], options);
this._lastUpdate = now;
this._hue.setLight(lightId, state);
options.colors[lightId] = color;
} else if (!this._delayedUpdate) {
// Make sure to apply update later if changes could not be applied
this._delayedUpdate = setTimeout(() => this._updateLights(dmxData, options), 100);
}
}
}
}
_setupOptions() {
const disabled = Util.config.get('disabledLights') || {};
const transition = Util.config.get('transition') || 0;
this._hue
.getLights()
.then(lights => {
return inquirer
.prompt([
{
type: 'input',
name: 'dmxAddress',
message: 'Set DMX address (range 1-511)',
default: Util.config.get('dmxAddress') || 1,
validate: input => {
const value = parseInt(input, 10);
return value > 0 && value <= 511;
}
},
{
type: 'input',
name: 'universe',
message: 'Set Art-Net universe',
default: Util.config.get('universe') || 0,
validate: input => parseInt(input, 10) >= 0
},
{
type: 'confirm',
name: 'colorloop',
message: 'Enable colorloop feature',
default: Util.config.get('colorloop') || false
},
{
type: 'confirm',
name: 'transitionChannel',
message: 'Use DMX channel for transition time',
default: transition === 'channel'
},
{
type: 'input',
name: 'transition',
message: 'Set transition time in ms',
default: transition === 'channel' ? 100 : transition,
when: answers => !answers.transitionChannel,
validate: input => parseInt(input, 10) > 0
},
{
type: 'checkbox',
name: 'lights',
message: 'Choose lights to use',
choices: lights.map(light => ({
name: light.name,
value: light.id,
checked: !disabled[light.id]
}))
}
])
.then(answers => {
Util.config.set('dmxAddress', parseInt(answers.dmxAddress, 10));
Util.config.set('universe', parseInt(answers.universe, 10));
Util.config.set('colorloop', answers.colorloop);
Util.config.set('transition', answers.transitionChannel ? 'channel' : parseInt(answers.transition, 10));
Util.config.set('disabledLights', lights
.filter(light => !answers.lights.includes(light.id))
.reduce((l, light) => {
l[light.id] = true;
return l;
}, {}));
console.log(`Configuration saved at ${chalk.green(Util.config.path)}`);
});
});
}
_shuffle(array) {
let currentIndex = array.length;
let temporaryValue;
let randomIndex;
// While there remain elements to shuffle...
while (currentIndex !== 0) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
run() {
if (this._args.help) {
Util.exit(help, 0);
} else if (this._args.version) {
Util.exit(pkg.version, 0);
} else if (this._args._[0] === 'setup') {
return this._args.list ? this._hue.listBridges() : this.setup(this._args.ip, this._args.force);
}
if (this._args.transition !== 'channel') {
const value = parseInt(this._args.transition, 10);
this._args.transition = value >= 0 ? value : 0;
}
return this.start({
host: this._args.host,
address: this._args.address || Util.config.get('dmxAddress') || 1,
colorloop: this._args.colorloop || Util.config.get('colorloop') || false,
transition: this._args.transition || Util.config.get('transition') || 100,
noLimit: this._args['no-limit'] || Util.config.get('noLimit') || false,
universe: this._args.universe || Util.config.get('universe') || 0,
disabled: Util.config.get('disabledLights') || {},
order: Util.config.get('lightsOrder') || []
});
}
}
module.exports = DmxHue;