Skip to content

Commit

Permalink
Added Persist State between HomeBridge restarts #3
Browse files Browse the repository at this point in the history
  • Loading branch information
rasod committed Aug 11, 2020
1 parent bf7ba5e commit 124acec
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 39 deletions.
93 changes: 55 additions & 38 deletions 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);
}

Expand All @@ -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;
Expand All @@ -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();
}
});
}
}
71 changes: 71 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -22,7 +22,9 @@
"node": ">=6.4.0",
"homebridge": ">=0.3.0"
},
"dependencies": {},
"dependencies": {
"node-persist": "^2.1.0"
},
"scripts": {},
"main": "index.js"
}

0 comments on commit 124acec

Please sign in to comment.