homebridge-garagedoor-uno
Version:
Garage Door open plugin for Homebridge
132 lines (114 loc) • 4.38 kB
JavaScript
const Gpio = require('onoff').Gpio;
const request = require('request');
var pin = 0;
var Service, Characteristic;
module.exports = (homebridge) => {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('Homebridge-GarageDoor-onoff', 'GarageDooronof', GarageDooronof);
};
class GarageDooronof {
constructor (log, config) {
//get config values
this.name = config['name'];
this.doorSwitchPin = config['doorSwitchPin'] || 5;
this.statoip = config['statoip'];
this.statoname = config['statoname'];
this.doorSwitchPressTimeInMs = config['doorSwitchPressTimeInMs'] || 1000;
this.doorSwitchActiveLow = config['doorSwitchActiveLow'] || 0;
pin = new Gpio(this.doorSwitchPin, 'out');
//initial setup
this.log = log;
this.lastOpened = new Date();
this.service = new Service.GarageDoorOpener(this.name, this.name);
this.setupGarageDoorOpenerService(this.service);
this.setstatus();
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, 'Garage Door ONOFF')
.setCharacteristic(Characteristic.Model, 'TOPMAX200')
.setCharacteristic(Characteristic.SerialNumber, '0711');
}
getServices () {
return [this.informationService, this.service];
}
setupGarageDoorOpenerService (service) {
if (this.doorSwitchActiveLow) {
pin.writeSync(1);
} else {
pin.writeSync(0);
}
request('http://'+this.statoip+'/'+this.statoname, { json: true }, (err, res, body) => {
if (err) {
this.service.setCharacteristic(Characteristic.TargetDoorState, Characteristic.TargetDoorState.CLOSED);
this.service.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.CLOSED);
}
if(body==1){
this.service.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.OPEN);
}else{
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;
if (targetDoorState === Characteristic.TargetDoorState.OPEN && ((new Date() - this.lastOpened) >= (this.closeAfter * 1000))) {
this.log('Setting TargetDoorState -> CLOSED');
callback(null, Characteristic.TargetDoorState.CLOSED);
} 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 {
callback();
}
});
}
openGarageDoor (callback) {
let pin2 = new Gpio(this.doorSwitchPin, 'out');
if (this.doorSwitchActiveLow) {
pin2.writeSync(0);
let t1 = function(){
pin2.writeSync(1);
}
setTimeout(t1, this.doorSwitchPressTimeInMs);
} else {
pin2.writeSync(1);
let t2 = function(){
pin2.writeSync(0);
}
setTimeout(t2, this.doorSwitchPressTimeInMs);
}
this.log('Opening the garage door for...');
this.setstatus();
callback();
}
setstatus () {
request('http://'+this.statoip+'/'+this.statoname, { json: true }, (err, res, body) => {
if (err) {
this.service.setCharacteristic(Characteristic.TargetDoorState, Characteristic.TargetDoorState.CLOSED);
this.service.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.CLOSED);
}
//console.log(body.url);
if(body==1){
this.service.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.OPEN);
}else{
this.service.setCharacteristic(Characteristic.TargetDoorState, Characteristic.TargetDoorState.CLOSED);
this.service.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.CLOSED);
}
});
}
}