-
Notifications
You must be signed in to change notification settings - Fork 0
/
discoveryService.js
170 lines (141 loc) · 5.01 KB
/
discoveryService.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
'use strict';
/**
* UPnP Device discovery API.
*
* @return {Object}
*/
var DiscoveryService = function () {
/**
* Include the "Q" library.
*
* @type {Q}
* @private
*/
var Q = require('q');
/**
* Include the "HTTP" library.
*
* @type {http}
* @private
*/
var http = require('http');
/**
* Perform an SSDP Search for Devices on the network, using the given SSDP Search string.
*
* @param {string} SSDPSearchString The SSDP Search string to use for Device search.
* @param {Object} options Configuration object for SSDP search (including Timeout delay, etc.).
* @return {Deferred.Promise} A promise to be resolved/rejected when results become available.
* @public
*/
var search = function (SSDPSearchString, options) {
options = options || {};
options.SSDPTimeout = options.SSDPTimeout || 2*1000;
var deferred = Q.defer();
var SSDPClient = require('node-ssdp').Client,
client = new SSDPClient(),
responses = [];
client.on('response', function (headers, statusCode, rinfo) {
// Split on line breaks ("/r/n"):
var headersFormatted = headers.split( String.fromCharCode(13, 10) );
// Extract the headers:
var headersParsed = {};
for (var i = 0, nbHeaders = headersFormatted.length; i < nbHeaders; i++) {
var item = headersFormatted[i];
var firstColonIndex = item.indexOf(':');
if (firstColonIndex > -1) {
var key = item.substring(0, firstColonIndex);
var value = item.substring(firstColonIndex + 1, item.length).trim();
headersParsed[key] = value;
}
}
responses.push({
headers: headers,
headersFormatted: headersFormatted,
headersParsed: headersParsed,
statusCode: statusCode,
rinfo: rinfo
});
});
// Search for a service type:
client.search(SSDPSearchString);
// Wait a few seconds while waiting for responses:
setTimeout(function () {
deferred.resolve(responses);
}, options.SSDPTimeout);
return deferred.promise;
};
/**
* Get all SSDP Devices on the network.
*
* @param {Object} options Configuration object for SSDP search (including Timeout delay, etc.).
* @return {Deferred.Promise} A promise to be resolved/rejected when results become available.
* @public
*/
var getAllDevices = function (options) {
return search('ssdp:all', options);
};
/**
* Get all Samsung SmartTV Devices on the network.
*
* @param {Object} options Configuration object for SSDP search (including Timeout delay, etc.).
* @return {Deferred.Promise} A promise to be resolved/rejected when results become available.
* @public
*/
var getAllSamsungSmartTVs = function (options) {
return search('urn:samsung.com:device:RemoteControlReceiver:1', options);
};
/**
* Get all "Rendering Controls" on the network.
*
* @param {Object} options Configuration object for SSDP search (including Timeout delay, etc.).
* @return {Deferred.Promise} A promise to be resolved/rejected when results become available.
* @public
*/
var getAllRenderingControls = function (options) {
return search('urn:schemas-upnp-org:service:RenderingControl:1', options);
};
/**
* Return the SmartTV "description.xml" file for the given TV "LOCATION" header.
*
* @param {string} locationURL The URL of the "description.xml" file.
* @return {Deferred.Promise} A promise to be resolved/rejected when results become available.
*/
var getDescription = function (locationURL) {
var deferred = Q.defer();
http.get(locationURL, function (xmlRes) {
var buffer = '';
var responseHasError = false;
var responseError = {};
xmlRes.on('data', function (data) {
buffer += data;
});
xmlRes.on('error', function (error) {
responseHasError = true;
responseError = error;
});
xmlRes.on('end', function (data) {
if (!responseHasError) {
deferred.resolve(buffer);
} else {
deferred.reject(responseError);
}
});
}).on('error', function (error) {
deferred.reject(error);
});
return deferred.promise;
};
return {
search: search,
getAllDevices: getAllDevices,
getAllSamsungSmartTVs: getAllSamsungSmartTVs,
getAllRenderingControls: getAllRenderingControls,
getDescription: getDescription
};
}();
/**
* Export the API Module.
*
* @type {Object}
*/
module.exports = DiscoveryService;