homebridge-smartsystem
Version:
SmartServer (Proxy TCP sockets to the cloud, Smappee MQTT, Duotecno IP Nodes, Homekit interface)
85 lines • 3.96 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Lock = void 0;
const types_1 = require("../duotecno/types");
const logger_1 = require("../duotecno/logger");
const accessory_1 = require("./accessory");
// Johan Coppieters Jun 2020
//
// Lock
//
// if name of mood contains $ => "lock"
// if name contains * = permanent locked=on/unlocked=off
// else short press -> unlock, locked again after 1.2 sec
class Lock extends accessory_1.Accessory {
constructor(homebridge, unit) {
super(homebridge, unit);
this.targetState = this.homebridge.Characteristic.CurrentDoorState.CLOSED; // locked
(0, logger_1.log)("accessory", "created Lock -> " + unit.getDescription());
// set default for Unlocked to Locked
if (this.unit.getType() === types_1.UnitExtendedType.kUnlocker)
unit.setState(1);
}
getAccessoryServices() {
// Lock needs authentication
const lock = this.makeService(this.homebridge.Service.LockMechanism);
this.attachServices(lock);
return [lock];
}
attachServices(door) {
door.getCharacteristic(this.homebridge.Characteristic.LockTargetState)
.on('get', this.getTarget.bind(this))
.on('set', this.setTarget.bind(this));
door.getCharacteristic(this.homebridge.Characteristic.LockCurrentState)
.on('get', this.getCurrent.bind(this));
}
setTarget(value, next) {
this.targetState = value;
// unlocker is a mood, a lock is a switch
const dtVal = (this.unit.getType() === types_1.UnitExtendedType.kUnlocker) ? -1 : value;
(0, logger_1.log)("accessory", "HB setting target state of lock: to " + value + " of " + this.unit.getDescription() + " (duotecno: " + dtVal + ")");
this.unit.setState(dtVal)
.then(() => {
//bypass ip node update mechanism of Duotecno
this.unit.status = value;
this.updateState();
if (this.unit.getType() === types_1.UnitExtendedType.kUnlocker) {
// always set to "locked" after sending the request.
this.unit.resetTimer = setTimeout(() => {
(0, logger_1.log)("accessory", "Autolock for an unlocker after 2 secs of " + this.unit.getDescription());
this.targetState = this.homebridge.Characteristic.CurrentDoorState.CLOSED;
this.me.getCharacteristic(this.homebridge.Characteristic.LockTargetState).updateValue(this.targetState);
this.unit.status = 1;
this.me.getCharacteristic(this.homebridge.Characteristic.LockCurrentState).updateValue(this.targetState);
}, 2000);
}
next();
})
.catch(err => next(err));
}
getTarget(next) {
(0, logger_1.log)("accessory", "HB getting target state of lock = " + this.unit.status + " of " + this.unit.getDescription());
next(null, this.targetState);
}
getCurrent(next) {
this.unit.reqState(unit => {
try {
if (!unit) {
return next(new Error("Unit state not available"));
}
(0, logger_1.log)("accessory", "HB getting current state of lock = " + this.unit.status + " of " + this.unit.getDescription());
next(null, !!this.unit.status);
}
catch (err) {
next(err);
}
}).catch(err => next(err));
}
// in response to Duotecno status messages
updateState() {
(0, logger_1.log)("accessory", "Received updateState -> Homekit Lock for " + this.unit.node.getName() + " - " + this.unit.getName() + " -> " + this.unit.status);
this.me.getCharacteristic(this.homebridge.Characteristic.LockCurrentState).updateValue(this.unit.status);
}
}
exports.Lock = Lock;
//# sourceMappingURL=lock.js.map