-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
320 lines (271 loc) · 10.4 KB
/
index.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
var http = require('http');
var qs = require("querystring");
module.exports.validateIP = function(IP_ADDRESS, callback){
if (!/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/.test(IP_ADDRESS)) {
return callback(new Error('This is not a valid IPv4 address: ' + IP_ADDRESS));
}
var options = {
hostname: IP_ADDRESS,
port: 8080,
path: '/info/getLocations'
};
const TIMEOUT_DURATION = 1500;
var req = http.request(options, function(res) {
var body = "";
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
try {
var parsedBody = JSON.parse(body);
} catch (err) {
callback(new Error('Parsing the request body failed: ' + err));
}
if (typeof parsedBody !== 'undefined' && typeof parsedBody.status !== 'undefined') {
if (parsedBody.status.code !== 200) {
callback(new Error('Host does not appear to be a valid STB: ' + parsedBody.status.code + ' (' + parsedBody.status.msg + ')'));
} else {
callback(null);
}
} else {
callback(new Error('Host does not appear to be a valid STB'));
}
});
});
req.on('error', function(err) {
if (err.message !== 'socket hang up') {
callback(new Error('HTTP request failed: ' + err));
}
});
req.setTimeout(TIMEOUT_DURATION, function() {
req.abort();
callback(new Error('HTTP request timed out after ' + TIMEOUT_DURATION + ' ms'));
});
req.end();
}
module.exports.Remote = function(ipAddr) {
if (typeof ipAddr === 'undefined') {
throw new Error('ipAddr is a required parameter');
}
this.IP_ADDRESS = ipAddr;
this.port = '8080';
// Lists the available endpoints on the system
this.getOptions = function(callback){
var path = '/info/getOptions';
var options = {
hostname: this.IP_ADDRESS,
port: 8080,
path: path
};
makeRequest(options, callbackHandler(callback));
};
// "List of available client locations."
// Returns an array of the networked set top boxes
// type is an optional parameter
// the docs label it as 'int'
// only 0 and 1 aren't *Forbidden*
// i'm not sure what the difference is yet,
// but 1 shows more of my wireless Genie STBs
this.getLocations = function(type, callback){
var path = '/info/getLocations';
var options = {
hostname: this.IP_ADDRESS,
port: 8080,
path: path
};
if (typeof type !== 'undefined') {
options.path = buildQueryString(options.path, { type: type });
}
makeRequest(options, callbackHandler(callback));
};
// "STB serial number."
// clientAddr is optional and for specifying a separate networked STB
this.getSerialNum = function(clientAddr, callback){
var path = '/info/getSerialNum';
var options = {
hostname: this.IP_ADDRESS,
port: 8080,
path: path
};
if (typeof clientAddr !== 'undefined') {
options.path = buildQueryString(options.path, { clientAddr: clientAddr });
}
makeRequest(options, callbackHandler(callback));
};
// "Set-top-box and SHEF information."
// Also returns the systemTime property, which is the current epoch timestamp.
this.getVersion = function(callback){
var path = '/info/getVersion';
var options = {
hostname: this.IP_ADDRESS,
port: 8080,
path: path
};
makeRequest(options, callbackHandler(callback));
};
// "Set-top-box mode."
// clientAddr is optional and for specifying a separate networked STB
// It seems the returned mode property reflects the statuses active (1) and inactive (0)
this.getMode = function(clientAddr, callback){
var path = '/info/mode';
var options = {
hostname: this.IP_ADDRESS,
port: 8080,
path: path
};
if (typeof clientAddr !== 'undefined') {
options.path = buildQueryString(options.path, { clientAddr: clientAddr });
}
makeRequest(options, callbackHandler(callback));
};
// "Process a key request from the remote control."
// key is a required string value, that corresponds to buttons on the remote, such as:
// format, power, rew, pause, play, stop, ffwd, replay, advance, record, guide, active, list, exit, up, down, select, left, right, back, menu, info, red, green, yellow, blue, chanup, chandown, prev, 1, 2, 3, 4, 5, 6, 7, 8, 9, dash, 0, enter
this.processKey = function(key, clientAddr, callback){
var path = '/remote/processKey';
var options = {
hostname: this.IP_ADDRESS,
port: 8080,
path: path + '?key=' + key
};
if (typeof clientAddr !== 'undefined') {
options.path = buildQueryString(options.path, { clientAddr: clientAddr });
}
makeRequest(options, callbackHandler(callback));
};
// "Process a command request from remote control."
// cmd is a required hex value, such as:
// 'FA81' Standby
// 'FA82' Active
// 'FA83' GetPrimaryStatus
// 'FA84' GetCommandVersion
// 'FA87' GetCurrentChannel
// 'FA90' GetSignalQuality
// 'FA91' GetCurrentTime
// 'FA92' GetUserCommand
// 'FA93' EnableUserEntry
// 'FA94' DisableUserEntry
// 'FA95' GetReturnValue
// 'FA96' Reboot
// 'FAA5' SendUserCommand
// 'FAA6' OpenUserChannel
// 'FA9A' GetTuner
// 'FA8A' GetPrimaryStatusMT
// 'FA8B' GetCurrentChannelMT
// 'FA9D' GetSignalQualityMT
// 'FA9F' OpenUserChannelMT
this.processCommand = function(cmd, callback){
var path = '/serial/processCommand';
var options = {
hostname: this.IP_ADDRESS,
port: 8080,
path: path + '?cmd=' + cmd
};
makeRequest(options, callbackHandler(callback));
};
// "Program information of specified channel at current or specific time."
// Returns program information for the specified channel, time and STB
// startTime is an optional epoch timestamp, default is now
// clientAddr is optional and for specifying a separate networked STB
this.getProgInfo = function(channel, startTime, clientAddr, callback){
var path = '/tv/getProgInfo';
var options = {
hostname: this.IP_ADDRESS,
port: 8080,
path: path + '?major=' + channel
};
if (typeof startTime !== 'undefined') {
options.path = buildQueryString(options.path, { time: startTime });
}
if (typeof clientAddr !== 'undefined') {
options.path = buildQueryString(options.path, { clientAddr: clientAddr });
}
makeRequest(options, callbackHandler(callback));
};
// "Information about the currently viewed program."
// clientAddr is optional and for specifying a separate networked STB
this.getTuned = function(clientAddr, callback){
var path = '/tv/getTuned';
var options = {
hostname: this.IP_ADDRESS,
port: 8080,
path: path
};
if (typeof clientAddr !== 'undefined') {
options.path = buildQueryString(options.path, { clientAddr: clientAddr });
}
makeRequest(options, callbackHandler(callback));
};
// "Tune to a channel."
// clientAddr is optional and for specifying a separate networked STB
this.tune = function(channel, clientAddr, callback){
var path = '/tv/tune';
var options = {
hostname: this.IP_ADDRESS,
port: 8080,
path: path + '?major=' + channel
};
if (typeof clientAddr !== 'undefined') {
options.path = buildQueryString(options.path, { clientAddr: clientAddr });
}
makeRequest(options, callbackHandler(callback));
};
// This is a utility function that allows the same optional callback for all of the separate Remote functions
var callbackHandler = function(callback) {
return function(err, response) {
if (typeof callback !== 'undefined') {
if (err) {
callback(err);
} else {
callback(null, response);
}
} else {
if (err) {
console.log(err);
} else {
console.log(response);
}
}
}
};
// This is a utility function for building URL paths
var buildQueryString = function(path, qs) {
firstVar = (path.indexOf('?') === -1) ? true : false;
for (prop in qs) {
if (firstVar) {
path = path + '?' + prop + '=' + qs[prop];
firstVar = false;
} else {
path = path + '&' + prop + '=' + qs[prop];
}
}
return path;
};
// This is the shared function that actually makes the HTTP requests
var makeRequest = function(options, callback){
var body = '';
http.get(options, function(res) {
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
try {
var parsedBody = JSON.parse(body);
} catch (err) {
return callback(new Error('Parsing the request body failed: ' + err));
}
if (typeof parsedBody !== 'undefined' && typeof parsedBody.status !== 'undefined') {
//console.log('Path:',parsedBody.status.query);
if (parsedBody.status.code !== 200) {
return callback(new Error('Received bad response code: ' + parsedBody.status.code + ' (' + parsedBody.status.msg + ')'));
} else {
delete parsedBody.status;
}
}
return callback(null, parsedBody);
});
}).on('error', function(err) {
return callback(new Error('HTTP request failed: ' + err));
});
};
};