forked from ToddGreenfield/homebridge-directv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
352 lines (325 loc) · 15.2 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
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
'use strict';
// Directv Platform for HomeBridge
//
// Remember to add platform to config.json. Example:
// "platforms": [
// {
// "platform": "Directv", // required
// "name": "DTV", // Optional - defaults to DTV
// "ip_address": "IP of Primary STB", // required
// "exclude_geni": true, // Optional - defaults to false
// "min_channel": 1, // Optional - defaults to 1
// "max_channel": 575 // Optional - defaults to 575
// }
// ],
//
//
var inherits = require('util').inherits;
var DirecTV = require('directv-remote');
var titleCase = require('title-case');
var remote, min_ch, max_ch;
function DirectvPlatform(log, config){
this.config = config;
this.ip_address = config["ip_address"];
this.name = config["name"] || 'DTV';
// Typically defaults to 1, but we need to do some checking below.
// Older units won't accept anything but an undefined value.
this.compatType = undefined;
this.excludeGeni = config["exclude_geni"] || false;
min_ch = config["min_channel"] || 1;
max_ch = config["max_channel"] || 575;
this.log = log;
if (!this.ip_address) throw new Error("DTV - You must provide a config value for 'ip_address'.");
var that=this;
// Determine compatibility type for /info/getLocations.
// Should only need to do this once per "root" STB.
// This means we'll have to iterate remotes each time we want to do this. Oi.
remote = new DirecTV.Remote(this.ip_address);
var response = undefined;
// getOptions expects a hash as input (despite the lack of argument):
// var options = {
// hostname: this.IP_ADDRESS,
// port: 8080,
// path: path
//};
// Will need to loop through every host setting up these to match the host (really, just IPs)
// then call get options one at a time.
remote.getOptions( function(err,response) {
if(!err){
optionsLabel:
{
for(var i=0; i <response.options.length; ++i){
if (response.options[i].command=="/info/getLocations") {
that.log("DTV Found /info/getLocations! Has %s urlParameters.",response.options[i].urlParameters.length);
urlParametersLabel:
for(var paramIndex=0; paramIndex < response.options[i].urlParameters.length; ++paramIndex){
that.log("DTV Checking response.options[%s].urlParameters[%s], with value %s",i,paramIndex,response.options[i].urlParameters[paramIndex]);
if (!response.options[i].urlParameters[paramIndex].name){
that.log("DTV STB at %s does not have a 'name' urlParameter.",that.ip_address);
if(paramIndex < response.options[i].urlParameters.length){
continue;
}
else{
that.log("DTV STB at %s is a legacy STB, and undefined should be sent to getLocations()",that.ip_address);
break urlParametersLabel;
}
}
else if (response.options[i].urlParameters[paramIndex].name == 'type') {
// If .name has a value, we'll be sending ?type=1 on getLocations.
that.log("DTV STB at %s is a modern STB, and we must submit 'type' to /info/getLocations!",that.ip_address);
++that.compatType;
// No need to keep searching.
break urlParametersLabel;
}
}
// Once we have found the /info/getLocations command, there's no need to
// continue iterating. This should break us out of the whole block.
break optionsLabel;
}
} // End optionsLabel block.
}
} else{
this.log( "DTV - failed to retrieve /info/getOptions for IP/hostname this.ip_address. Check IP address in config.json!");
try {
callback(null);
}
catch(error) {
that.log.debug('homebridge DTV Error', error);
}
}
});
}
DirectvPlatform.prototype = {
accessories: function(callback) {
this.log("Fetching DTV locations.");
var that = this;
var foundAccessories = [];
remote.getLocations(that.compatType, function(err, response) {
if (!err) {
that.log( "Finding DTV Locations (accessories)...");
for(var i=0; i <response.locations.length; ++i){
if ((!that.excludeGeni) || (response.locations[i].clientAddr == '0')) {
that.log( "Found DTV Location %s", response.locations[i].locationName);
var accessory = new DirectvSTBAccessory(that.log, response.locations[i], that.config);
foundAccessories.push(accessory);
} else {
that.log( "Found DTV Location %s, but EXCLUDING per configuration setting.", response.locations[i].locationName);
}
}
try {
callback(foundAccessories);
}
catch(error) {
that.log.debug('homebridge DTV Error', error);
}
} else {
that.log( "Failed to find any DTV Locations (accessories) - Check IP address in config.json!");
try {
callback(null);
}
catch(error) {
that.log.debug('homebridge DTV Error', error);
}
}
}
);
}
}
function DirectvSTBAccessory(log, location, config) {
// STB basic device info
this.log = log;
this.config = config;
this.location = titleCase(location.locationName);
this.config_name = config["name"] || 'DTV';
this.name = this.location + ' ' + this.config_name;
this.ip_address = config["ip_address"];
this.clientAddr = location.clientAddr.toUpperCase();
}
DirectvSTBAccessory.prototype = {
getRemote: function(type, callback){
var that = this;
switch(type) {
case "power":
remote.getMode(that.clientAddr, function (err, response) {
if (err || response.mode == "1") {
that.log.debug('DTV location %s power state is currently OFF or UNREACHABLE', that.location);
that.log.debug('DTV IP Address %s for location %s.', that.ip_address, that.location);
that.log.debug('DTV clientAddr is %s for location %s.', that.clientAddr, that.location);
try {
callback(null, false);
}
catch(error) {
that.log.debug('homebridge DTV Error', error);
}
return;
} else if (response.mode == "0") {
that.log.debug('DTV location %s power state is currently ON', that.location);
try {
callback(null, true);
}
catch(error) {
that.log.debug('homebridge DTV Error', error);
}
}
});
break;
case "channel":
remote.getMode(that.clientAddr, function (err, response) {
if (err || response.mode == "1") {
that.log.debug('Unable to call for current channel at DTV location %s.', that.location);
try {
callback(null, parseInt("0"));
}
catch(error) {
that.log.debug('homebridge DTV Error', error);
}
} else if (response.mode == "0") {
remote.getTuned(that.clientAddr, function (err, response) {
if (!err) {
that.log.debug('DTV location %s current channel is %d', that.location, parseInt(response.major));
try {
callback(null, parseInt(response.major));
}
catch(error) {
that.log.debug('homebridge DTV Error', error);
}
} else {
that.log.debug('Unable to call for current channel at DTV location %s.', that.location);
try {
callback(null, parseInt("0"));
}
catch(error) {
that.log.debug('homebridge DTV Error', error);
}
}
});
}
});
break;
}
},
setRemote: function(type, value, callback){
var that = this;
switch(type) {
case "power":
remote.getMode(that.clientAddr, function (err, response) {
var responseMode = response.mode;
if (err) { responseMode = "1"}
if (parseInt(responseMode) === parseInt(value ? 1 : 0)) {
remote.processKey('power', that.clientAddr, function(err, response) {
if (err) {
that.log.debug('Unable to change DTV location %s power state!', that.location);
try {
callback(new Error("STB Process Power Error."), false);
}
catch(error) {
that.log.debug('homebridge DTV Error', error);
}
return;
} else {
that.log.debug('DTV location %s power state is now: %s', that.location, (value) ? 'ON' : 'OFF');
try {
callback();
}
catch(error) {
that.log.debug('homebridge DTV Error', error);
}
}
});
} else {
try {
callback();
}
catch(error) {
that.log.debug('homebridge DTV Error', error);
}
}
});
break;
case "channel":
remote.getMode(that.clientAddr, function (err, response) {
if (err || response.mode == "1") {
that.log.debug('Unable to set channel at DTV location %s.', that.location);
try {
callback();
}
catch(error) {
that.log.debug('homebridge DTV Error', error);
}
} else if (response.mode == "0") {
remote.tune(value, that.clientAddr, function(err, response) {
if (err) {
that.log.debug('Unable to set channel at DTV location %s', that.location);
try {
callback();
}
catch(error) {
that.log.debug('homebridge DTV Error', error);
}
} else {
that.log.debug('DTV location %s Channel is now: %s', that.location, value);
try {
callback();
}
catch(error) {
that.log.debug('homebridge DTV Error', error);
}
}
});
}
});
break;
}
},
getServices: function() {
var that = this;
var services = []
if (this.clientAddr == '0') {
this.service = new Service.Switch(this.name);
this.service.getCharacteristic(Characteristic.On)
.on('get', function(callback) { that.getRemote("power", callback);})
.on('set', function(value, callback) {that.setRemote("power", value, callback);});
} else {
this.service = new Service.MotionSensor(this.name);
this.service.getCharacteristic(Characteristic.MotionDetected)
.on('get', function(callback) { that.getRemote("power", callback);});
}
this.service.addCharacteristic(ChannelCharacteristic)
.on('get', function(callback) { that.getRemote("channel", callback);})
.on('set', function(value, callback) {that.setRemote("channel", value, callback);});
services.push(this.service);
var service = new Service.AccessoryInformation();
service.setCharacteristic(Characteristic.Manufacturer, "Directv")
.setCharacteristic(Characteristic.Name, this.name)
.setCharacteristic(Characteristic.SerialNumber, this.clientAddr)
.setCharacteristic(Characteristic.Model, this.ip_address);
services.push(service);
return services;
}
}
module.exports.accessory = DirectvSTBAccessory;
module.exports.platform = DirectvPlatform;
var Service, Characteristic, ChannelCharacteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
// we can only do this after we receive the homebridge API object
makeChannelCharacteristic();
homebridge.registerAccessory("homebridge-directv", "DirectvSTB", DirectvSTBAccessory);
homebridge.registerPlatform("homebridge-directv", "Directv", DirectvPlatform);
};
function makeChannelCharacteristic() {
ChannelCharacteristic = function () {
Characteristic.call(this, 'Channel', '212131F4-2E14-4FF4-AE13-C97C3232499D');
this.setProps({
format: Characteristic.Formats.INT,
unit: Characteristic.Units.NONE,
maxValue: max_ch,
minValue: min_ch,
minStep: 1,
perms: [Characteristic.Perms.READ, Characteristic.Perms.WRITE, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
};
inherits(ChannelCharacteristic, Characteristic);
}