-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
58 lines (46 loc) · 1.46 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
var Xbox = require('xbox-on');
var ping = require('ping');
var Service, Characteristic;
module.exports = function(homebridge){
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-xbox-one", "Xbox", XboxAccessory);
};
function XboxAccessory(log, config) {
this.log = log;
this.name = config['name'] || 'Xbox';
this.xbox = new Xbox(config['ipAddress'], config['liveId']);
this.tries = config['tries'] || 5;
this.tryInterval = config['tryInterval'] || 1000;
this.waitForLastTry = config['waitForLastTry'] || false;
}
XboxAccessory.prototype = {
setPowerState: function(powerOn, callback) {
this.log("Sending on command to '" + this.name + "'...");
// Queue tries times at tryInterval
this.xbox.powerOn({
tries: this.tries,
delay: this.tryInterval,
waitForCallback: this.waitForLastTry
}, callback);
},
getPowerState: function(callback) {
ping.sys.probe(this.xbox.ip, function(isAlive){
callback(null, isAlive);
});
},
identify: function(callback) {
this.log("Identify...");
callback();
},
getServices: function() {
var switchService = new Service.Switch(this.name);
switchService
.getCharacteristic(Characteristic.On)
.on('set', this.setPowerState.bind(this));
switchService
.getCharacteristic(Characteristic.On)
.on('get', this.getPowerState.bind(this));
return [switchService];
}
};