-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·199 lines (160 loc) · 6.38 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
var gpio = require('pi-gpio');
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-gpio-blinds", "GPIO-Blinds", GPIOBlindsAccessory);
}
function GPIOBlindsAccessory(log, config) {
// global vars
this.log = log;
// configuration vars
this.name = config["name"];
this.upPin = config["up_pin"];
this.downPin = config["down_pin"];
this.stopPin = config["stop_pin"];
this.presetPosition = config["preset_position"];
this.motionTime = config["motion_time"];
this.motionLag = config["motion_lag"];
// state vars
this.interval = null;
this.movingToPreset = false;
this.lastPosition = 100; // last known position of the blinds, up by default
this.currentPositionState = 2; // stopped by default
this.currentTargetPosition = 100; // up by default
this.awaitingMotion = false;
// 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));
}
GPIOBlindsAccessory.prototype.getCurrentPosition = function(callback) {
this.log("Requested CurrentPosition: %s", this.lastPosition);
callback(null, this.lastPosition);
}
GPIOBlindsAccessory.prototype.getPositionState = function(callback) {
this.log("Requested PositionState: %s", this.currentPositionState);
callback(null, this.currentPositionState);
}
GPIOBlindsAccessory.prototype.getTargetPosition = function(callback) {
this.log("Requested TargetPosition: %s", this.currentTargetPosition);
callback(null, this.currentTargetPosition);
}
GPIOBlindsAccessory.prototype.setTargetPosition = function(pos, callback) {
this.log("Set TargetPosition: %s", pos);
this.currentTargetPosition = pos;
this.movingToPreset = false;
if (this.currentTargetPosition == this.lastPosition) {
this.log("currentPositionState -> Stopped");
if (this.currentPositionState != 2) {
this.pulsePin(this.stopPin);
} else if(pos == 100) {
this.pulsePin(this.upPin);
} else if(pos == 0) {
this.pulsePin(this.downPin);
}
this.currentPositionState = 2;
} else if (this.currentTargetPosition == this.presetPosition && this.currentPositionState == 2) {
this.currentPositionState = this.currentTargetPosition > this.lastPosition ? 1 : 0;
if (this.currentPositionState) {
this.log("currentPositionState -> Opening (towards Preset)");
} else {
this.log("currentPositionState -> Closing (towards Preset)");
}
this.pulsePin(this.stopPin);
this.movingToPreset = true;
} else if (this.currentTargetPosition > this.lastPosition) {
this.log("currentPositionState -> Opening");
if (this.currentPositionState != 1) {
this.pulsePin(this.upPin);
this.awaitMotion();
}
this.currentPositionState = 1;
} else {
this.log("currentPositionState -> Closing");
if (this.currentPositionState != 0) {
this.pulsePin(this.downPin);
this.awaitMotion();
}
this.currentPositionState = 0;
}
this.service.setCharacteristic(Characteristic.PositionState, this.currentPositionState);
if (this.currentPositionState != 2) {
if (!this.interval) {
this.interval = setInterval(this.motionTick.bind(this), parseInt(this.motionTime) / 100);
}
}
callback(null);
}
GPIOBlindsAccessory.prototype.motionTick = function() {
if (this.awaitingMotion) {
this.log("motionTick warming up");
return;
}
switch (this.currentPositionState) {
case 0:
//Closing
this.lastPosition--;
break;
case 1:
//Opening
this.lastPosition++;
break;
}
if (!(this.lastPosition % 10)) {
this.log("motionTick estimated position %s -> %s", this.lastPosition, this.currentTargetPosition);
this.service.setCharacteristic(Characteristic.CurrentPosition, this.lastPosition);
}
if (this.lastPosition == this.currentTargetPosition) {
this.log("Reached target position (%s%%).", this.lastPosition);
this.currentPositionState = 2;
this.service.setCharacteristic(Characteristic.CurrentPosition, this.lastPosition);
this.service.setCharacteristic(Characteristic.PositionState, this.currentPositionState);
//Pulse stop pin if stopping halfway
if (this.lastPosition != 0 && this.lastPosition != 100 && !this.movingToPreset) {
this.pulsePin(this.stopPin);
}
//Stop our timer
clearInterval(this.interval);
this.interval = null;
this.movingToPreset = false;
}
}
GPIOBlindsAccessory.prototype.awaitMotion = function() {
if (this.motionLag) {
this.awaitingMotion = true;
setTimeout(function() {
this.awaitingMotion = false;
}.bind(this), this.motionLag);
}
}
GPIOBlindsAccessory.prototype.pulsePin = function(pin) {
this.log("pulsePin %s", pin);
gpio.open(pin, "output", function(err) { // Open pin for output
gpio.write(pin, 0, function() { // Set pin low
setTimeout(function() {
gpio.setDirection(pin, "input", function() { // Set pin back to floating
gpio.close(pin); // Close pin
});
}, 333);
});
});
}
GPIOBlindsAccessory.prototype.getServices = function() {
return [this.service];
}