forked from DualBit/homebridge-sonoff-garage-opener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
172 lines (154 loc) · 6.64 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
var request = require('request');
let Service, Characteristic, TargetDoorState, CurrentDoorState;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
TargetDoorState = Characteristic.TargetDoorState;
CurrentDoorState = Characteristic.CurrentDoorState;
homebridge.registerAccessory('homebridge-sonoff-garage-opener', 'Sonoff Garage Door Opener', GarageDoorOpener);
};
class GarageDoorOpener {
constructor(log, config) {
this.log = log;
this.name = config.name;
this.ip = config.ip;
this.openCloseTime = config.openCloseTime || 0;
this.openingTime = config.openingTime || this.openCloseTime;
this.closureTime = config.closureTime || this.openingTime;
this.doorRelayPin = config.doorRelayPin;
this.timeBeforeClosure = config.timeBeforeClosure || 0;
this.currentDoorState = CurrentDoorState.CLOSED;
this.targetDoorState = TargetDoorState.CLOSED;
}
identify(callback) {
this.log('Identify requested!');
callback(null);
}
openCloseGarage(callback) {
request.get({
url: 'http://' + this.ip + '/cm?user=admin&password=' + this.password + '&cmnd=Power' + this.doorRelayPin,
timeout: 120000
}, (error, response, body) => {
this.log.debug('openCloseGarage', response.statusCode, body);
if (!error && response.statusCode == 200) {
//this.log.debug('Response: %s', body);
callback();
}
//this.log.debug('Error setting door state. (%s)', error);
});
}
getServices() {
const informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, 'Dualbit')
.setCharacteristic(Characteristic.Model, 'Sonoff Garage Door Opener')
.setCharacteristic(Characteristic.SerialNumber, '0xl33t');
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;
clearTimeout(this.timerBeforeClosure);
if (this.targetDoorState === TargetDoorState.OPEN) {
// want to 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 status: ', this.doorStateToString(this.currentDoorState));
if (this.currentDoorState === CurrentDoorState.OPENING) {
clearTimeout(this.openCloseTimer);
this.doorOpenStartTime = new Date();
const timeSinceDoorStartedClosing = new Date() - this.doorCloseStartTime;
let openingTimer = this.openCloseTime != 0 ? this.openCloseTime : this.openingTime;
let stateChangeTimer = openingTimer;
if (timeSinceDoorStartedClosing < openingTimer) {
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 closureTimer = this.openCloseTime != 0 ? this.openCloseTime : this.closureTime;
let stateChangeTimer = closureTimer;
if (timeSinceDoorStartedOpening < closureTimer) {
stateChangeTimer = timeSinceDoorStartedOpening;
}
this.openCloseTimer = setTimeout(() => {
this.service.setCharacteristic(CurrentDoorState, CurrentDoorState.CLOSED);
}, stateChangeTimer);
} else if (this.currentDoorState === CurrentDoorState.OPEN) {
if (this.timeBeforeClosure != 0) {
this.log.debug('AUTOCLOSING in ' + this.timeBeforeClosure / 1000 + ' SECONDS');
this.timerBeforeClosure = setTimeout(() => {
this.targetDoorState = TargetDoorState.CLOSED;
this.openCloseGarage(() =>
this.service.setCharacteristic(TargetDoorState, TargetDoorState.CLOSED));
}, this.timeBeforeClosure);
} else {
this.service.setCharacteristic(TargetDoorState, TargetDoorState.CLOSED);
}
}
callback();
});
this.service
.getCharacteristic(Characteristic.Name)
.on('get', callback => {
callback(null, this.name);
});
return [informationService, this.service];
}
doorStateToString(state) {
switch (state) {
case CurrentDoorState.OPEN:
return 'OPEN';
case CurrentDoorState.CLOSED:
return 'CLOSED';
case CurrentDoorState.STOPPED:
return 'STOPPED';
case CurrentDoorState.OPENING:
return 'OPENING';
case CurrentDoorState.CLOSING:
return 'CLOSING';
default:
return 'UNKNOWN';
}
}
}