-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
307 lines (256 loc) · 11.6 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// ISC License
// Original work Copyright (c) 2017, Andreas Bauer
// Modified work Copyright 2020, Sander van Woensel
"use strict";
// -----------------------------------------------------------------------------
// Module variables
// -----------------------------------------------------------------------------
let Service, Characteristic, api;
// -----------------------------------------------------------------------------
// Constants
// -----------------------------------------------------------------------------
const configParser = require("homebridge-http-base").configParser;
const http = require("homebridge-http-base").http;
const PullTimer = require("homebridge-http-base").PullTimer;
const PACKAGE_JSON = require('./package.json');
const MANUFACTURER = PACKAGE_JSON.author.name;
const SERIAL_NUMBER = '001';
const MODEL = PACKAGE_JSON.name;
const FIRMWARE_REVISION = PACKAGE_JSON.version;
// -----------------------------------------------------------------------------
// Exports
// -----------------------------------------------------------------------------
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
api = homebridge;
homebridge.registerAccessory(MODEL, "HttpCurtain", HttpCurtain);
};
// -----------------------------------------------------------------------------
// Module public functions
// -----------------------------------------------------------------------------
function HttpCurtain(log, config) {
this.log = log;
this.name = config.name;
this.targetPosition = 0;
this.validateUrl = function(url, mandatory=false) {
if (config[url]) {
try {
this[url] = configParser.parseUrlProperty(config[url]);
} catch (error) {
this.log.warn("Error occurred while parsing '"+url+"': " + error.message);
this.log.warn("Aborting...");
return;
}
}
else if(mandatory) {
this.log.warn("Property '"+url+"' is required!");
this.log.warn("Aborting...");
return;
}
};
this.validateUrl('getCurrentPosUrl', true);
this.validateUrl('getPositionStateUrl');
this.validateUrl('setTargetPosUrl', true);
this.validateUrl('getTargetPosUrl');
this.validateUrl('getPositionStateUrl');
this.validateUrl('identifyUrl');
this.getCurrentPosRegEx = config.getCurrentPosRegEx || '';
this.getTargetPosRegEx = config.getTargetPosRegEx || '';
this.homebridgeService = new Service.WindowCovering(this.name);
if (config.pullInterval) {
this.pullTimer = new PullTimer(log, config.pullInterval, this.getCurrentPosition.bind(this), value => {
this.homebridgeService.setCharacteristic(Characteristic.CurrentPosition, value);
});
this.pullTimer.start();
}
this.invertPosition = config.invertPosition || false
api.on('didFinishLaunching', function() {
// Check if notificationRegistration is set, if not 'notificationRegistration' is probably not installed on the system.
if (global.notificationRegistration && typeof global.notificationRegistration === "function") {
try {
global.notificationRegistration(config.notificationID, this.handleNotification.bind(this), config.notificationPassword);
} catch (error) {
// notificationID is already taken.
}
}
}.bind(this));
}
HttpCurtain.prototype = {
identify: function (callback) {
this.log.info("Identify requested");
if (this.identifyUrl) {
http.httpRequest(this.identifyUrl, (error, response, body) => {
if (error) {
this.log.error("identify() failed: %s", error.message);
callback(error);
}
else if (response.statusCode !== 200) {
this.log.error("identify() returned http error: %s", response.statusCode);
callback(new Error("Got http error code " + response.statusCode));
}
else {
callback(null);
}
});
}
else {
callback(null);
}
},
getServices: function () {
const informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, MANUFACTURER)
.setCharacteristic(Characteristic.Model, MODEL)
.setCharacteristic(Characteristic.SerialNumber, SERIAL_NUMBER)
.setCharacteristic(Characteristic.FirmwareRevision, FIRMWARE_REVISION);
this.homebridgeService
.getCharacteristic(Characteristic.CurrentPosition)
.on("get", this.getCurrentPosition.bind(this));
this.homebridgeService
.getCharacteristic(Characteristic.PositionState)
.on("get", this.getPositionState.bind(this));
this.homebridgeService
.getCharacteristic(Characteristic.TargetPosition)
.on('get', this.getTargetPosition.bind(this))
.on('set', this.setTargetPosition.bind(this));
return [informationService, this.homebridgeService];
},
handleNotification: function(body) {
const value = body.value;
let characteristic;
switch (body.characteristic) {
case "CurrentPosition":
characteristic = Characteristic.CurrentPosition;
break;
case "PositionState":
characteristic = Characteristic.PositionState;
break;
default:
this.log.warn("Encountered unknown characteristic handling notification: " + body.characteristic);
return;
}
this.log.debug("Update received from device: " + body.characteristic + ": " + body.value);
this.homebridgeService.setCharacteristic(characteristic, value);
},
getCurrentPosition: function (callback) {
http.httpRequest(this.getCurrentPosUrl, (error, response, body) => {
if (this.pullTimer)
this.pullTimer.resetTimer();
if (error) {
this.log.error("getCurrentPosition() failed: %s", error.message);
callback(error);
}
else if (response.statusCode !== 200) {
this.log.error("getCurrentPosition() returned http error: %s", response.statusCode);
callback(new Error("Got http error code " + response.statusCode));
}
else {
if(this.getCurrentPosRegEx) {
let matches = body.match(this.getCurrentPosRegEx);
if(matches && matches.length > 1) {
body = matches[1];
this.log.debug("Retrieving current position via regular expression. Full ungrouped match: %s", matches[0]);
}
else {
this.log.warn("Your CurrentPosRegEx regular expression: \"%s\" did not match any part of the returned body: \"%s\"", this.getCurrentPosRegEx, body);
}
}
let posValue = parseInt(body);
this.log.info("Current position (retrieved via http): %s\%", posValue);
if (this.invertPosition) {
posValue = 100 - posValue;
}
callback(null, posValue);
}
});
},
// Seems like HomeKit doesn't care about the state, but rather compares target and current pos.
getPositionState: function (callback) {
if (this.getPositionStateUrl) { // Position state URL is optional
http.httpRequest(this.getPositionStateUrl, (error, response, body) => {
if (this.pullTimer)
this.pullTimer.resetTimer();
if (error) {
this.log.error("getPositionState() failed: %s", error.message);
callback(error);
}
else if (response.statusCode !== 200) {
this.log.error("getPositionState() returned http error: %s", response.statusCode);
callback(new Error("Got http error code " + response.statusCode));
}
else {
const state = parseInt(body);
this.log.info("Position state: %s", state);
callback(null, state);
}
});
} else {
this.log.debug("Position state URL not configured. Returning: Stopped ("+Characteristic.PositionState.STOPPED+")");
callback(null, Characteristic.PositionState.STOPPED); // No state defined.
}
},
setTargetPosition: function (value, callback) {
this.targetPosition = value;
if (this.invertPosition) {
value = 100 - value;
}
// Replace %d with target position.
let urlObj = {...this.setTargetPosUrl};
urlObj.url = urlObj.url.replace(/%d/g, value.toString());
urlObj.body = urlObj.body.replace(/%d/g, value.toString());
this.log.info("Requesting: %s for value: %d", urlObj.url, value);
http.httpRequest(urlObj, (error, response, body) => {
if (error) {
this.log.error("setTargetPositionUrl() failed: %s", error.message);
callback(error);
}
else if (response.statusCode !== 200) {
this.log.error("setTargetPositionUrl() returned http error: %s; body: %s", response.statusCode), body;
callback(new Error("Got http error code " + response.statusCode));
}
else {
this.log.debug("Succesfully requested target position: %d\%", value);
callback(null);
}
});
},
getTargetPosition: function (callback) {
if (this.getTargetPosUrl) { // Target position URL is optional
http.httpRequest(this.getTargetPosUrl, (error, response, body) => {
if (error) {
this.log.error("getTargetPosition() failed: %s", error.message);
callback(error);
}
else if (response.statusCode !== 200) {
this.log.error("getTargetPosition() returned http error: %s", response.statusCode);
callback(new Error("Got http error code " + response.statusCode));
}
else {
if(this.getTargetPosRegEx) {
let matches = body.match(this.getTargetPosRegEx);
if(matches && matches.length > 1) {
body = matches[1];
this.log.debug("Retrieving target position via regular expression. Full ungrouped match: %s", matches[0]);
}
else {
this.log.warn("Your TargetPosRegEx regular expression: \"%s\" did not match any part of the returned body: \"%s\"", this.getTargetPosRegEx, body);
}
}
let targetPosition = parseInt(body);
this.log.info("Target position (retrieved via http): %s\%", targetPosition);
if (this.invertPosition) {
targetPosition = 100 - targetPosition;
}
callback(null, targetPosition);
}
});
}
else
{
this.log.info("Target position (retrieved from cache): %s\%", this.targetPosition);
callback(null, this.targetPosition);
}
},
};