diff --git a/index.js b/index.js index 314af21..8a2a900 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,9 @@ -var Service, Characteristic; +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); } @@ -13,6 +13,12 @@ class DummyGarage { //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; @@ -22,50 +28,61 @@ class DummyGarage { this.informationService = new Service.AccessoryInformation(); this.informationService - .setCharacteristic(Characteristic.Manufacturer, 'rasod') + .setCharacteristic(Characteristic.Manufacturer, 'github/rasod') .setCharacteristic(Characteristic.Model, 'Dummy Garage') .setCharacteristic(Characteristic.FirmwareRevision, '1.1') .setCharacteristic(Characteristic.SerialNumber, this.name.replace(/\s/g, '').toUpperCase()); +} - } - - getServices () { - return [this.informationService, this.service]; - } +getServices () { + return [this.informationService, this.service]; +} - setupGarageDoorOpenerService (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.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.autoCloseDelay * 1000); - } - - callback(); - - } else if (value === Characteristic.TargetDoorState.CLOSED) { - this.log("Closing: " + this.name) - this.service.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.CLOSED); - callback(); - } else { - callback(); + 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.setItemSync(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.setItemSync(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.setItemSync(this.name, false); + callback(); + } else { + callback(); + } + }); } } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..c8e38a5 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,71 @@ +{ + "name": "homebridge-dummy-garage", + "version": "1.1.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "is-absolute": { + "version": "0.2.6", + "resolved": "https://registry.npmjs.org/is-absolute/-/is-absolute-0.2.6.tgz", + "integrity": "sha1-IN5p89uULvLYe5wto28XIjWxtes=", + "requires": { + "is-relative": "^0.2.1", + "is-windows": "^0.2.0" + } + }, + "is-relative": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/is-relative/-/is-relative-0.2.1.tgz", + "integrity": "sha1-0n9MfVFtF1+2ENuEu+7yPDvJeqU=", + "requires": { + "is-unc-path": "^0.1.1" + } + }, + "is-unc-path": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/is-unc-path/-/is-unc-path-0.1.2.tgz", + "integrity": "sha1-arBTpyVzwQJQ/0FqOBTDUXivObk=", + "requires": { + "unc-path-regex": "^0.1.0" + } + }, + "is-windows": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/is-windows/-/is-windows-0.2.0.tgz", + "integrity": "sha1-3hqm1j6indJIc3tp8f+LgALSEIw=" + }, + "minimist": { + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", + "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" + }, + "mkdirp": { + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz", + "integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==", + "requires": { + "minimist": "^1.2.5" + } + }, + "node-persist": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/node-persist/-/node-persist-2.1.0.tgz", + "integrity": "sha1-5lK784haBNrWo1PXQXYXfIORRwc=", + "requires": { + "is-absolute": "^0.2.6", + "mkdirp": "~0.5.1", + "q": "~1.1.1" + } + }, + "q": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/q/-/q-1.1.2.tgz", + "integrity": "sha1-Y1fikSBnAdmfGXq4TlforRlvKok=" + }, + "unc-path-regex": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/unc-path-regex/-/unc-path-regex-0.1.2.tgz", + "integrity": "sha1-5z3T17DXxe2G+6xrCufYxqadUPo=" + } + } +} diff --git a/package.json b/package.json index 1229fc1..22770f7 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,9 @@ "node": ">=6.4.0", "homebridge": ">=0.3.0" }, - "dependencies": {}, + "dependencies": { + "node-persist": "^2.1.0" + }, "scripts": {}, "main": "index.js" }