-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
88 lines (76 loc) · 3.37 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
var Service, Characteristic, HomebridgeAPI;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
HomebridgeAPI = homebridge;
homebridge.registerAccessory('homebridge-dummy-garage', 'DummyGarage', DummyGarage);
}
class DummyGarage {
constructor (log, config) {
//get config values
this.name = config['name'] || "Dummy Garage";
this.autoCloseDelay = config["autoCloseDelay"] === undefined ? 0 : Number(config["autoCloseDelay"]);
//persist storage
this.cacheDirectory = HomebridgeAPI.user.persistPath();
this.storage = require('node-persist');
this.storage.initSync({dir:this.cacheDirectory, forgiveParseErrors: true});
this.cachedState = this.storage.getItemSync(this.name);
//initial setup
this.log = log;
this.lastOpened = new Date();
this.service = new Service.GarageDoorOpener(this.name, this.name);
this.setupGarageDoorOpenerService(this.service);
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, 'github/rasod')
.setCharacteristic(Characteristic.Model, 'Dummy Garage')
.setCharacteristic(Characteristic.FirmwareRevision, '1.2.2')
.setCharacteristic(Characteristic.SerialNumber, this.name.replace(/\s/g, '').toUpperCase());
}
getServices () {
return [this.informationService, this.service];
}
setupGarageDoorOpenerService (service) {
this.log.debug("setupGarageDoorOpenerService");
this.log.debug("Cached State: " + this.cachedState);
if((this.cachedState === undefined) || (this.cachedState === true)) {
this.log.debug("Using Saved OPEN State");
this.service.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.OPEN);
} else {
this.log.debug("Using Default CLOSED State");
this.service.setCharacteristic(Characteristic.TargetDoorState, Characteristic.TargetDoorState.CLOSED);
this.service.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.CLOSED);
}
service.getCharacteristic(Characteristic.TargetDoorState)
.on('get', (callback) => {
var targetDoorState = service.getCharacteristic(Characteristic.TargetDoorState).value;
callback(null, targetDoorState);
})
.on('set', (value, callback) => {
if (value === Characteristic.TargetDoorState.OPEN) {
this.log("Opening: " + this.name)
this.lastOpened = new Date();
this.service.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.OPEN);
this.storage.setItem(this.name, true);
this.log.debug("autoCloseDelay = " + this.autoCloseDelay);
if (this.autoCloseDelay > 0) {
this.log("Closing in " + this.autoCloseDelay + " seconds.");
setTimeout(() => {
this.log("Auto Closing");
this.service.setCharacteristic(Characteristic.TargetDoorState, Characteristic.TargetDoorState.CLOSED);
this.service.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.CLOSED);
this.storage.setItem(this.name, false);
}, this.autoCloseDelay * 1000);
}
callback();
} else if (value === Characteristic.TargetDoorState.CLOSED) {
this.log("Closing: " + this.name)
this.service.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.CLOSED);
this.storage.setItem(this.name, false);
callback();
} else {
callback();
}
});
}
}