-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarantz-client.js
254 lines (237 loc) · 7.09 KB
/
marantz-client.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
var telnet = require('telnet-client');
var connection = new telnet();
var events = require('events');
var myEmitter = new events.EventEmitter();
var state = {
main: {
isPowered: false,
source: null,
mainLevel: null,
isMuted: false
},
aux: {
isPowered: false,
source: null,
mainLevel: null,
isMuted: false
}
};
connection.on('close', function() {
console.log('connection closed');
process.exit();
});
connection.on('error', function(error) {
console.log('connection error ' + error);
});
connection.on('data', function(data) {
data = data.toString().trim();
switch (data) {
case 'SISAT/CBL':
myEmitter.emit('source', 'AppleTV', 'main');
state.main.source = 'AppleTV';
console.log('Main Source: AppleTV');
return;
case 'SIDVD':
myEmitter.emit('source', 'Mac Mini', 'main');
state.main.source = 'Mac Mini';
console.log('Main Source: Mac Mini');
return;
case 'SIBD':
myEmitter.emit('source', 'KVM Switch', 'main');
state.main.source = 'KVM Switch';
console.log('Main Source: KVM Switch');
return;
case 'Z2SAT/CBL':
myEmitter.emit('source', 'AppleTV', 'aux');
state.aux.source = 'AppleTV';
console.log('Aux Source: AppleTV');
return;
case 'Z2DVD':
myEmitter.emit('source', 'Mac Mini', 'aux');
state.aux.source = 'Mac Mini';
console.log('Aux Source: Mac Mini');
return;
case 'Z2BD':
myEmitter.emit('source', 'KVM Switch', 'aux');
state.aux.source = 'KVM Switch';
console.log('Aux Source: KVM Switch');
return;
case 'Z2OFF':
myEmitter.emit('isPowered', false, 'aux');
state.aux.isPowered = false;
console.log('Aux Power: Off');
return;
case 'Z2ON':
myEmitter.emit('isPowered', true, 'aux');
state.aux.isPowered = true;
console.log('Aux Power: On');
return;
case 'ZMOFF':
myEmitter.emit('isPowered', false, 'main');
state.main.isPowered = false;
console.log('Main Power: Off');
return;
case 'ZMON':
myEmitter.emit('isPowered', true, 'main');
state.main.isPowered = true;
console.log('Main Power: On');
return;
case 'MUOFF':
myEmitter.emit('isMuted', false, 'main');
state.main.isMuted = false;
console.log('Main Mute: Off');
return;
case 'MUON':
myEmitter.emit('isMuted', true, 'main');
state.main.isMuted = true;
console.log('Main Mute: On');
return;
case 'Z2MUOFF':
myEmitter.emit('isMuted', false, 'aux');
state.aux.isMuted = false;
console.log('Aux Mute: Off');
return;
case 'Z2MUON':
myEmitter.emit('isMuted', true, 'aux');
state.aux.isMuted = true;
console.log('Aux Mute: On');
return;
}
if (data.indexOf('MV') == 0) {
var main_vol = getVolume(data);
if (main_vol !== false) {
myEmitter.emit('volume', main_vol, 'main');
state.main.mainLevel = main_vol;
console.log('Main Volume: ' + main_vol);
return;
}
}
if (data.indexOf('Z2') == 0) {
var aux_vol = getVolume(data);
if (aux_vol !== false) {
myEmitter.emit('volume', aux_vol, 'aux');
state.aux.mainLevel = aux_vol;
console.log('Aux Volume: ' + aux_vol);
return;
}
}
console.log('\t\t\t\tUnknown data: ' + data);
});
function getVolume(data) {
var vol = data.substr(2);
if (/^[0-9]{3}$/.test(vol)) {
vol = vol.substring(0, 2);
vol = parseFloat(vol);
vol += 0.5;
return vol;
} else if (/^[0-9]{2}$/.test(vol)) {
return parseFloat(vol);
}
return false;
}
var marantzHost = process.env.MARANTZ_HOST || 'marantz';
//TODO: add automatic reconnect logic
connection.connect({host: marantzHost, shellPrompt: ''});
setTimeout(function() { connection.send('SI?\r'); }, 1000);
setTimeout(function() { connection.send('ZM?\r'); }, 2000);
setTimeout(function() { connection.send('MV?\r'); }, 3000);
setTimeout(function() { connection.send('MU?\r'); }, 4000);
setTimeout(function() { connection.send('Z2?\r'); }, 5000);
setTimeout(function() { connection.send('Z2MU?\r'); }, 6000);
module.exports.getState = function() {
return state;
};
module.exports.setPower = function(device, isOn) {
switch (device) {
case 'main':
if (isOn) {
connection.send('ZMON\r'); //might be PWON
} else {
connection.send('ZMOFF\r'); //might be PWSTANDBY
}
return true;
case 'aux':
if (isOn) {
connection.send('Z2ON\r');
} else {
connection.send('Z2OFF\r');
}
return true;
}
return false;
};
module.exports.setMute = function(device, isOn) {
switch (device) {
case 'main':
if (isOn) {
connection.send('MUON\r');
} else {
connection.send('MUOFF\r');
}
return true;
case 'aux':
if (isOn) {
connection.send('Z2MUON\r');
} else {
connection.send('Z2MUOFF\r');
}
return true;
}
return false;
};
module.exports.volumeUp = function(device) {
switch (device) {
case 'main':
connection.send('MVUP\r');
return true;
case 'aux':
connection.send('Z2UP\r');
return true;
}
return false;
};
module.exports.volumeDown = function(device) {
switch (device) {
case 'main':
connection.send('MVDOWN\r');
return true;
case 'aux':
connection.send('Z2DOWN\r');
return true;
}
return false;
};
module.exports.setVolume = function(device, volume) {
if (volume < 10) {
volume = '0' + volume;
}
switch (device) {
case 'main':
connection.send('MV' + volume + '\r');
return true;
case 'aux':
connection.send('Z2' + volume + '\r');
return true;
}
return false;
};
module.exports.setSource = function(device, source) {
var prefix = (device == 'main' ? 'SI' : 'Z2');
switch (source) {
case 'AppleTV':
connection.send(prefix + 'SAT/CBL\r');
break;
case 'Mac Mini':
connection.send(prefix + 'DVD\r');
break;
case 'KVM Switch':
connection.send(prefix + 'BD\r');
break;
default:
return false;
}
return true;
};
module.exports.on = function (key, callback) {
myEmitter.on(key, callback);
};