UNPKG

homebridge-gpio-sensor-door

Version:

Raspberry Pi GPIO Doorsensor plugin for Homebridge forked from homebridge-gpio-garagedoor-d

66 lines 2.58 kB
"use strict"; const GPIOPort_1 = require("./GPIOPort"); const DoorStateExtension_1 = require("./DoorStateExtension"); var Characteristic; class SwitchPort extends GPIOPort_1.GPIOPort { constructor(pin, service, log, doorSensor, doorOpensInSeconds) { super(pin, 'out'); 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 = 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 (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, 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(); } static init(exportTypes) { Characteristic = exportTypes.Characteristic; } refresh() { if (this.isOperating) return; this.service.getCharacteristic(Characteristic.TargetDoorState) .setValue(DoorStateExtension_1.getCurrentDoorState(this.service) == Characteristic.CurrentDoorState.OPEN ? Characteristic.TargetDoorState.OPEN : Characteristic.TargetDoorState.CLOSED); } ; } exports.SwitchPort = SwitchPort;