homebridge-garagedoor-uno
Version:
Garage Door open plugin for Homebridge
161 lines (140 loc) • 5.02 kB
JavaScript
const Gpio = require('onoff').Gpio;
var Service, Characteristic;
var popen = 0;
module.exports = (api) => {
Service = api.hap.Service;
Characteristic = api.hap.Characteristic;
api.registerAccessory('GarageDooronof', GarageDooronof);
};
class GarageDooronof {
constructor(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
// extract name from config
this.name = config.name;
this.PinOpen = config.PinOpen;
this.PinStart = config.PinStart;
this.TimeOpen = config.TimeOpen;
this.TimeOpenSw = config.TimeOpenSw;
popen = new Gpio(this.PinOpen, 'out');
// create a new Garage Door Opener service
this.lastOpened = new Date();
this.service = new Service.GarageDoorOpener(this.name);
this.setupGarageDoorOpenerService(this.service);
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, 'Stefano Ghidini')
.setCharacteristic(Characteristic.Model, 'S400')
.setCharacteristic(Characteristic.SerialNumber, '0111');
}
getServices () {
return [this.informationService, this.service];
}
setupGarageDoorOpenerService (service) {
const sensor = new Gpio(19, 'in', 'both');
sensor.watch(function (err, value) {
if (err) {
log.error(err);
return;
}
console.log(err);
console.log(value);
if (value) {
popen.writeSync(1);
this.service.setCharacteristic(Characteristic.TargetDoorState, Characteristic.TargetDoorState.CLOSED);//OPEN
this.service.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.CLOSED);//OPEN
} else {
popen.writeSync(1);
this.service.setCharacteristic(Characteristic.TargetDoorState, Characteristic.TargetDoorState.OPEN);//CLOSED
this.service.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.OPEN);//CLOSED
}
});
service.getCharacteristic(Characteristic.TargetDoorState)
.on('get', (callback) => {
var targetDoorState = service.getCharacteristic(Characteristic.TargetDoorState).value;
if (targetDoorState === Characteristic.TargetDoorState.OPEN && ((new Date() - this.lastOpened) >= (this.closeAfter * 1000))) {
this.log('Setting TargetDoorState -> CLOSED');
callback(null, Characteristic.TargetDoorState.CLOSED);
}else if (targetDoorState === Characteristic.TargetDoorState.CLOSED && ((new Date() - this.lastOpened) >= (this.openAfter * 1000))) {
this.log('Setting TargetDoorState -> OPEN');
callback(null, Characteristic.TargetDoorState.OPEN);
} else {
callback(null, targetDoorState);
}
})
.on('set', (value, callback) => {
if (value === Characteristic.TargetDoorState.OPEN) {
this.lastOpened = new Date();
switch (service.getCharacteristic(Characteristic.CurrentDoorState).value) {
case Characteristic.CurrentDoorState.CLOSED:
case Characteristic.CurrentDoorState.CLOSING:
case Characteristic.CurrentDoorState.OPEN:
this.openGarageDoor(callback);
break;
default:
callback();
}
}else if (value === Characteristic.TargetDoorState.CLOSED) {
this.lastOpened = new Date();
switch (service.getCharacteristic(Characteristic.CurrentDoorState).value) {
case Characteristic.CurrentDoorState.OPEN:
case Characteristic.CurrentDoorState.CLOSING:
case Characteristic.CurrentDoorState.CLOSED:
this.closeGarageDoor(callback);
break;
default:
callback();
}
} else {
callback();
}
});
}
openGarageDoor (callback) {
if (this.PinStart) {
popen.writeSync(0);
setTimeout(function(){
popen.writeSync(1);
}, this.TimeOpenSw);
}else{
popen.writeSync(1);
setTimeout(function(){
popen.writeSync(0);
}, this.TimeOpenSw);
}
this.log('Opening the garage door for...');
this.simulateGarageDoorOpening();
callback();
}
simulateGarageDoorOpening () {
this.service.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.OPENING);
setTimeout(() => {
this.service.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.OPEN);
}, this.TimeOpen * 100);
}
closeGarageDoor (callback) {
if (this.PinStart) {
popen.writeSync(0);
setTimeout(function(){
popen.writeSync(1);
}, this.TimeOpenSw);
}else{
popen.writeSync(1);
setTimeout(function(){
popen.writeSync(0);
}, this.TimeOpenSw);
}
this.log('Closing the garage door for...');
this.simulateGarageDoorclosening();
callback();
}
simulateGarageDoorclosening () {
this.service.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.CLOSING);
setTimeout(() => {
this.service.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.CLOSED);
}, this.TimeOpen * 100);
}
}