-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
388 lines (344 loc) · 10.9 KB
/
index.js
File metadata and controls
388 lines (344 loc) · 10.9 KB
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
/**
* phantom-remote-control
* HTTP Adapter to remote control Devialet Phantom sound system
* (Play, Pause, Volume etc.)
*
* @french Contrôlez les enceintes Devialet via une API HTTP. (Lecture, Pause, Volume etc.)
* @package phantom-remote-control
* @author https://github.com/VanVan
*/
const config = require('./config.json');
if (config.debug) {
const dotenv = require('dotenv');
dotenv.config();
}
var ssdp = require('node-ssdp').Client
var parseUri = require('parseuri');
var client = new ssdp();
var http = require('http');
var url = require('url');
var request = require('request');
var MediaRendererClient = require('upnp-mediarenderer-client');
var clientMedia=null;
var devialetDevice = {};
list_local_address();
create_http_server();
client.on('response', function inResponse(headers, code, rinfo) {
console.log('Detected device ip: '+rinfo.address);
if (typeof headers.LOCATION != 'undefined' && headers.LOCATION) { // Download file
var http_options = {
hostname: parseUri(headers.LOCATION).host,
port: parseUri(headers.LOCATION).port,
path: parseUri(headers.LOCATION).path,
method: 'GET'
}
var req = http.request(http_options, (res) => {
res.setEncoding('utf8');
res.on('data', (data) => {
if (data.includes('Devialet UPnP Renderer') || data.includes('Phantom')) {
clientMedia = new MediaRendererClient(headers.LOCATION);
devialetDevice.host = parseUri(headers.LOCATION).host;
devialetDevice.port = parseUri(headers.LOCATION).port;
console.log("Devialet found : "+devialetDevice.host+":"+devialetDevice.port);
}
});
});
req.on('error', (e) => {
console.log(`get Location xml problem with request: ${e.message}`);
});
req.end();
}
if(rinfo.address == config.devialet_ipaddress) {
devialetDevice.host = parseUri(headers.LOCATION).host;
devialetDevice.port = parseUri(headers.LOCATION).port;
}
});
/**
* Set volume of Devialet speaker
* @param {int} volumeLevel
* @returns {Promise} Returns new volume level
*/
function setVolume(volumeLevel)
{
return new Promise(function(resolve, reject) {
var xml = '<s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">'+
'<s:Body>'+
'<u:SetVolume xmlns:u="urn:schemas-upnp-org:service:RenderingControl:2">'+
'<InstanceID>0</InstanceID>'+
'<Channel>Master</Channel>'+
'<DesiredVolume>'+volumeLevel+'</DesiredVolume>'+
'</u:SetVolume>'+
'</s:Body>'+
'</s:Envelope>';
var http_options = {
method: 'POST',
path: '/Control/LibRygelRenderer/RygelRenderingControl',
hostname: devialetDevice.host,
port: devialetDevice.port,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'SOAPACTION': 'urn:schemas-upnp-org:service:RenderingControl:2#SetVolume',
'Content-Length': xml.length
}
}
var req = http.request(http_options, (res) => {
res.setEncoding('utf8');
res.on('data', (data) => {
if (data.includes('SetVolumeResponse')) {
console.log(' -> Success');
resolve(volumeLevel);
}
else {
console.log(' -> Error: volume unchanged');
reject(volumeLevel);
}
});
});
req.on('error', (e) => {
console.log(` -> Error, volume cannot be set: ${e.message}`);
reject(volumeLevel);
});
req.write(xml);
req.end();
process.stdout.write("Trying to set Volume to "+volumeLevel+"%");
});
}
/**
* Get Volume of Devialet Speaker
* @returns {Promise} Returns current volume level
*/
function getVolume()
{
return new Promise(function(resolve, reject) {
var http_options = {
method: 'POST',
path: '/Control/LibRygelRenderer/RygelRenderingControl',
hostname: devialetDevice.host,
port: devialetDevice.port,
headers: {
'Content-Type': 'text/xml',
'SOAPACTION': 'urn:schemas-upnp-org:service:RenderingControl:2#GetVolume',
'Content-Length': 350
}
}
var req = http.request(http_options, (res) => {
res.setEncoding('utf8');
res.on('data', (data) => {
resolve(data);
console.log(data);
});
});
req.on('error', (e) => {
console.log(`Volume cannot be retrieved: ${e.message}`);
reject(e);
});
req.end();
process.stdout.write("Trying to get Volume");
});
}
/**
* Search Devialet system on network
*/
function searchDevialetSpeaker()
{
if (devialetDevice.host) {
console.log('Updating device ...');
}
else
console.log('Trying to detect, please wait ...');
client.search('urn:schemas-upnp-org:service:RenderingControl:2');
setTimeout(function() {
searchDevialetSpeaker();
}, config.ssdp_detection_interval);
}
/**
* List local ip address
*/
function list_local_address() {
console.log();
var os = require('os');
var ifaces = os.networkInterfaces();
console.log('You can open your browser on:');
Object.keys(ifaces).forEach(function (ifname) {
ifaces[ifname].forEach(function (iface) {
if ('IPv4' !== iface.family || iface.internal !== false)
return;
console.log('http://'+iface.address+':'+config.http_local_port);
});
});
console.log();
}
/**
* Create HTTP server to listen request
*/
function create_http_server() {
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
var q = url.parse(req.url, true).query;
if (!devialetDevice.host)
return res.end("<h2>Devialet not found</h2><h5>Please wait ...<h5>"+
'<META http-equiv="refresh" content="10; URL="><style>body{text-align: center}.lds-dual-ring{display:inline-block;width:64px;height:64px;} .lds-dual-ring:after{content:" ";display:block;width:46px;height:46px;margin:1px;border-radius:50%;border:5px solid #fff;border-color:#888 transparent #888 transparent;animation:lds-dual-ring 1.2s linear infinite;} @keyframes lds-dual-ring{0%{transform:rotate(0deg);}100%{transform:rotate(360deg);}}</style><div class="lds-dual-ring"></div>');
if(q.setVolume && parseInt(q.setVolume, 10) >= 0)
{
setVolume(parseInt(q.setVolume)).then(function() {
res.end('<h5>Volume successfully set to '+volumeLevel+'%</h5>');
}, function(volumeLevel) {
res.end('<p style="color:red;">Error, cannot set volume to '+volumeLevel+'%</p>');
});
}
else if(q.setVolume == 'up')
{
if (!clientMedia)
return res.end('Volume Up & Down not available');
clientMedia.getVolume(function(err, volume) {
if (err)
res.end('<p style="color:red;">Error, unable to get current volume:'+err+'</p>');
volume = volume+config.volume_step>100?100:volume+config.volume_step;
setVolume(volume).then(function(volumeLevel) {
res.end('<h5>Volume successfully set to '+volumeLevel+'%</h5>');
}, function(volumeLevel) {
res.end('<p style="color:red;">Error, cannot set volume to '+volumeLevel+'%</p>');
})
});
}
else if(q.setVolume == 'down')
{
if (!clientMedia)
return res.end('Volume Up & Down not available');
clientMedia.getVolume(function(err, volume) {
if (err)
res.end('<p style="color:red;">Error, unable to get current volume:'+err+'</p>');
volume=volume-config.volume_step<0?0:volume-config.volume_step;
setVolume(volume).then(function(volumeLevel) {
res.end('<h5>Volume successfully set to '+volumeLevel+'%</h5>');
}, function(volumeLevel) {
res.end('<p style="color:red;">Error, cannot set volume to '+volumeLevel+'%</p>');
})
});
}
else if (q.getVolume) {
clientMedia.getVolume(function(err, volume) {
if (err)
res.end('<p style="color:red;">Error, unable to get current volume:'+err+'</p>');
console.log('Volume: '+volume);
res.end('<h5>Volume: '+volume+'%</h5>');
});
} else if(q.play) {
if (q.play.length < 5) {
clientMedia.play(function(err, result) {
if (err) {
console.log(err);
res.end('Error: '+err);
}
else {
console.log('Play');
res.end("Play");
}
});
}
else {
var ext = q.play.split('.').pop();
if (ext != 'mp3' && ext != 'wav' && ext != 'ogg')
return res.end('You can only play MP3 / WAV / OGG file ...');
var options = {
autoplay: true,
contentType: 'audio/'+ ext
};
clientMedia.stop(function() {
setTimeout(function() {
clientMedia.load(q.play, options, function(err, result) {
if(err) {
res.end("Error, unable to play file");
console.log("Error, unable to play file");
console.log(err);
}
else {
console.log('playing '+q.play+' ...');
res.end("Playing "+q.play+' ...');
}
});
}, 300);
});
}
} else if (q.stop) {
clientMedia.stop(function(err, result) {
if (err) {
console.log(err);
res.end('Stop: '+err);
}
else {
console.log('Stopped');
res.end("Stopped");
}
});
} else if (q.pause) {
clientMedia.pause(function(err, result) {
if (err) {
console.log(err);
res.end('Pause: '+err);
}
else {
console.log('Paused');
res.end("Paused");
}
});
} else if (q.getPosition) {
clientMedia.getPosition(function(err, result) {
if (err) {
console.log(err);
res.end('getPosition: '+err);
}
else {
console.log('Position '+result);
res.end("Position "+result);
}
});
} else if (q.getDuration) {
clientMedia.getDuration(function(err, result) {
if (err) {
console.log(err);
res.end('getDuration: '+err);
}
else {
console.log('Duration '+result);
res.end("Duration "+result);
}
});
} else if (q.seek) {
clientMedia.seek(q.seek, function(err, result) {
if (err) {
console.log(err);
res.end('Seek: '+err);
}
else {
console.log('Seeked to '+q.seek);
res.end("Seeked to "+q.seek);
}
});
}
else
{
res.end('<meta charset="UTF-8"><h2>phantom-remote-control ✔️</h2> <h4>Please specify a command</h4>'+
'<br /><br /><h5>Example</h5><a href="?play='+encodeURI('https://raw.githubusercontent.com/VanVan/phantom-remote-control/binary/sound/test.mp3')+'">Play 15s sound</a><br />'+
'<a href="?setVolume=up">Increase the volume</a><br />'+
'<a href="?setVolume=down">Reduce the volume</a><br />'+
'<a href="?stop=1">Stop sound</a>');
}
}).listen(config.http_local_port);
}
console.log("Searching for Devialet Phantom or Dialog ...");
console.log("This may take several minutes");
//On force
if (config.devialet_port && config.devialet_ipaddress && config.devialet_port != 'autodetect' && config.devialet_ipaddress != 'autodetect') {
devialetDevice.host = config.devialet_ipaddress;
devialetDevice.port = config.devialet_port;
console.log('Detection forced from config file to be '+config.devialet_ipaddress+':'+config.devialet_port+' -> Only set volume usable');
}
else if (config.devialet_device_description_xml && config.devialet_device_description_xml != 'autodetect') {
clientMedia = new MediaRendererClient(config.devialet_device_description_xml);
devialetDevice.host = parseUri(config.devialet_device_description_xml).host;
devialetDevice.port = parseUri(config.devialet_device_description_xml).port;
console.log('Detection forced from config device description to be '+devialetDevice.host+':'+devialetDevice.port);
}
else
searchDevialetSpeaker();