@elshaer/homebridge-hdl-buspro-enhanced
Version:
Linking the HDL bus into the Homebridge widget
100 lines • 4.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RelayLock = void 0;
const HMBOpen = 0;
const HMBClosed = 1;
class RelayLock {
constructor(platform, accessory, name, controller, device, listener, channel, nc, lock_timeout) {
this.platform = platform;
this.accessory = accessory;
this.name = name;
this.controller = controller;
this.device = device;
this.listener = listener;
this.channel = channel;
this.nc = nc;
this.lock_timeout = lock_timeout;
this.RelayLockStates = {
Lock: HMBClosed,
Target: HMBClosed,
};
this.HDLOpen = 1;
this.HDLClosed = 0;
const Service = this.platform.Service;
const Characteristic = this.platform.Characteristic;
this.accessory.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, 'HDL');
this.service =
this.accessory.getService(Service.LockMechanism) || this.accessory.addService(Service.LockMechanism);
this.service.setCharacteristic(Characteristic.Name, name);
this.service.getCharacteristic(Characteristic.LockCurrentState)
.onGet(this.handleLockCurrentStateGet.bind(this));
this.service.getCharacteristic(Characteristic.LockTargetState)
.onSet(this.handleLockTargetStateSet.bind(this))
.onGet(this.handleLockTargetStateGet.bind(this));
if (this.nc === false) {
this.HDLOpen = 0;
this.HDLClosed = 1;
}
const eventEmitter = this.listener.getChannelEventEmitter(this.channel);
eventEmitter.on('update', (level) => {
if (channel === this.channel) {
if (this.nc) {
this.RelayLockStates.Lock = (level === 0 ? HMBClosed : HMBOpen);
}
else {
this.RelayLockStates.Lock = (level === 0 ? HMBOpen : HMBClosed);
}
this.service.getCharacteristic(Characteristic.LockCurrentState).updateValue(this.RelayLockStates.Lock);
if (this.RelayLockStates.Lock === HMBClosed) {
this.platform.log.debug(this.name + ' is now closed');
}
else {
this.platform.log.debug(this.name + ' is now open');
}
}
});
}
async handleLockTargetStateSet(value) {
value = value;
const oldValue = this.RelayLockStates.Target;
this.RelayLockStates.Target = value;
this.service.getCharacteristic(this.platform.Characteristic.LockTargetState).updateValue(this.RelayLockStates.Target);
if (this.nc) {
if (value === 0) {
value = 1;
}
else {
value = 0;
}
}
this.controller.send({
target: this.device,
command: 0x0031,
data: { channel: this.channel, level: (value * 100) },
}, (err) => {
if (err) {
// Revert to the old value
this.RelayLockStates.Target = oldValue;
this.service.getCharacteristic(this.platform.Characteristic.LockTargetState).updateValue(this.RelayLockStates.Target);
this.platform.log.error(`Error setting LockTarget state for ${this.device.name}: ${err.message}`);
}
else {
this.platform.log.debug('Successfully sent command to ' + this.name);
if ((value === 1) && (this.lock_timeout > 0)) {
setTimeout(() => {
this.handleLockTargetStateSet(HMBClosed);
}, 1000 * this.lock_timeout);
}
}
});
}
async handleLockCurrentStateGet() {
return this.RelayLockStates.Lock;
}
async handleLockTargetStateGet() {
return this.RelayLockStates.Target;
}
}
exports.RelayLock = RelayLock;
//# sourceMappingURL=RelayLock.js.map