@raiden733/homebridge-gpio-garagedoor
Version:
Raspberry Pi GPIO based Garage Door plugin for Homebridge
73 lines (72 loc) • 2.86 kB
JavaScript
;
/**
* Created by kraig on 7/2/16.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SwitchPort = void 0;
const GPIOPort_1 = require("./GPIOPort");
const DoorStateExtension_1 = require("./DoorStateExtension");
var Characteristic;
class SwitchPort extends GPIOPort_1.GPIOPort {
static init(exportTypes) {
Characteristic = exportTypes.Characteristic;
}
constructor(pin, service, log, doorSensor, doorOpensInSeconds) {
super(pin, 'out', 'none', { activeLow: true });
this.writeAsync(GPIOPort_1.GPIOState.Off);
this.service = service;
this.log = log;
this.isOperating = false;
var self = this;
var targetState = service.getCharacteristic(Characteristic.TargetDoorState);
targetState.on('set', function (state, callback) {
var curState = (0, DoorStateExtension_1.getCurrentDoorState)(service);
switch (curState) {
case Characteristic.CurrentDoorState.OPENING:
case Characteristic.CurrentDoorState.CLOSING:
callback(new Error('Must wait until operation is finished'));
return;
default:
// If the target state is equal to current state, do nothing.
if ((0, DoorStateExtension_1.asDoorState)(state) == curState) {
callback();
return;
}
break;
}
self.isOperating = true;
self.log.debug("Started operation");
self.writeAsync(GPIOPort_1.GPIOState.On)
.then(function () {
service.setCharacteristic(Characteristic.CurrentDoorState, (0, DoorStateExtension_1.asOperationState)(state));
})
.asCallback(callback)
.delay(1000)
.then(function () {
return self.writeAsync(GPIOPort_1.GPIOState.Off);
})
.delay(doorOpensInSeconds * 1000)
.catch(function (err) {
self.log.error(err);
})
.finally(function () {
self.isOperating = false;
self.log.debug("Finished operation");
doorSensor.reset();
self.refresh();
//TODO: log issues
});
});
this.refresh();
}
refresh() {
if (this.isOperating)
return;
this.service.getCharacteristic(Characteristic.TargetDoorState)
.setValue((0, DoorStateExtension_1.getCurrentDoorState)(this.service) == Characteristic.CurrentDoorState.OPEN ?
Characteristic.TargetDoorState.OPEN :
Characteristic.TargetDoorState.CLOSED);
}
;
}
exports.SwitchPort = SwitchPort;