-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.js
307 lines (258 loc) · 10.1 KB
/
lib.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
"use strict";
let SerialPort = require("serialport"),
util = require("util"),
events = require('events');
let Readline = require('@serialport/parser-readline');
function Meridian(devicetype) {
if (devicetype == "TN51") {
// Meridian Technical Note TN51.2
} else if (devicetype == "TN49") {
// Meridian Technical Note TN49
} else if (devicetype == "218") {
// Meridian 218 Zone Controller
} else if (devicetype == "DS 6ii03") {
// Meridian DSP5000 and DSP420
} else {
throw new Error("unsupported device type " + devicetype + " -- Unfortunately, there are many protocols for Meridian control.");
}
this.devicetype = devicetype;
this.seq = 0;
}
util.inherits(Meridian, events.EventEmitter);
let _processw = function() {
if (!this._port) return;
if (this._woutstanding) return;
if (this._qw.length == 0) return;
this._woutstanding = true;
console.log("[Meridian] writing:", this._qw[0]);
this._port.write(this._qw[0] + "\r",
(err) => {
if (err) return;
this._qw.shift();
this._woutstanding = false;
setTimeout(() => { _processw.call(this); }, 150);
});
}
function send(val, cb) {
this._qw.push(val);
_processw.call(this);
};
Meridian.prototype.volume_up = function() {
if (this.devicetype == "TN51" ||
this.devicetype == "TN49" ||
this.devicetype == "DS 6ii03")
{
send.call(this, "VP\r");
} else if (this.devicetype == "218") {
send.call(this, "#MSR VP\r");
} else {
throw new Error("device type " + this.devicetype + " do not support volume_up");
}
};
Meridian.prototype.volume_down = function(val) {
if (this.devicetype == "TN51" ||
this.devicetype == "TN49" ||
this.devicetype == "DS 6ii03")
{
send.call(this, "VM\r");
} else {
throw new Error("device type " + this.devicetype + " do not support volume_down");
}
};
Meridian.prototype.set_volume = function(val) {
if (this.devicetype == "TN51" ||
this.devicetype == "TN49" ||
this.devicetype == "DS 6ii03")
{
if (this.properties.volume == val) return;
if (this.volumetimer) clearTimeout(this.volumetimer);
this.volumetimer = setTimeout(() => {
send.call(this, "VN" + ("00" + Number(val).toString()).slice(-2) + "\r");
}, 50)
} else {
throw new Error("device type " + this.devicetype + " do not support set_volume");
}
};
Meridian.prototype.standby = function(val) {
if (this.devicetype == "TN51" ||
this.devicetype == "TN49" ||
this.devicetype == "DS 6ii03")
{
send.call(this, "SB\r");
} else {
throw new Error("device type " + this.devicetype + " do not support standby");
}
};
Meridian.prototype.set_source = function(val) {
if (this.devicetype == "TN51" ||
this.devicetype == "TN49" ||
this.devicetype == "DS 6ii03")
{
send.call(this, val.slice(0,2) + "\r");
} else {
throw new Error("device type " + this.devicetype + " do not support set_source");
}
};
Meridian.prototype.mute = function() {
if (this.devicetype == "TN51" ||
this.devicetype == "TN49" ||
this.devicetype == "DS 6ii03")
{
send.call(this, "MU\r");
} else {
throw new Error("device type " + this.devicetype + " do not support mute"); }
};
Meridian.prototype.init = function(opts, closecb) {
let self = this;
this._qw = [];
this._woutstanding = false;
this.properties = { volume: opts.volume || 1, source: opts.source || 'SB' };
this.initializing = true;
if (this.devicetype == "TN51") {
this._port = new SerialPort(opts.port, {
baudRate: opts.baud || 9600
});
let parser = this._port.pipe(new Readline({ delimiter: '\r' }));
parser.on('data', data => {
if (this.initializing) {
this.initializing = false;
this.emit('connected');
}
console.log('[Meridian] received:', data);
if (data) data = data.trim();
let m;
if ((m = data.match(/^(V\.|VN|Volume) *([0-9]+)$/))) {
let val = Number(m[2]);
if (this.properties.volume != val) {
this.properties.volume = val;
this.emit('volume', val);
}
} else if (/^Standby$/.test(data) || /^\.$/.test(data)) {
let src = "Standby";
if (this.properties.source != src) { this.properties.source = src; this.emit('source', src); }
} else if (/^Mute/.test(data)) { // Mute or Muted
let src = "Muted";
if (this.properties.source != src) { this.properties.source = src; this.emit('source', src); }
} else if ((m = data.match(/^([A-Za-z]+) *([0-9]+)$/))) {
let src = m[1];
let vol = Number(m[2]);
if (this.properties.volume != vol) { this.properties.volume = vol; this.emit('volume', vol); }
if (this.properties.source != src) { this.properties.source = src; this.emit('source', src); }
} else if ((m = data.match(/^.* ([0-9]+)$/))) {
let vol = Number(m[1]);
if (this.properties.volume != vol) { this.properties.volume = vol; this.emit('volume', vol); }
} else {
let src = data;
if (this.properties.source != src) { this.properties.source = src; this.emit('source', src); }
}
});
} else if (this.devicetype == "TN49") {
this._port = new SerialPort(opts.port, {
baudRate: opts.baud || 9600
});
let parser = this._port.pipe(new Readline({ delimiter: '\r' }));
parser.on('data', data => {
if (this.initializing) {
this.initializing = false;
this.emit('connected');
}
console.log('[Meridian] received:', data);
if (data.length != 20) return;
if (/Diag\./.test(data)) return;
if (/^FIFO /.test(data)) return;
if (/^DSP /.test(data)) return;
if (/^Error /.test(data)) return;
if (/^ *$/.test(data)) return;
if (/^ *\. *$/.test(data)) {
let val = "Standby";
if (this.properties.source != val) { this.properties.source = val; this.emit('source', val); }
return;
}
if (/^Mute *$/.test(data) || /^Muted *$/.test(data)) {
let val = "Muted";
if (this.properties.source != val) { this.properties.source = val; this.emit('source', val); }
return;
}
if (data[5] != ' ') return;
let source = data.substring(0,5).trim();
if (this.properties.source != source) { this.properties.source = source; this.emit('source', source); }
if (/ [ 0-9][0-9]$/.test(data)) {
let vol = Number(data.substring(18));
if (!Number.isNaN(vol))
if (this.properties.volume != vol) { this.properties.volume = vol; this.emit('volume', vol); }
}
});
} else if (this.devicetype == "DS 6ii03") {
this._port = new SerialPort(opts.port, {
baudRate: opts.baud || 9600
});
let parser = this._port.pipe(new Readline({ delimiter: '\r' }));
parser.on('data', data => {
if (this.initializing) {
this.initializing = false;
this.emit('connected');
}
if (data) data = data.trim();
console.log('[Meridian] received:', data);
if (/^..... +([0-9][0-9]?) *$/.test(data)) {
let vol = Number(data.replace(/^..... +([0-9][0-9]?) *$/, "$1"));
let src = data.replace(/^(.....) +[0-9][0-9]? *$/, "$1").trim();
if (this.properties.volume != vol) { this.properties.volume = vol; this.emit('volume', vol); }
if (this.properties.source != src) { this.properties.source = src; this.emit('source', src); }
} else if (/^Standby$/i.test(data) || /^\.$/.test(data)) {
let src = "Standby";
if (this.properties.source != src) { this.properties.source = src; this.emit('source', src); }
} else if (/^Mute/i.test(data)) { // Mute or Muted
let src = "Muted";
if (this.properties.source != src) { this.properties.source = src; this.emit('source', src); }
}
});
} else if (this.devicetype == "218") {
// "#MSR SB\n"
// "#MSR VP\n"
} else {
throw new Error("unsupported device type " + devicetype + " -- Someone forgot to write some code here.");
}
let timer = setTimeout(() => {
if (this.initializing) {
this.initializing = false;
this.emit('connected');
}
}, 3000);
this._port.on('open', err => {
this.emit('preconnected');
if (this.devicetype == "TN51" || this.devicetype == "TN49") {
send.call(this, this.properties.source + "\r");
send.call(this, "VN" + ("00" + Number(this.properties.volume).toString()).slice(-2) + "\r");
}
});
this._port.on('close', () => { this._port.close(() => { this._port = undefined; if (closecb) { var cb2 = closecb; closecb = undefined; cb2('close'); } }) });
this._port.on('error', err => { this._port.close(() => { this._port = undefined; if (closecb) { var cb2 = closecb; closecb = undefined; cb2('error'); } }) });
this._port.on('disconnect', () => { this._port.close(() => { this._port = undefined; if (closecb) { var cb2 = closecb; closecb = undefined; cb2('disconnect'); } }) });
};
Meridian.prototype.start = function(opts) {
this.seq++;
let closecb = (why) => {
this.emit('disconnected');
if (why != 'close') {
var seq = ++this.seq;
setTimeout(() => {
if (seq != this.seq) return;
this.start(opts);
}, 1000);
}
};
if (this._port) {
this._port.close(() => {
this.init(opts, closecb);
});
} else {
this.init(opts, closecb);
}
};
Meridian.prototype.stop = function() {
this.seq++;
if (this._port)
this._port.close(() => {});
};
exports = module.exports = Meridian;