-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
73 lines (57 loc) · 2.07 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
var Service, Characteristic
module.exports = (homebridge) => {
Service = homebridge.hap.Service
Characteristic = homebridge.hap.Characteristic
homebridge.registerAccessory('homebridge-dummy-lock', 'DummyLock', DummyLock)
}
class DummyLock {
constructor (log, config) {
// get config values
this.log = log;
this.name = config['name'];
this.lockService = new Service.LockMechanism(this.name);
this.lockState = Characteristic.LockCurrentState.SECURED;
}
getServices () {
const informationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, 'Acme')
.setCharacteristic(Characteristic.Model, 'Dummy Lock 1.0')
.setCharacteristic(Characteristic.SerialNumber, '1234');
this.lockService.getCharacteristic(Characteristic.LockCurrentState)
.on('get', this.getLockCharacteristicHandler.bind(this));
this.lockService.getCharacteristic(Characteristic.LockTargetState)
.on('get', this.getLockCharacteristicHandler.bind(this))
.on('set', this.setLockCharacteristicHandler.bind(this));
return [informationService, this.lockService]
}
actionCallback(err, result) {
if(err) {
this.updateCurrentState(Characteristic.LockCurrentState.JAMMED);
return console.error(err);
}
}
// Lock Handler
setLockCharacteristicHandler (targetState, callback) {
var lockh = this;
if (targetState == Characteristic.LockCurrentState.SECURED) {
this.log(`locking `+this.name, targetState)
this.lockState = targetState
this.updateCurrentState(this.lockState);
this.log(this.lockState+" "+this.name);
} else {
this.log(`unlocking `+this.name, targetState)
this.lockState = targetState
this.updateCurrentState(this.lockState);
this.log(this.lockState+" "+this.name);
}
callback();
}
updateCurrentState(toState) {
this.lockService
.getCharacteristic(Characteristic.LockCurrentState)
.updateValue(toState);
}
getLockCharacteristicHandler (callback) {
callback(null, this.lockState);
}
}