-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
executable file
·151 lines (131 loc) · 4.42 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
#!/usr/bin/env node
const log = require('yalm');
const Mqtt = require('mqtt');
const Artnet = require('artnet');
const config = require('./config.js');
const pkg = require('./package.json');
const artnet = new Artnet({
host: config.address,
port: config.port
});
log.setLevel(config.verbosity);
log.info(pkg.name, pkg.version, 'starting');
const scenes = require(config.scenes);
log.info('scenes loaded from', config.scenes);
const sequences = require(config.sequences);
log.info('sequences loaded from', config.sequences);
artnet.data[0] = [];
// eslint-disable-next-line import/order
const sequencer = require('scene-sequencer')({
setter: artnet.set,
data: artnet.data[0],
scenes,
sequences
});
sequencer.on('transition-conflict', ch => {
log.debug('transition conflict channel', ch);
});
let mqttConnected;
log.info('mqtt trying to connect', config.url);
const mqtt = Mqtt.connect(config.url, {will: {topic: config.name + '/connected', payload: '0'}});
mqtt.on('connect', () => {
mqttConnected = true;
log.info('mqtt connected ' + config.url);
mqtt.publish(config.name + '/connected', '2');
log.info('mqtt subscribe', config.name + '/set/#');
mqtt.subscribe(config.name + '/set/#');
});
mqtt.on('close', () => {
if (mqttConnected) {
mqttConnected = false;
log.info('mqtt closed ' + config.url);
}
});
mqtt.on('error', () => {
log.error('mqtt error ' + config.url);
});
process.on('SIGINT', () => {
log.info('got SIGINT. exiting.');
process.exit(0);
});
process.on('SIGTERM', () => {
log.info('got SIGTERM. exiting.');
process.exit(0);
});
const runningSequences = {};
mqtt.on('message', (topic, payload) => {
payload = payload.toString();
log.debug('mqtt <', topic, payload);
const tpArr = topic.split('/');
let channel;
let scene;
let sequence;
let transition;
switch (tpArr[2]) {
case 'channel':
channel = parseInt(tpArr[3], 10);
if (channel < 1 || channel > 512) {
log.error('invalid channel', tpArr[3]);
} else {
artnet.set(channel, parseInt(payload, 10));
}
break;
case 'scene':
scene = tpArr[3];
if (scenes[scene]) {
log.debug('setScene', scene);
transition = parseFloat(payload) || 0;
sequencer.setScene([scene, transition]);
} else {
log.error('unknown scene', scene);
}
break;
case 'sequence':
sequence = tpArr[3];
if (tpArr[4] === 'stop' && runningSequences[sequence]) {
runningSequences[sequence].stop();
} else if (tpArr[4] === 'stop' && sequence === 'all') {
Object.keys(runningSequences).forEach(s => {
runningSequences[s].stop();
});
} else if (sequences[sequence]) {
log.debug('newSequence', sequence);
newSequence(sequence, payload);
} else {
log.error('unknown sequence', sequence);
}
break;
default:
log.error('unknown cmd', tpArr[2]);
}
function newSequence(sequence, payload) {
let repeat = false;
let shuffle = false;
let speed = 1;
if (payload.indexOf('{') !== -1) {
try {
const tmp = JSON.parse(payload);
repeat = tmp.repeat;
shuffle = tmp.shuffle;
speed = tmp.speed;
} catch (err) {
log.error(err);
}
}
if (runningSequences[sequence]) {
runningSequences[sequence].speed(speed);
runningSequences[sequence].shuffle(shuffle);
runningSequences[sequence].repeat(repeat);
return;
}
log.debug('newSequence', repeat, shuffle, speed);
log.debug('mqtt >', config.name + '/status/sequence/' + sequence, '1');
mqtt.publish(config.name + '/status/sequence/' + sequence, '1');
runningSequences[sequence] = sequencer.newSequence(sequence, repeat, shuffle, speed, () => {
log.debug('sequence end', sequence);
log.debug('mqtt >', config.name + '/status/sequence/' + sequence, '0');
mqtt.publish(config.name + '/status/sequence/' + sequence, '0');
delete runningSequences[sequence];
});
}
});