@elshaer/homebridge-hdl-buspro-enhanced
Version:
Linking the HDL bus into the Homebridge widget
99 lines • 3.96 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RelayListener = exports.RelayLightbulb = void 0;
const events_1 = require("events");
class RelayLightbulb {
constructor(platform, accessory, name, controller, device, listener, channel) {
this.platform = platform;
this.accessory = accessory;
this.name = name;
this.controller = controller;
this.device = device;
this.listener = listener;
this.channel = channel;
this.RelayLightbulbStates = {
On: false,
};
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.Lightbulb) || this.accessory.addService(Service.Lightbulb);
this.service.setCharacteristic(Characteristic.Name, name);
this.service.getCharacteristic(Characteristic.On)
.onSet(this.setOn.bind(this))
.onGet(this.getOn.bind(this));
const eventEmitter = this.listener.getChannelEventEmitter(this.channel);
eventEmitter.on('update', (level) => {
this.RelayLightbulbStates.On = (level > 0);
this.service.getCharacteristic(Characteristic.On).updateValue(this.RelayLightbulbStates.On);
if (this.RelayLightbulbStates.On) {
this.platform.log.debug(this.name + ' is now on');
}
else {
this.platform.log.debug(this.name + ' is now off');
}
});
}
async setOn(value) {
const oldValue = this.RelayLightbulbStates.On;
this.RelayLightbulbStates.On = value;
this.controller.send({
target: this.device,
command: 0x0031,
data: { channel: this.channel, level: (value * 100) },
}, (err) => {
if (err) {
// Revert to the old value
this.RelayLightbulbStates.On = oldValue;
this.platform.log.error(`Error setting On state for ${this.name}: ${err.message}`);
}
else {
this.platform.log.debug('Successfully sent command to ' + this.name);
}
});
}
async getOn() {
return this.RelayLightbulbStates.On;
}
}
exports.RelayLightbulb = RelayLightbulb;
class RelayListener {
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(0x0032, (command) => {
const data = command.data;
const channel = data.channel;
const level = data.level;
this.channelsMap.set(channel, level);
this.eventEmitter.emit(`update_${channel}`, level);
});
// status request response listener
this.device.on(0x0034, (command) => {
const data = command.data;
for (const channelInfo of data.channels) {
this.channelsMap.set(channelInfo.number, channelInfo.level);
this.eventEmitter.emit(`update_${channelInfo.number}`, channelInfo.level);
}
});
// status request
this.controller.send({
target: this.device,
command: 0x0033,
}, false);
}
// This function returns an EventEmitter for the specified channel
getChannelEventEmitter(channel) {
const eventEmitter = new events_1.EventEmitter();
this.eventEmitter.on(`update_${channel}`, (level) => {
eventEmitter.emit('update', level);
});
return eventEmitter;
}
}
exports.RelayListener = RelayListener;
//# sourceMappingURL=RelayLightbulb.js.map