forked from dxdc/homebridge-blinds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
157 lines (134 loc) · 5.7 KB
/
index.js
File metadata and controls
157 lines (134 loc) · 5.7 KB
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
var request = require("request");
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-blinds", "BlindsHTTP", BlindsHTTPAccessory);
}
function BlindsHTTPAccessory(log, config) {
// global vars
this.log = log;
// configuration vars
this.name = config["name"];
this.upURL = config["up_url"];
this.downURL = config["down_url"];
this.stopURL = config["stop_url"];
this.stopAtBoundaries = config["trigger_stop_at_boundaries"];
this.httpMethod = config["http_method"] || "POST";
this.motionTime = config["motion_time"];
// state vars
this.interval = null;
this.timeout = null;
this.lastPosition = 0; // last known position of the blinds, down by default
this.currentPositionState = 2; // stopped by default
this.currentTargetPosition = 0; // down by default
// register the service and provide the functions
this.service = new Service.WindowCovering(this.name);
// the current position (0-100%)
// https://github.com/KhaosT/HAP-NodeJS/blob/master/lib/gen/HomeKitTypes.js#L493
this.service
.getCharacteristic(Characteristic.CurrentPosition)
.on('get', this.getCurrentPosition.bind(this));
// the position state
// 0 = DECREASING; 1 = INCREASING; 2 = STOPPED;
// https://github.com/KhaosT/HAP-NodeJS/blob/master/lib/gen/HomeKitTypes.js#L1138
this.service
.getCharacteristic(Characteristic.PositionState)
.on('get', this.getPositionState.bind(this));
// the target position (0-100%)
// https://github.com/KhaosT/HAP-NodeJS/blob/master/lib/gen/HomeKitTypes.js#L1564
this.service
.getCharacteristic(Characteristic.TargetPosition)
.on('get', this.getTargetPosition.bind(this))
.on('set', this.setTargetPosition.bind(this));
}
BlindsHTTPAccessory.prototype.getCurrentPosition = function(callback) {
this.log("Requested CurrentPosition: %s", this.lastPosition);
callback(null, this.lastPosition);
}
BlindsHTTPAccessory.prototype.getPositionState = function(callback) {
this.log("Requested PositionState: %s", this.currentPositionState);
callback(null, this.currentPositionState);
}
BlindsHTTPAccessory.prototype.getTargetPosition = function(callback) {
this.log("Requested TargetPosition: %s", this.currentTargetPosition);
callback(null, this.currentTargetPosition);
}
BlindsHTTPAccessory.prototype.setTargetPosition = function(pos, callback) {
this.log("Set TargetPosition: %s", pos);
this.currentTargetPosition = pos;
const moveUp = (this.currentTargetPosition == 100) || (this.currentTargetPosition > this.lastPosition);
this.log("Last Position was %s", this.lastPosition);
this.log((moveUp ? "Moving up" : "Moving down"));
this.service
.setCharacteristic(Characteristic.PositionState, (moveUp ? 1 : 0));
this.httpRequest((moveUp ? this.upURL : this.downURL), this.httpMethod, function() {
this.log(
"Success moving %s",
(moveUp ? "up (to " + pos + ")" : "down (to " + pos + ")")
);
this.service
.setCharacteristic(Characteristic.CurrentPosition, pos);
this.service
.setCharacteristic(Characteristic.PositionState, 2);
}.bind(this));
var localThis = this;
if (this.interval != null) clearInterval(this.interval);
if (this.timeout != null) clearTimeout(this.timeout);
this.interval = setInterval(function(){
localThis.lastPosition += (moveUp ? 1 : -1);
if (localThis.lastPosition > 100){
localThis.lastPosition = 100;
}
if (localThis.lastPosition < 0){
localThis.lastPosition = 0;
}
if (localThis.lastPosition == localThis.currentTargetPosition) {
if (localThis.currentTargetPosition != 0 && localThis.currentTargetPosition != 100) {
localThis.httpRequest(localThis.stopURL, localThis.httpMethod, function() {
localThis.log(
"Success stop moving %s",
(moveUp ? "up (to " + pos + ")" : "down (to " + pos + ")")
);
localThis.service
.setCharacteristic(Characteristic.CurrentPosition, pos);
localThis.service
.setCharacteristic(Characteristic.PositionState, 2);
localThis.lastPosition = pos;
}.bind(localThis));
}
clearInterval(localThis.interval);
}
}, parseInt(this.motionTime) / 100);
if (this.stopAtBoundaries && (this.currentTargetPosition == 0 || this.currentTargetPosition == 100)) {
this.timeout = setTimeout(function() {
localThis.httpRequest(localThis.stopURL, localThis.httpMethod, function() {
localThis.log(
"Success stop adjusting moving %s",
(moveUp ? "up (to " + pos + ")" : "down (to " + pos + ")")
);
}.bind(localThis));
}, parseInt(this.motionTime));
}
callback(null);
}
BlindsHTTPAccessory.prototype.httpRequest = function(url, method, callback) {
request({
method: method,
url: url,
}, function(err, response, body) {
if (!err && response && response.statusCode == 200) {
callback(null);
} else {
this.log(
"Error getting state (status code %s): %s",
(response ? response.statusCode : "not defined"),
err
);
callback(err);
}
}.bind(this));
}
BlindsHTTPAccessory.prototype.getServices = function() {
return [this.service];
}