-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
147 lines (127 loc) · 4.17 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
var request = require("request");
var wol = require("wake_on_lan");
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-sonybraviatv", "SonyBraviaTV", SonyBraviaTVAccessory);
}
function SonyBraviaTVAccessory(log, config) {
this.log = log;
this.name = config["name"];
this.psk = config["presharedkey"];
this.ipaddress = config["ipaddress"];
this.macaddress = config["macaddress"];
this.polling = config["polling"] === true;
this.interval = parseInt(config["interval"], 10) | 1;
this.timer;
this.isOn;
this.service = new Service.Switch(this.name);
this.service
.getCharacteristic(Characteristic.On)
.on('get', this.getOn.bind(this))
.on('set', this.setOn.bind(this));
this.updateTimer();
}
SonyBraviaTVAccessory.prototype.runTimer = function() {
this.getState(function(err, isOn) {
if (err == null && isOn != this.isOn) {
this.log("State changed: %s", isOn ? "on" : "off");
this.service.getCharacteristic(Characteristic.On).updateValue(isOn);
this.isOn = isOn;
}
}.bind(this));
}
SonyBraviaTVAccessory.prototype.updateTimer = function() {
if (this.polling) {
clearTimeout(this.timer);
this.timer = setTimeout(function() {
this.runTimer();
this.updateTimer();
}.bind(this), this.interval * 1000);
}
}
SonyBraviaTVAccessory.prototype.getState = function(callback) {
var postData = JSON.stringify({
method: 'getPowerStatus',
params: [],
id: 1,
version: '1.0'
});
request.post({
url: "http://" + this.ipaddress + "/sony/system",
headers: {
'X-Auth-PSK': this.psk
},
form: postData
}, function(err, response, body) {
if (!err && response.statusCode == 200) {
var json = JSON.parse(body);
var status = json.result[0].status;
var isOn = status == "active";
callback(null, isOn); // success
} else {
if (response != null) {
this.log("Error getting TV status (status code %s): %s", response.statusCode, err);
callback(err);
} else {
callback(null, false);
}
}
}.bind(this));
}
SonyBraviaTVAccessory.prototype.getOn = function(callback) {
this.log("Getting whether Sony TV is on...");
if (this.polling) {
callback(null, this.isOn);
} else {
this.getState(function(err, isOn) {
if (err == null) this.log("State is: %s", isOn ? "on" : "off");
callback(err, isOn);
}.bind(this));
}
}
SonyBraviaTVAccessory.prototype.setOn = function(value, callback) {
value = Boolean(value);
this.log("Set state to %s", value ? "on" : "off");
if (value && this.macaddress) {
wol.wake(this.macaddress, function(error) {
if (error) {
// handle error
this.log("Error '%s' setting TV power state using WOL.", error);
callback(error);
} else {
// done sending packets
this.updateTimer();
callback();
}
}.bind(this));
} else {
var postData = JSON.stringify({
method: 'setPowerStatus',
params: [{
'status': value
}],
id: 1,
version: '1.0'
});
request.post({
url: "http://" + this.ipaddress + "/sony/system",
headers: {
'X-Auth-PSK': this.psk
},
form: postData
}, function(err, response, body) {
if (!err && response.statusCode == 200) {
this.updateTimer();
callback(); // success
} else {
this.log("Error '%s' setting TV power state. Response: %s", err, body);
callback(err || new Error("Error setting TV power state."));
}
}.bind(this));
}
}
SonyBraviaTVAccessory.prototype.getServices = function() {
return [this.service];
}