-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
291 lines (228 loc) · 9.35 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
let {PythonShell} = require('python-shell')
let PlatformAccessory, Accessory, hap, Service, Characteristic, UUIDGen;
let pyshell = null;
const PLUGIN_NAME = "homebridge-intercom-automation-hat";
const PLATFORM_NAME = "IntercomAutomationHAT";
module.exports = function (homebridge) {
PlatformAccessory = homebridge.platformAccessory;
Accessory = homebridge.hap.Accessory;
hap = homebridge.hap;
Service = hap.Service;
Characteristic = hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
homebridge.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, IntercomPlatform);
homebridge.registerAccessory(PLUGIN_NAME, PLATFORM_NAME + "LockAccessory", IntercomLockAccessory);
homebridge.registerAccessory(PLUGIN_NAME, PLATFORM_NAME + "BellAccessory", IntercomDoorbellAccessory);
}
function IntercomPlatform(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.log("IntercomPlatform constructor");
//device configuration
this.bellTimeout = this.config['bellTimeout'] || 1000; // milliseconds - 1 sec
this.lockTimeout = this.config['lockTimeout'] || 1000; // milliseconds - 1 sec
this.voltageLowLimit = this.config['voltageLowLimit'] || 0.03; // V
this.name = this.config['name'] || 'Intercom';
//setup variables
var scriptPath = __dirname + "/";
//get Device info
this.manufacturer = 'Raspberry Pi';
this.modelName = 'Zero W';
this.serialNumber = 'SN000001';
this.firmwareRevision = 'FW000001';
this.bellRang = false;
// Prepare to kill pyshell
process.on('SIGINT', () => { this.shutdown('SIGINT') })
process.on('SIGTERM', () => { this.shutdown('SIGTERM') })
this.log("Setting up pyhton shell. scriptPath = " + scriptPath);
// Create pyshell
pyshell = new PythonShell('intercom-automation-hat.py', {
mode: 'text',
pythonPath: '/usr/bin/python3',
pythonOptions: ['-u'], // get print results in real-time
args: [this.voltageLowLimit],
scriptPath: scriptPath//'../../homebridge-intercom-automation-hat'
});
pyshell.on('stderr', function (stderr) {
// handle stderr (a line of text from stderr)
this.log(stderr);
}.bind(this));
pyshell.on('close', function (stderr) {
// handle stderr (a line of text from stderr)
this.log("pyshell close:");
this.log(" " + stderr);
}.bind(this));
pyshell.on('error', function (stderr) {
// handle stderr (a line of text from stderr)
this.log("pyshell error:");
this.log(" " + stderr);
}.bind(this));
//this.pyshell.on('message', function (message) {
// // received a message sent from the Python script (a simple "print" statement)
// console.log(message);
//});
}
IntercomPlatform.prototype = {
shutdown: function(signal) {
this.log('Got %s, shutting down', signal)
pyshell.terminate(signal)
setImmediate(() => { process.exit(0) })
},
lock: function() {
this.log('Send lock to pyshell');
pyshell.send('lock');
},
unlock: function() {
this.log('Send unlock to pyshell');
pyshell.send('unlock');
},
listenDoorbell: function(doorbellOnCallback, doorbellOffCallback) {
pyshell.on('message', function (message) {
this.log(message);
switch(message) {
case 'doorbell on':
if(!this.bellRang) doorbellOnCallback();
this.bellRang = true;
break;
case 'doorbell off':
if(this.bellRang) {
var that = this;
setTimeout(function() {
that.bellRang = false;
doorbellOffCallback();
}, this.bellTimeout);
}
break;
}
}.bind(this));
},
accessories: function(callback) {
this.log("Creating IntercomPlatform accessories...");
this.accessories = [];
// Lock
var lockAccessory = new IntercomLockAccessory(this.log, this.config);
lockAccessory.prepareLockAccessory();
var lockService = lockAccessory.prepareLockService();
lockAccessory.connectLockUnlock(this.lock, this.unlock);
// Door
var doorbellAccessory = new IntercomDoorbellAccessory(this.log, this.config);
doorbellAccessory.prepareDoorbellAccessory();
var doorbellService = doorbellAccessory.prepareDoorbellService();
doorbellAccessory.connectListingToDoorbel.call(this, this.listenDoorbell, doorbellService);
this.accessories.push(lockAccessory);
this.accessories.push(doorbellAccessory);
callback(this.accessories);
}
};
class IntercomLockAccessory {
constructor(log, config) {
this.log = log;
this.config = config;
this.lockTimeout = this.config['lockTimeout'] || 1000; // milliseconds - 1 sec
this.name = this.config['name'] || 'Intercom';
this.name += "Lock";
this.log("IntercomLockAccessory constructor");
}
prepareLockAccessory() {
this.log('prepareLockAccessory');
let uuid = UUIDGen.generate(this.name + "LockAccessory");
this.intercomLockAccessory = new PlatformAccessory(this.name, uuid, Accessory.Categories.LOCK);
this.intercomLockAccessory.on('identify', function(callback){
this.identify.call(this,callback);
})
}
prepareLockService(lock, unlock) {
this.log('prepareLockService');
this.lockService = new Service.LockMechanism(this.name, 'Door lock');
this.lockService.setCharacteristic(Characteristic.LockTargetState, Characteristic.LockTargetState.SECURED) // force initial state
.setCharacteristic(Characteristic.LockCurrentState, Characteristic.LockCurrentState.SECURED)
.getCharacteristic(Characteristic.LockTargetState)
.on('set', this.setDoorTargetState.bind(this));
this.intercomLockAccessory.addService(this.lockService);
return this.lockService;
}
connectLockUnlock(lock, unlock) {
this.lock = lock;
this.unlock = unlock;
}
setDoorTargetState(state, callback) {
switch(state) {
case Characteristic.LockTargetState.UNSECURED:
this.unlockDoor();
break;
case Characteristic.LockTargetState.SECURED:
this.lockDoor();
break;
}
callback();
}
unlockDoor() {
this.unlock();
this.lockService.getCharacteristic(Characteristic.LockCurrentState)
.updateValue(Characteristic.LockCurrentState.UNSECURED);
var that = this;
setTimeout(function() {
that.log("unlock timeout door");
that.lock(); // just for safety...
that.lockService.getCharacteristic(Characteristic.LockTargetState)
.setValue(Characteristic.LockTargetState.SECURED);
}, this.lockTimeout);
}
lockDoor() {
this.lock();
this.lockService.getCharacteristic(Characteristic.LockCurrentState)
.updateValue(Characteristic.LockCurrentState.SECURED);
}
identify(callback) {
this.log('identify intercom lock');
callback();
}
// Get Services
getServices() {
return [this.lockService];
}
}
class IntercomDoorbellAccessory {
constructor(log, config) {
this.log = log;
this.config = config;
this.name = this.config['name'] || 'Intercom';
this.name += "Bell";
this.log("IntercomBellAccessory constructor");
}
prepareDoorbellAccessory() {
this.log('prepareBellAccessory');
let uuid = UUIDGen.generate(this.name + "BellAccessory");
this.intercomDoorbellAccessory = new PlatformAccessory(this.name, uuid, Accessory.Categories.VIDEO_DOORBELL);
this.intercomDoorbellAccessory.on('identify', function(callback){
this.identify.call(this,callback);
})
}
prepareDoorbellService() {
this.log('prepareDoorbellService');
this.doorbellService = new Service.Doorbell(this.name, 'Doorbell');
//this.doorbellService.setCharacteristic(Characteristic.ProgrammableSwitchEvent, Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS);
this.intercomDoorbellAccessory.addService(this.doorbellService);
return this.doorbellService;
}
connectListingToDoorbel(listenDoorbell, doorbellService) {
this.log("connectListingToDoorbel");
this.listenDoorbell = listenDoorbell;
this.listenDoorbell(
function() {
doorbellService.getCharacteristic(Characteristic.ProgrammableSwitchEvent)
.updateValue(Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS);
},
function() {});
}
identify(callback) {
this.log('identify intercom doorbell');
this.doorbellService.getCharacteristic(Characteristic.ProgrammableSwitchEvent).setValue(0);
callback();
}
// Get Services
getServices() {
return [this.doorbellService];
}
}