@elshaer/homebridge-hdl-buspro-enhanced
Version:
Linking the HDL bus into the Homebridge widget
118 lines • 6.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HDLBusproHomebridge = void 0;
const SmartBus = require("smart-bus");
const settings_1 = require("./settings");
const DeviceList_1 = require("./DeviceList");
const RelayRGB_1 = require("./RelayRGB");
class HDLBusproHomebridge {
constructor(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
this.accessories = [];
this.log.debug('Finished initializing platform:', this.config.name);
this.api.on('didFinishLaunching', () => {
log.debug('Executed didFinishLaunching callback');
this.discoverDevices();
});
}
configureAccessory(accessory) {
this.log.info('Loading accessory from cache:', accessory.displayName);
this.accessories.push(accessory);
}
discoverDevices() {
if (!Array.isArray(this.config.buses)) {
this.log.error(`Plugin configuration error: "buses" must be a valid array in your config.json. Current value: ${JSON.stringify(this.config.buses)}`);
return;
}
for (const bus of this.config.buses) {
const ip = bus.bus_IP;
const port = bus.bus_port;
const busObj = new SmartBus({
gateway: ip,
port: port,
});
for (const subnet of bus.subnets) {
const subnet_number = subnet.subnet_number;
const cd_number = subnet.cd_number;
const controllerObj = busObj.controller(`${subnet_number}.${cd_number}`);
const addressedDeviceMap = new Map();
const uniqueIDPrefix = `${ip}:${port}.${subnet_number}`;
for (const device of subnet.devices) {
this.discoverDevice(busObj, subnet_number, device, uniqueIDPrefix, controllerObj, addressedDeviceMap);
}
}
}
}
discoverDevice(busObj, subnet_number, device, uniqueIDPrefix, controllerObj, addressedDeviceMap) {
const deviceAddress = `${subnet_number}.${device.device_address}`;
const deviceType = (device.device_type === 'drycontact') ? device.drycontact_type : device.device_type;
this.log.info(`🔍 Discovering Device: ${device.device_name}, Type: ${deviceType}, Address: ${deviceAddress}`);
this.log.info('🔍 Raw Device Data:', JSON.stringify(device, null, 2));
if (deviceType === 'relayrgb') {
this.handleRGBDevice(device, deviceAddress, uniqueIDPrefix, busObj, controllerObj);
return;
}
const deviceTypeConfig = DeviceList_1.deviceTypeMap[deviceType];
if (!deviceTypeConfig) {
this.log.error(`Invalid device type: ${deviceType}`);
return;
}
const { deviceClass, listener, uniqueArgs, idEnding } = deviceTypeConfig;
const uniqueID = `${uniqueIDPrefix}.${device.device_address}.${idEnding(device)}`;
const uuid = this.api.hap.uuid.generate(uniqueID);
let deviceObj;
let listenerObj;
if (addressedDeviceMap.has(deviceAddress)) {
({ deviceObj, listenerObj } = addressedDeviceMap.get(deviceAddress));
}
else {
deviceObj = busObj.device(deviceAddress);
listenerObj = new listener(deviceObj, controllerObj);
addressedDeviceMap.set(deviceAddress, { deviceObj, listenerObj });
}
const commonArgs = [device.device_name, controllerObj, deviceObj, listenerObj];
const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid);
this.buildDevice(existingAccessory, deviceClass, commonArgs, uniqueArgs(device), uuid);
}
handleRGBDevice(device, deviceAddress, uniqueIDPrefix, busObj, controllerObj) {
this.log.info(`Found RGB Light Device: ${device.device_name} at Address ${deviceAddress}`);
const redChannel = device.red_channel;
const greenChannel = device.green_channel;
const blueChannel = device.blue_channel;
if (redChannel === undefined || greenChannel === undefined || blueChannel === undefined) {
this.log.error(`RGB Channels Undefined for ${device.device_name} - Check Configuration`);
return;
}
this.log.info(`Assigned RGB Channels - Red: ${redChannel}, Green: ${greenChannel}, Blue: ${blueChannel}`);
const uuid = this.api.hap.uuid.generate(`${uniqueIDPrefix}.${device.device_address}.rgb.${redChannel}-${greenChannel}-${blueChannel}`);
const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid);
if (existingAccessory) {
this.log.info(`Restoring existing RGB accessory: ${device.device_name}`);
new RelayRGB_1.RelayRGB(this, existingAccessory, device.device_name, controllerObj, busObj.device(deviceAddress), redChannel, greenChannel, blueChannel);
}
else {
this.log.info(`Creating new RGB accessory: ${device.device_name}`);
const accessory = new this.api.platformAccessory(device.device_name, uuid);
accessory.context.device = device;
new RelayRGB_1.RelayRGB(this, accessory, device.device_name, controllerObj, busObj.device(deviceAddress), redChannel, greenChannel, blueChannel);
this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
}
}
buildDevice(accessory, deviceClass, commonArgs, uniqueArgs, uuid) {
if (accessory) {
this.log.info('Restoring existing accessory from cache:', accessory.displayName);
}
else {
this.log.info('Adding new accessory:', commonArgs[0]);
accessory = new this.api.platformAccessory(commonArgs[0], uuid);
this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
}
new deviceClass(this, accessory, ...commonArgs, ...uniqueArgs);
}
}
exports.HDLBusproHomebridge = HDLBusproHomebridge;
//# sourceMappingURL=HDLPlatform.js.map