-
Notifications
You must be signed in to change notification settings - Fork 11
/
pioneer-avr.js
476 lines (420 loc) · 10.9 KB
/
pioneer-avr.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/**
* To control a Pioneer receiver via IP protocol.
* Tested with VSX-2021
*/
var util = require('util'),
net = require('net'),
events = require('events');
var TRACE = false;
var DETAIL = false; // detail logging flag
/**
* Important include host and port.
* e.g.:
* var options = {
* port: 23,
* host: "192.168.0.9",
* log: true
* };
*/
var VSX = function(options) {
events.EventEmitter.call(this); // inherit from EventEmitter
this.client = this.connect(options);
this.inputNames = {};
TRACE = options.log;
};
util.inherits(VSX, events.EventEmitter);
VSX.prototype.connect = function(options) {
var self = this;
var client = net.connect(options);
client.on("connect", function(socket) {
handleConnection(self, socket);
});
client.on("data", function(data) {
handleData(self, data);
});
client.on("end", function() {
handleEnd(self);
});
client.on("error", function(err) {
handleError(self, err);
});
return client;
};
VSX.prototype.query = function() {
var self = this;
self.client.write("?P\r"); // query power state
self.client.write("?V\r"); // query volume state
self.client.write("?ZV\r"); // query volume state
self.client.write("?AP\r"); // query power state
self.client.write("?M\r"); // query mute state
self.client.write("?Z2M\r"); // query mute state
self.client.write("?F\r"); // query selected input
// get input names
var timeout = 100;
for (var i in Inputs) {
var inputId = Inputs[i];
var getInputName = function(inputId, timeout) {
setTimeout(function() {
self.client.write("?RGB" + inputId + "\r");
}, timeout);
}
getInputName(inputId, timeout);
timeout += 100;
}
}
/**
* Turn unit power on or off
*/
VSX.prototype.power = function(on) {
if (TRACE) {
console.log("turning power: " + on);
}
if (on) {
this.client.write("PO\r");
} else {
this.client.write("PF\r");
}
};
/**
* Turn unit power zone2 on or off
*/
VSX.prototype.zpower = function(on) {
if (TRACE) {
console.log("turning power: " + on);
}
if (on) {
this.client.write("APO\r");
} else {
this.client.write("APF\r");
}
};
/**
* Turn mute on or off
*/
VSX.prototype.mute = function(on) {
if (TRACE) {
console.log("turning mute: " + on);
}
if (on) {
this.client.write("MO\r");
} else {
this.client.write("MF\r");
}
};
/**
* Turn mute on or off zone2
*/
VSX.prototype.zmute = function(on) {
if (TRACE) {
console.log("turning mute: " + on);
}
if (on) {
this.client.write("Z2MO\r");
} else {
this.client.write("Z2MF\r");
}
};
/**
*
* @param {Object} db from -80 to +12
*/
VSX.prototype.volume = function(db) {
// [0 .. 185] 1 = -80dB , 161 = 0dB, 185 = +12dB
if (TRACE) {
console.log("setting volume db: " + db);
}
var val = 0;
if (typeof db === "undefined" || db === null) {
val = 0;
} else if (db < -80) {
val = 0;
} else if (db > 12) {
val = 185;
} else {
val = Math.round((db * 2) + 161);
}
var level = val.toString();
while (level.length < 3) {
level = "0" + level;
}
if (TRACE) {
console.log("setting volume level: " + level);
}
this.client.write(level + "VL\r");
};
VSX.prototype.zvolume = function(db) {
// [0 .. 81] 1 = -80dB , 81 = 0dB, 00 = ---dB
if (TRACE) {
console.log("setting zvolume db: " + db);
}
var val = 0;
if (typeof db === "undefined" || db === null) {
val = 0;
} else if (db < -80) {
val = 0;
} else if (db > 0) {
val = 81;
} else {
val = Math.round(db + 81 ) ;
}
var level = val.toString();
while (level.length < 2) {
level = "0" + level;
}
if (TRACE) {
console.log("setting zvolume level: " + level);
}
this.client.write(level + "ZV\r");
};
VSX.prototype.volumeUp = function() {
this.client.write("VU\r");
};
VSX.prototype.volumeDown = function() {
this.client.write("VD\r");
};
VSX.prototype.zvolumeUp = function() {
this.client.write("ZU\r");
};
VSX.prototype.zvolumeDown = function() {
this.client.write("ZD\r");
};
/**
* Set the input
*/
VSX.prototype.selectInput = function(input) {
this.client.write(input + "FN\r");
};
VSX.prototype.selectZInput = function(input) {
this.client.write(input + "ZS\r");
};
/**
* Query the input name
*/
VSX.prototype.queryInputName = function(inputId) {
this.client.write("?RGB" + inputId + "\r");
}
/**
* Set the listening mode
*/
VSX.prototype.listeningMode = function(mode) {
this.client.write("MF\r");
};
/**
* Press the enter button in HMG
*/
VSX.prototype.buttonHMGEnter = function() {
this.client.write("30NW\r");
};
/**
* Press the return button in HMG
*/
VSX.prototype.buttonHMGReturn = function() {
this.client.write("31NW\r");
};
/**
* Press the play button in HMG
*/
VSX.prototype.buttonHMGPlay = function() {
this.client.write("10NW\r");
};
/**
* Press the stop button in HMG
*/
VSX.prototype.buttonHMGStop = function() {
this.client.write("20NW\r");
};
/**
* Press the Up button in HMG
*/
VSX.prototype.buttonHMGUp = function() {
this.client.write("26NW\r");
};
/**
* Press the Down button in HMG
*/
VSX.prototype.buttonHMGDown = function() {
this.client.write("27NW\r");
};
/**
* Press the '1' button in HMG
*/
VSX.prototype.buttonHMG1 = function() {
this.client.write("01NW\r");
};
function handleConnection(self, socket) {
if (TRACE) {
console.log("got connection.");
}
self.client.write("\r"); // wake
setTimeout(function() {
self.query();
self.emit("connect");
}, 100);
self.socket = socket;
}
function handleData(self, d) {
var input;
var data = d.toString(); // make sure it's a string
var length = data.lastIndexOf('\r');
data = data.substr(0, length);
// TODO implement a message to handler mapping instead of this big if-then statement
if (data.startsWith("PWR")) { // power status
var pwr = (data == "PWR0"); // PWR0 = on, PWR1 = off
if (TRACE) {
console.log("got power: " + pwr);
}
self.emit("power", pwr);
}
else if (data.startsWith("APR")) { // power status Zone2
var pwr = (data == "APR0"); // APR0 = on, APR1 = off
if (TRACE) {
console.log("got zpower: " + pwr);
}
self.emit("zpower", pwr);
}
else if (data.startsWith("ZV")) { // volume status zone2
var vol = data.substr(2, 3);
// translate to dB.
var db = (parseInt(vol) - 81) ;
if (TRACE) {
console.log("got zvolume: " + db + "dB (" + vol + ")");
}
self.emit("zvolume", db);
}
else if (data.startsWith("VOL")) { // volume status
var vol = data.substr(3, 3);
// translate to dB.
var db = (parseInt(vol) - 161) / 2;
if (TRACE) {
console.log("got volume: " + db + "dB (" + vol + ")");
}
self.emit("volume", db);
}
else if (data.startsWith("MUT")) { // mute status
var mute = data.endsWith("0"); // MUT0 = muted, MUT1 = not muted
if (TRACE) {
console.log("got mute: " + mute);
}
self.emit("mute", mute);
}
else if (data.startsWith("Z2MUT")) { // mute status
var mute = data.endsWith("0"); // MUT0 = muted, MUT1 = not muted
if (TRACE) {
console.log("got zmute: " + mute);
}
self.emit("zmute", mute);
}
else if (data.startsWith("FN")) {
input = data.substr(2, 2);
if (TRACE) {
console.log("got input: " + input + " : " + self.inputNames[input]);
}
self.emit("input", input, self.inputNames[input]);
}
else if (data.startsWith("Z2F")) {
input = data.substr(3, 2);
if (TRACE) {
console.log("got input: " + input + " : " + self.inputNames[input]);
}
self.emit("zinput", input, self.inputNames[input]);
}
else if (data.startsWith("SSA")) {
if (TRACE && DETAIL) {
console.log("got SSA: " + data);
}
} else if (data.startsWith("APR")) {
if (TRACE && DETAIL) {
console.log("got APR: " + data);
}
} else if (data.startsWith("BPR")) {
if (TRACE && DETAIL) {
console.log("got BPR: " + data);
}
} else if (data.startsWith("LM")) { // listening mode
var mode = data.substring(2);
if (TRACE) {
console.log("got listening mode: " + mode);
}
} else if (data.startsWith("FL")) { // FL display information
if (TRACE && DETAIL) {
console.log("got FL: " + data);
}
} else if (data.startsWith("RGB")) { // input name information. informs on input names
// handle input info
var inputId = data.substr(3, 2);
for (input in Inputs) {
if (Inputs[input] == inputId) {
// if (data.substr(5, 1) == "0") {
// console.log("default input name")
// }
self.inputNames[inputId] = data.substr(6);
if (TRACE && DETAIL) {
console.log("set input " + input + " to " + self.inputNames[inputId]);
}
self.emit("inputName", inputId, self.inputNames[inputId]);
break;
}
}
} else if (data.startsWith("RGC")) {
if (TRACE && DETAIL) {
console.log("got RGC: " + data);
}
} else if (data.startsWith("RGF")) {
if (TRACE && DETAIL) {
console.log("got RGF: " + data);
}
} else if (data.length > 0) {
if (TRACE) {
console.log("got data: " + data);
}
}
}
function handleEnd(self) {
if (TRACE) {
console.log("connection ended");
}
self.emit("end");
}
function handleError(self, err) {
if (TRACE) {
console.log("connection error: " + err.message);
}
self.emit("error", err);
}
if (typeof String.prototype.startsWith != 'function') {
String.prototype.startsWith = function(str) {
return this.slice(0, str.length) == str;
};
}
if (typeof String.prototype.endsWith != 'function') {
String.prototype.endsWith = function(str) {
return this.slice(-str.length) == str;
};
}
var Inputs = {
dvd: "04",
bd: "25",
tv_sat: "05",
kabel: "06",
dvr_bdr: "15",
video_1: "10",
video_2: "14",
hdmi_1: "19",
hdmi_2: "20",
hdmi_3: "21",
hdmi_4: "22",
hdmi_5: "23",
media: "26",
ipod_usb: "17",
xm_radio: "18",
cd: "01",
cdr_tape: "03",
tuner: "02",
phono: "00",
multi_ch: "12",
adapter_port: "33",
sirius: "27",
//hdmi_cyclic: "31",
};
exports.VSX = VSX;
exports.Inputs = Inputs;