forked from rudders/homebridge-http
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
93 lines (75 loc) · 2.25 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
var Service, Characteristic;
var http = require("http");
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-irkit", "IRKit", IRKitAccessory);
}
function IRKitAccessory(log, config) {
this.log = log;
// url info
this.irkit_host = config["irkit_host"];
this.on_form = config["on_form"];
this.off_form = config["off_form"];
this.name = config["name"];
}
IRKitAccessory.prototype = {
httpRequest: function (host, form, callback) {
var formData = JSON.stringify(form);
var req = http.request({
host: host,
path: "/messages",
method: "POST",
headers: {
"X-Requested-With": "homebridge-irkit",
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
"Content-Length": formData.length
}
}, function (response) {
callback(response);
});
req.on('error', function (response) {
callback(response);
});
req.write(formData);
req.end();
},
setPowerState: function (powerOn, callback) {
var form;
if (powerOn) {
form = this.on_form;
this.log("Setting power state to on");
} else {
form = this.off_form;
this.log("Setting power state to off");
}
this.httpRequest(this.irkit_host, form, function (response) {
if (response.statusCode == 200) {
this.log('IRKit power function succeeded!');
callback();
} else {
this.log(response.message);
this.log('IRKit power function failed!');
callback('error');
}
}.bind(this));
},
identify: function (callback) {
this.log("Identify requested!");
callback(); // success
},
getServices: function () {
// you can OPTIONALLY create an information service if you wish to override
// the default values for things like serial number, model, etc.
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "IRKit Manufacturer")
.setCharacteristic(Characteristic.Model, "IRKit Model")
.setCharacteristic(Characteristic.SerialNumber, "IRKit Serial Number");
var switchService = new Service.Switch(this.name);
switchService
.getCharacteristic(Characteristic.On)
.on('set', this.setPowerState.bind(this));
return [switchService];
}
};