UPnP.js
3.13 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
'use strict';
const dgram = require('dgram')
, EventEmitter = require('events').EventEmitter
;
class SSDPSearch {
constructor() {
const self = this;
this.responseEmitter = new EventEmitter();
this.socket = dgram.createSocket('udp4');
this.socket.on('error', function(err) {
self.responseEmitter.emit('error', err);
});
this.socket.on('message', function onMessage(msg, rinfo) {
const msgStrings = msg.toString().split('\r\n');
// HTTP/#.# ### Response
if (msgStrings[0].match(/HTTP\/(\d{1})\.(\d{1}) (\d+) (.*)/)) {
self.responseEmitter.emit('response', _parseSearchResponse(msgStrings.slice(1)));
}
});
// Ensure the socket is released on exit of the process (in case of errors)
process.on('exit', this._finished.bind(this));
}
search(timeout) {
const self = this;
return new Promise((resolve, reject) => {
const results = [];
self.responseEmitter.on('error', err => {
self.finished();
reject(err);
});
self.responseEmitter.on('response', value => {
results.push(value);
});
// Await our timeout before returning any results
setTimeout(() => {
self._finished();
resolve(_processResults(results));
}, timeout || 5000);
self._start();
});
}
_start() {
const ip = '239.255.255.250',
port = 1900;
const pkt = Buffer.from(_buildSearchPacket(
{
'HOST': ip + ':' + port,
'MAN': 'ssdp:discover',
'MX': 10,
// "ST": "SsdpSearch:all"
'ST': 'urn:schemas-upnp-org:device:Basic:1'
}
));
this.socket.send(pkt, 0, pkt.length, port, ip);
}
_finished() {
if (this.socket) {
this.socket.close();
this.socket = null;
}
}
}
module.exports = SSDPSearch;
function _buildSearchPacket(vars) {
let packet = 'M-SEARCH * HTTP/1.1\r\n';
Object.keys(vars).forEach(function(n) {
packet += n + ': ' + vars[n] + '\r\n';
});
return packet + '\r\n';
}
function _parseSearchResponse(lines) {
const result = {};
lines.forEach(line => {
const separatorIndex = line.indexOf(':');
if (separatorIndex > 0 && separatorIndex < line.length) {
const key = line.substring(0, separatorIndex).toLowerCase();
const value = line.substring(separatorIndex + 1, line.length).trim();
result[key] = value;
}
});
return result;
}
function _processResults(responses) {
const results = {};
responses.forEach(response => {
const location = response.location;
// Older versions used to use hue-bridgeId, now newer software versions use hue-bridgeid, remove the older fallback
// once they are outdated.
let hueBridgeId = response['hue-bridgeid'];
if (hueBridgeId == undefined) {
hueBridgeId = response['hue-bridgeId'];
}
if (location && hueBridgeId) {
if (!results[location]) {
const ipAddress = /\/\/(.*):/.exec(location);
if (ipAddress) {
results[location] = {
id: hueBridgeId,
internalipaddress: ipAddress[1]
};
}
}
}
});
return Object.values(results);
}