-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
168 lines (148 loc) · 5.53 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
var Service;
var Characteristic;
var exec = require('child_process').exec;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-garagedoor-command', 'GarageCommand', GarageCmdAccessory);
};
function GarageCmdAccessory(log, config) {
this.log = log;
this.name = config.name;
this.openCommand = config.open;
this.closeCommand = config.close;
this.stateCommand = config.state;
this.statusUpdateDelay = config.status_update_delay || 15;
this.pollStateDelay = config.poll_state_delay || 0;
this.ignoreErrors = config.ignore_errors || false;
this.logPolling = config.log_polling || false;
this.manufacturer = config.manufacturer || 'Apexad';
this.model = config.model || 'Garage Command';
this.serialNum = config.serialNum || '001';
}
GarageCmdAccessory.prototype.setState = function(isClosed, callback, context) {
if (context === 'pollState') {
// The state has been updated by the pollState command - don't run the open/close command
callback(null);
return;
}
var accessory = this;
var state = isClosed ? 'close' : 'open';
var prop = state + 'Command';
var command = accessory[prop];
accessory.log('Commnand to run: ' + command);
exec(
command,
{
encoding: 'utf8',
timeout: 10000,
maxBuffer: 200*1024,
killSignal: 'SIGTERM',
cwd: null,
env: null
},
function (err, stdout, stderr) {
if (err) {
accessory.log('Error: ' + err + ', command output: ' + stderr);
callback(err || new Error('Error setting ' + accessory.name + ' to ' + state));
} else {
accessory.log('Set ' + accessory.name + ' to ' + state);
if (stdout.indexOf('OPENING') > -1) {
accessory.garageDoorService.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.OPENING);
setTimeout(
function() {
accessory.garageDoorService.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.OPEN);
},
accessory.statusUpdateDelay * 1000
);
} else if (stdout.indexOf('CLOSING') > -1) {
accessory.garageDoorService.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.CLOSING);
setTimeout(
function() {
accessory.garageDoorService.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.CLOSED);
},
accessory.statusUpdateDelay * 1000
);
}
callback(null);
}
});
};
GarageCmdAccessory.prototype.getState = function(callback) {
var accessory = this;
var command = accessory.stateCommand;
exec(
command,
{
encoding: 'utf8',
timeout: 10000,
maxBuffer: 200*1024,
killSignal: 'SIGTERM',
cwd: null,
env: null
},
function (err, stdout, stderr) {
if (err) {
accessory.log('Error: ' + err + ', command output: ' + stderr);
callback(err || new Error('Error getting state of ' + accessory.name));
} else {
var state = 'OPEN';
if ((stdout.indexOf('STOPPED') > -1 && accessory.ignoreErrors) || (stdout.indexOf('CLOSED') > -1)) {
state = 'CLOSED';
} else if (stdout.indexOf('STOPPED') > -1 && !accessory.ignoreErrors) {
state = 'STOPPED';
} else if (stdout.indexOf('CLOSING') > -1) {
state = 'CLOSING';
} else if (stdout.indexOf('OPENING') > -1) {
state = 'OPENING';
}
if (accessory.logPolling) {
accessory.log('State of ' + accessory.name + ' is: ' + state);
}
callback(null, Characteristic.CurrentDoorState[state]);
}
if (accessory.pollStateDelay > 0) {
accessory.pollState();
}
});
};
GarageCmdAccessory.prototype.pollState = function() {
var accessory = this;
// Clear any existing timer
if (accessory.stateTimer) {
clearTimeout(accessory.stateTimer);
accessory.stateTimer = null;
}
accessory.stateTimer = setTimeout(
function() {
accessory.getState(function(err, currentDeviceState) {
if (err) {
accessory.log(err);
return;
}
if (currentDeviceState === Characteristic.CurrentDoorState.OPEN || currentDeviceState === Characteristic.CurrentDoorState.CLOSED) {
// Set the target state to match the actual state
// If this isn't done the Home app will show the door in the wrong transitioning state (opening/closing)
accessory.garageDoorService.getCharacteristic(Characteristic.TargetDoorState)
.setValue(currentDeviceState, null, 'pollState');
}
accessory.garageDoorService.setCharacteristic(Characteristic.CurrentDoorState, currentDeviceState);
})
},
accessory.pollStateDelay * 1000
);
}
GarageCmdAccessory.prototype.getServices = function() {
this.informationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serialNum);
this.garageDoorService = new Service.GarageDoorOpener(this.name);
this.garageDoorService.getCharacteristic(Characteristic.TargetDoorState)
.on('set', this.setState.bind(this));
if (this.stateCommand) {
this.garageDoorService.getCharacteristic(Characteristic.CurrentDoorState)
.on('get', this.getState.bind(this));
}
return [this.informationService, this.garageDoorService];
};