-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
88 lines (74 loc) · 2.3 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
var Service, Characteristic;
var request = require('request');
var url
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-http-switch", "HTTPSwitch", HTTPSwitch);
}
function HTTPSwitch(log, config) {
this.log = log;
this.toggle = config["toggle_url"];
this.status = config["status_url"]
this.name = config["name"];
}
HTTPSwitch.prototype = {
getPowerState: function (callback) {
request.get({
url: this.status
}, function(error, response, body) {
if (!error && response.statusCode == 200){
if(body == "1"){
this.log("HTTP status 1 received!")
callback(null, true);
}else{
this.log("HTTP status 0 received!")
callback(null, false);
}
}else{
this.log('HTTP status failed for ' + toggle);
callback(error);
}
}.bind(this));
},
setPowerState: function(powerOn, callback) {
request.get({
url: this.toggle
}, function(error, response, body) {
if (!error && response.statusCode == 200){
if(body == "1"){
this.log("HTTP status 1 received!")
}else{
this.log("HTTP status 0 received!")
}
if( (body == "1") == powerOn ){
this.log("HTTP toggle successfull!")
callback(null, powerOn)
}else{
this.log("HTTP toggle failed! " + body)
callback(null, !powerOn)
}
}else{
this.log('HTTP status failed for ' + toggle);
callback(error);
}
}.bind(this));
},
identify: function (callback) {
this.log("Identify request received!");
callback();
},
getServices: function () {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "HTTP Manufacturer")
.setCharacteristic(Characteristic.Model, "HTTP Model")
.setCharacteristic(Characteristic.SerialNumber, "HTTP Serial Number");
switchService = new Service.Switch(this.name);
switchService
.getCharacteristic(Characteristic.On)
.on('get', this.getPowerState.bind(this))
.on('set', this.setPowerState.bind(this));
return [switchService];
}
};