-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
130 lines (117 loc) · 4.96 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
const gpio = require('rpi-gpio');
gpio.setMode(gpio.MODE_BCM);
let Service, Characteristic, TargetDoorState, CurrentDoorState;
const OFF = true;
const ON = false;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
TargetDoorState = Characteristic.TargetDoorState;
CurrentDoorState = Characteristic.CurrentDoorState;
homebridge.registerAccessory('homebridge-garage-door-opener', 'Garage Door Opener', GarageDoorOpener);
};
class GarageDoorOpener {
constructor(log, config) {
this.log = log;
this.name = config.name;
this.openCloseTime = config.openCloseTime;
this.doorRelayPin = config.doorRelayPin;
gpio.setup(this.doorRelayPin, gpio.DIR_HIGH);
this.currentDoorState = CurrentDoorState.CLOSED;
this.targetDoorState = TargetDoorState.CLOSED;
}
identify(callback) {
this.log('Identify requested!');
callback(null);
}
openCloseGarage(callback) {
gpio.write(this.doorRelayPin, ON);
setTimeout(() => {
gpio.write(this.doorRelayPin, OFF);
callback();
}, 500);
}
getServices() {
const informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, 'Encore Dev Labs')
.setCharacteristic(Characteristic.Model, 'Pi Garage Door Opener')
.setCharacteristic(Characteristic.SerialNumber, 'Raspberry Pi');
this.service = new Service.GarageDoorOpener(this.name, this.name);
this.service.setCharacteristic(TargetDoorState, TargetDoorState.CLOSED);
this.service.setCharacteristic(CurrentDoorState, CurrentDoorState.CLOSED);
this.service.getCharacteristic(TargetDoorState)
.on('get', (callback) => {
callback(null, this.targetDoorState);
})
.on('set', (value, callback) => {
this.targetDoorState = value;
if (this.targetDoorState === TargetDoorState.OPEN) {
if (this.currentDoorState === CurrentDoorState.CLOSED) {
this.openCloseGarage(() =>
this.service.setCharacteristic(CurrentDoorState, CurrentDoorState.OPENING));
} else if (this.currentDoorState === CurrentDoorState.OPENING) {
// Do nothing
} else if (this.currentDoorState === CurrentDoorState.CLOSING) {
this.openCloseGarage(() =>
this.service.setCharacteristic(CurrentDoorState, CurrentDoorState.OPENING));
} else if (this.currentDoorState === CurrentDoorState.OPEN) {
// Do nothing
}
} else if (this.targetDoorState === TargetDoorState.CLOSED) {
if (this.currentDoorState === CurrentDoorState.CLOSED) {
// Do nothing
} else if (this.currentDoorState === CurrentDoorState.OPENING) {
this.openCloseGarage(() =>
this.openCloseGarage(() =>
this.service.setCharacteristic(CurrentDoorState, CurrentDoorState.CLOSING)));
// Do nothing
} else if (this.currentDoorState === CurrentDoorState.CLOSING) {
// Do nothing
} else if (this.currentDoorState === CurrentDoorState.OPEN) {
this.openCloseGarage(() =>
this.service.setCharacteristic(CurrentDoorState, CurrentDoorState.CLOSING));
}
}
callback();
});
this.service.getCharacteristic(CurrentDoorState)
.on('get', (callback) => {
callback(null, this.currentDoorState);
})
.on('set', (value, callback) => {
this.currentDoorState = value;
this.log('current', this.currentDoorState);
if (this.currentDoorState === CurrentDoorState.OPENING) {
clearTimeout(this.openCloseTimer);
this.doorOpenStartTime = new Date();
const timeSinceDoorStartedClosing = new Date() - this.doorCloseStartTime;
let stateChangeTimer = this.openCloseTime;
if (timeSinceDoorStartedClosing < this.openCloseTime) {
stateChangeTimer = timeSinceDoorStartedClosing;
}
this.openCloseTimer = setTimeout(() => {
this.service.setCharacteristic(CurrentDoorState, CurrentDoorState.OPEN);
}, stateChangeTimer);
} else if (this.currentDoorState === CurrentDoorState.CLOSING) {
clearTimeout(this.openCloseTimer);
this.doorCloseStartTime = new Date();
const timeSinceDoorStartedOpening = new Date() - this.doorOpenStartTime;
let stateChangeTimer = this.openCloseTime;
if (timeSinceDoorStartedOpening < this.openCloseTime) {
stateChangeTimer = timeSinceDoorStartedOpening;
}
this.openCloseTimer = setTimeout(() => {
this.service.setCharacteristic(CurrentDoorState, CurrentDoorState.CLOSED);
}, stateChangeTimer);
}
callback();
});
this.service
.getCharacteristic(Characteristic.Name)
.on('get', callback => {
callback(null, this.name);
});
return [informationService, this.service];
}
}