-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·373 lines (313 loc) · 11.3 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/env node
//
// HTD Lync to MQTT bridge
//
// Author: Andy Swing
//
var pkg = require('./package.json');
var config = require('./config.js');
var HTDLync = require('./htd.js');
var log = require('yalm');
var net = require('net');
var PassThrough = require('stream').PassThrough;
var Mqtt = require('mqtt');
log.setLevel(config.v);
log.info(pkg.name + ' ' + pkg.version + ' starting');
var serialConnected;
var mqttConnected;
// Individual parsed commands are dropped into this
var cmdStream = new PassThrough;
// Used by the raw incoming data parse logic to join data across TCP/Serial packets
var prevRawData = new Buffer(0);
var rawDataStream = new net.Socket();
var htd = new HTDLync(rawDataStream, log);
//
// Handling of incoming raw data
//
log.info('TCP serial bridge trying to connect on ' + config.tcpserialHost + ':' + config.tcpserialPort);
rawDataStream.connect({
host: config.tcpserialHost,
port: config.tcpserialPort
});
rawDataStream.on('connect', function () {
log.info('TCP serial bridge connected');
serialConnected = true;
mqtt.publish(config.topic + '/connected', '2');
// Ensure HTD device is in echo mode so we recieve responses
htd.setEchoMode('on');
});
rawDataStream.on('error', function (err) {
log.error('TCP serial bridge connection error: ' + err);
serialConnected = false;
mqtt.publish(config.topic + '/connected', '1');
});
rawDataStream.on('close', function () {
log.info('TCP serial bridge connection closed');
serialConnected = false;
mqtt.publish(config.topic + '/connected', '1');
});
// Handle incoming raw data from device, parse out commands, send them for processing
rawDataStream.on('data', function (newRawData) {
log.debug('Recieved', newRawData.length, 'bytes of raw incoming data');
log.debug('Raw data (hex):', newRawData.toString('hex'));
var cmd = 0x00;
var len = 0;
var offset = 0;
var cmdPacket;
var remainingBytes;
// Pick up where we left off with any data left over from last time
log.debug('Prepended previous raw data (hex):', prevRawData.toString('hex'));
var data = new Buffer(prevRawData.length + newRawData.length);
prevRawData.copy(data);
newRawData.copy(data, prevRawData.length);
prevRawData = new Buffer(0);
// Loop through each packet
while (offset < data.length) {
// Make sure we are at the start of a packet, if not go to next byte
if (data[offset] != 0x02) {
offset++;
continue;
}
// How many bytes are left in the data buffer?
remainingBytes = data.length - offset;
// Do we not have enough to read the command byte? Then save the rest for next time.
if ( remainingBytes < 4 ) {
prevRawData = new Buffer(data.slice(offset));
break;
}
// Determine command packet length based on type
cmd = data[offset+3];
switch (cmd) {
case 0x1B: // Echo error status
len = 4+1+8+1;
break;
case 0x05: // Zone internal status
case 0x06: // Audio and Keypad Exist channel
len = 4+9+1;
break;
case 0x09: // MP3 Play End
case 0x13: // MP3 ON
len = 4+1+1;
break;
case 0x14: // MP3 OFF
// Additional 31 bytes is due to a bug in the HTD firmware
len = 4+16+1+31;
break;
case 0x0C: // Zone Source Name
case 0x0D: // Zone Name
case 0x0E: // Generic Name
len = 4+13+1;
break;
// Un-handled, so bail on whole packet
// TODO: Figure out how to deal with variable packet length for these cmds
case 0x11: // MP3 File Name
case 0x12: // MP3 Artist Name
default:
len = 1;
log.warn('Unknown response recieved');
log.debug(data.slice(offset));
break;
}
// Not enough data to form a complete packet? Save for next time.
if ( remainingBytes < len ) {
prevRawData = new Buffer(data.slice(offset));
break;
}
// Isolate this packet and drop in the queue
if (len > 4) {
cmdPacket = new Buffer(data.slice(offset, offset+len));
cmdStream.write(cmdPacket);
}
// Continue to next packet
offset = offset + len;
}
return;
});
//
// Handling of parsed incoming responses
//
// Fires each time a new command is dropped into the stream
cmdStream.on('readable', function () {
var data = cmdStream.read();
log.debug('Response received (hex data):', data.toString('hex'));
// Check validity
if ( !htd.validChecksum(data) ){
log.warn('Response recieved with invalid checksum');
return;
}
var zone = data[2];
var cmd = data[3];
var cmdName = '';
var str = '';
var strTerm = 0;
var num = 0;
var topic = config.topic + '/status/' + zone + '/';
switch (cmd) {
case 0x1B: // Echo error status
cmdName = 'Error';
num = data.readUInt8(4);
pubMQTT(topic+'error', num);
break;
case 0x05: // Zone internal status
cmdName = 'Zone Status';
// Data1
pubMQTT(topic+'power', (data[4] & 0x01<<0) ? 'ON' : 'OFF');
pubMQTT(topic+'mute', (data[4] & 0x01<<1) ? 'ON' : 'OFF');
pubMQTT(topic+'dnd', (data[4] & 0x01<<2) ? 'ON' : 'OFF');
// Data2
//pubMQTT(topic+'party', (data[5] & 0x01<<5) ? 'ON' : 'OFF');
//pubMQTT(topic+'alloff', (data[5] & 0x01<<6) ? 'ON' : 'OFF');
//pubMQTT(topic+'allon', (data[5] & 0x01<<7) ? 'ON' : 'OFF');
// Data5
num = data.readUInt8(8) + 1; // Off by one error in protocol
pubMQTT(topic+'source', data.readUInt8(8).toString());
// Data6
num = (data[9] == 0x00) ? 60 : (data.readUInt8(9) - 0xC4);
pubMQTT(topic+'volume', num.toString());
num = Math.round((num/60)*100);
pubMQTT(topic+'volumepercent', num.toString());
break;
case 0x06: // Audio and Keypad Exist channel
cmdName = 'Exist Status';
var zoneBits = data[5] | data[7]<<8;
var keypadBits = data[6] | data[8]<<8;
for (var zz=0; zz<12; zz++){
str = (zoneBits & 0x01<<zz) ? '1' : '0';
pubMQTT(config.topic + '/status/' + (zz+1) + '/exists', str);
str = (keypadBits & 0x01<<zz) ? '1' : '0';
pubMQTT(config.topic + '/status/' + (zz+1) + '/keypadpresent', str);
}
break;
case 0x09: // MP3 Play End
cmdName = 'MP3 Play End Stop';
pubMQTT(topic+'mp3', 'end');
break;
case 0x13: // MP3 ON
cmdName = 'MP3 On';
pubMQTT(topic+'mp3', 'on');
break;
case 0x14: // MP3 OFF
cmdName = 'MP3 Off';
// Additional 31 bytes is due to a bug in the HTD firmware
//str = data.toString('utf8', 4, 19+31);
pubMQTT(topic+'mp3', 'off');
break;
case 0x0C: // Zone Source Name
case 0x0E: // Zone Source Name (undocumented)
cmdName = 'Source Name';
// String from device is null-terminated
str = data.toString('utf8', 4, 14).split("\0").shift();
num = data.readUInt8(15)+1; // Note off-by-one error in protcol
pubMQTT(topic+'sourcename/'+num, str);
break;
case 0x0D: // Zone Name
cmdName = 'Zone Name';
// String from device is null-terminated
str = data.toString('utf8', 4, 14).split("\0").shift();
num = data.readUInt8(15);
pubMQTT(topic+'name', str);
break;
}
log.debug('Completed incoming', cmdName, 'response');
});
// Shotcut for publsihing to MQTT and logging it
function pubMQTT(topic, payload){
log.debug('mqtt >', topic, payload);
mqtt.publish(topic, payload);
}
//
// Handling of incoming MQTT messages
//
log.info('mqtt trying to connect to', config.url);
var mqtt = Mqtt.connect(config.url, {will: {topic: config.topic + '/connected', payload: '0'}});
mqtt.on('connect', function () {
mqttConnected = true;
log.info('mqtt connected ' + config.url);
mqtt.publish(config.topic + '/connected', serialConnected ? '2' : '1');
log.info('mqtt subscribe', config.topic + '/set/+/+');
mqtt.subscribe(config.topic + '/set/+/+');
});
mqtt.on('close', function () {
if (mqttConnected) {
mqttConnected = false;
log.info('mqtt closed ' + config.url);
}
});
mqtt.on('error', function (error) {
log.error('mqtt error ' + error);
});
mqtt.on('message', function (topic, payload) {
payload = payload.toString();
log.debug('mqtt <', topic, payload);
if (!serialConnected) {
log.error('TCP serial bridge disconnected. Can\'t send command.');
return;
}
var parts = topic.split('/');
var zone = parts[parts.length-2];
var cmd = parts[parts.length-1].toLowerCase();
log.debug('htd > zone:', zone, 'command:', cmd, 'value:', payload);
switch (cmd) {
case 'power':
htd.setPower(zone, payload);
break;
case 'volume':
htd.setVolume(zone, payload);
break;
case 'volumepercent':
htd.setVolumeByPercent(zone, payload);
break;
case 'source':
htd.setSource(zone, payload);
break;
case 'mute':
htd.setMute(zone, payload);
break;
case 'dnd':
htd.setDND(zone, payload);
break;
case 'update':
switch (payload) {
case 'status':
htd.queryZoneStatus();
break;
case 'fullstatus':
htd.queryFullStatus();
break;
case 'zonenames':
htd.queryAllZoneNames();
break;
case 'sourcenames':
htd.queryAllSourceNames();
break;
case 'all':
htd.queryZoneStatus();
htd.queryAllZoneNames();
//htd.queryAllSourceNames();
break;
}
break;
}
});
//
// Grab initial data
//
// After program is stable (4 seconds), request info
setTimeout(function() {
if (!serialConnected || !mqttConnected) {
log.error('Could not query initial info because connections not ready');
return;
}
/* Final response packet is buggy in the HTD firmware, so we'll do it piece by piece
log.info('Querying initial Full Status');
htd.queryFullStatus();
*/
log.info('Querying initial Zone Status');
htd.queryZoneStatus();
log.info('Querying initial Zone Names');
htd.queryAllZoneNames();
/* Currently buggy - some commands/responses get dropped
log.info('Querying initial Source Names');
htd.queryAllSourceNames();
*/
}, 4000);