@elshaer/homebridge-hdl-buspro-enhanced
Version:
Linking the HDL bus into the Homebridge widget
95 lines • 4.02 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DryListener = exports.ContactSensor = void 0;
const events_1 = require("events");
class ContactSensor {
constructor(platform, accessory, name, controller, device, listener, area, channel, nc) {
this.platform = platform;
this.accessory = accessory;
this.name = name;
this.controller = controller;
this.device = device;
this.listener = listener;
this.area = area;
this.channel = channel;
this.nc = nc;
this.ContactStates = {
Detected: this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED,
};
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.ContactSensor) || this.accessory.addService(Service.ContactSensor);
this.service.setCharacteristic(Characteristic.Name, name);
this.service.getCharacteristic(Characteristic.ContactSensorState)
.onGet(this.getOn.bind(this));
if (area !== -1) {
const eventEmitter = this.listener.getChannelEventEmitter(this.area, this.channel);
eventEmitter.on('update', (contact) => {
if (this.nc) {
if (contact) {
this.ContactStates.Detected = Characteristic.ContactSensorState.CONTACT_DETECTED;
}
else {
this.ContactStates.Detected = Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
}
}
else {
if (contact) {
this.ContactStates.Detected = Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
}
else {
this.ContactStates.Detected = Characteristic.ContactSensorState.CONTACT_DETECTED;
}
}
this.service.getCharacteristic(Characteristic.ContactSensorState).updateValue(this.ContactStates.Detected);
if (this.ContactStates.Detected ===
Characteristic.ContactSensorState.CONTACT_DETECTED) {
this.platform.log.debug(this.name + ' has detected contact');
}
});
setInterval(() => {
this.controller.send({
target: this.device,
command: 0x15CE,
data: { area: this.area, switch: this.channel },
}, false);
}, 1000);
}
}
async getOn() {
return this.ContactStates.Detected;
}
}
exports.ContactSensor = ContactSensor;
class DryListener {
constructor(device, controller) {
this.device = device;
this.controller = controller;
this.channelsMap = new Map();
this.eventEmitter = new events_1.EventEmitter();
// control response listener
this.device.on(0x15CF, (command) => {
const data = command.data;
const area = data.area;
const channel = data.switch;
const contact = data.contact;
const key = `${area}_${channel}`;
this.channelsMap.set(key, contact);
this.eventEmitter.emit(`update_${key}`, contact);
});
}
// This function returns an EventEmitter for the specified area and channel
getChannelEventEmitter(area, channel) {
const key = `${area}_${channel}`;
const eventEmitter = new events_1.EventEmitter();
this.eventEmitter.on(`update_${key}`, (contact) => {
eventEmitter.emit('update', contact);
});
return eventEmitter;
}
}
exports.DryListener = DryListener;
//# sourceMappingURL=ContactSensor.js.map