forked from rwifall/homebridge-http-extensive
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
184 lines (150 loc) · 5.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
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
"use strict";
var Service, Characteristic;
var request = require("request");
var pollingtoevent = require('polling-to-event');
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-http-switch", "http-switch", HttpSwitchAccessory);
};
function HttpSwitchAccessory(log, config) {
this.log = log;
this.name = config["name"] || "HTTP Switch";
this.checkStatus = config["checkStatus"] || "no";
this.pollingMillis = config["pollingMillis"] || 10000;
this.onUrl = config["onUrl"];
this.onBody = config["onBody"];
this.offUrl = config["offUrl"];
this.offBody = config["offBody"];
this.statusUrl = config["statusUrl"];
this.statusRegex = config["statusRegex"] || "";
this.httpMethod = config["httpMethod"] || "GET";
this.state = false;
var that = this;
this.services = {
AccessoryInformation: new Service.AccessoryInformation(),
Switch: new Service.Switch(this.name)
};
this.services.AccessoryInformation
.setCharacteristic(Characteristic.Manufacturer, "vectronic");
this.services.AccessoryInformation
.setCharacteristic(Characteristic.Model, "HTTP Switch");
switch (this.checkStatus) {
case "yes":
this.services.Switch
.getCharacteristic(Characteristic.On)
.on('get', this.getStatusState.bind(this))
.on('set', this.setPowerState.bind(this));
break;
case "polling":
this.services.Switch
.getCharacteristic(Characteristic.On)
.on('get', function(callback) {callback(null, that.state)})
.on('set', this.setPowerState.bind(this));
break;
default :
this.services.Switch
.getCharacteristic(Characteristic.On)
.on('set', this.setPowerState.bind(this));
break;
}
// Status Polling
if (this.statusUrl && this.checkStatus === "polling") {
var url = this.statusUrl;
var statusemitter = pollingtoevent(function(done) {
that.httpRequest(url, "", "GET", function(error, response, body) {
if (error) {
that.log('HTTP get status function failed: %s', error.message);
}
else {
done(null, body);
}
})
}, {longpolling:true, interval:that.pollingMillis, longpollEventName:"statuspoll"});
statusemitter.on("statuspoll", function(data) {
if (Boolean(that.statusRegex)) {
var re = new RegExp(that.statusRegex);
that.state = re.test(data);
}
else {
var binaryState = parseInt(data);
that.state = binaryState > 0;
}
that.services.Switch
.getCharacteristic(Characteristic.On)
.setValue(that.state);
});
}
}
HttpSwitchAccessory.prototype.httpRequest = function (url, body, method, callback) {
var callbackMethod = callback;
request({
url: url,
body: body,
method: method,
rejectUnauthorized: false
},
function (error, response, responseBody) {
if (callbackMethod) {
callbackMethod(error, response, responseBody)
}
else {
this.log.warn("callbackMethod not defined!");
}
})
};
HttpSwitchAccessory.prototype.getStatusState = function (callback) {
if (!this.statusUrl) {
this.log.warn("Ignoring request: No status url defined.");
callback(new Error("No status url defined."));
return;
}
var url = this.statusUrl;
var regex = this.statusRegex;
this.httpRequest(url, "", "GET", function (error, response, responseBody) {
if (error) {
this.log('HTTP get status function failed: %s', error.message);
callback(error);
}
else {
var powerOn = false;
if (Boolean(regex)) {
var re = new RegExp(regex);
powerOn = re.test(responseBody);
}
else {
var binaryState = parseInt(responseBody);
powerOn = binaryState > 0;
}
callback(null, powerOn);
}
}.bind(this));
};
HttpSwitchAccessory.prototype.setPowerState = function (powerOn, callback) {
var url;
var body;
if (!this.onUrl || !this.offUrl) {
this.log.warn("Ignoring request: No power url defined.");
callback(new Error("No power url defined."));
return;
}
if (powerOn) {
url = this.onUrl;
body = this.onBody;
} else {
url = this.offUrl;
body = this.offBody;
}
this.httpRequest(url, body, this.httpMethod, function (error, response, responseBody) {
if (error) {
this.log('HTTP set power function failed: %s', error.message);
callback(error);
}
else {
callback();
}
}.bind(this));
};
HttpSwitchAccessory.prototype.getServices = function () {
return [this.services.AccessoryInformation, this.services.Switch];
};