UNPKG

knxnetjs

Version:

A TypeScript library for KNXnet/IP communication

188 lines 8.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KNXInterfaceInformationImpl = void 0; exports.discoverInterfaces = discoverInterfaces; exports.createInterface = createInterface; const types_1 = require("./types"); const discovery_1 = require("./discovery"); const usb_1 = require("./interfaces/usb"); const routing_1 = require("./interfaces/routing"); const tunneling_1 = require("./interfaces/tunneling"); const constants_1 = require("./constants"); class KNXInterfaceInformationImpl { constructor(info) { this.type = info.type; this.name = info.name; if (info.description !== undefined) this.description = info.description; if (info.address !== undefined) this.address = info.address; if (info.port !== undefined) this.port = info.port; if (info.protocol !== undefined) this.protocol = info.protocol; if (info.capabilities !== undefined) this.capabilities = info.capabilities; if (info.knxAddress !== undefined) this.knxAddress = info.knxAddress; if (info.macAddress !== undefined) this.macAddress = info.macAddress; if (info.serialNumber !== undefined) this.serialNumber = info.serialNumber; if (info.friendlyName !== undefined) this.friendlyName = info.friendlyName; if (info.devicePath !== undefined) this.devicePath = info.devicePath; if (info.vendorId !== undefined) this.vendorId = info.vendorId; if (info.productId !== undefined) this.productId = info.productId; if (info.manufacturer !== undefined) this.manufacturer = info.manufacturer; if (info.product !== undefined) this.product = info.product; } /** * Check if this interface supports tunneling */ supportsTunneling() { return (this.type === types_1.KNXInterfaceType.TUNNELING || (this.type === types_1.KNXInterfaceType.ROUTING && this.capabilities !== undefined && (this.capabilities & constants_1.KNX_CONSTANTS.DEVICE_CAPABILITIES.TUNNELLING) !== 0)); } /** * Check if this interface supports routing */ supportsRouting() { return (this.type === types_1.KNXInterfaceType.ROUTING || (this.capabilities !== undefined && (this.capabilities & constants_1.KNX_CONSTANTS.DEVICE_CAPABILITIES.ROUTING) !== 0)); } /** * Check if this interface supports busmonitor mode */ supportsBusmonitor() { return this.type === types_1.KNXInterfaceType.USB || this.supportsTunneling(); } /** * Get a human-readable description of the interface */ toString() { switch (this.type) { case types_1.KNXInterfaceType.ROUTING: return `KNX Routing (${this.address}:${this.port || 3671}) - ${this.name}`; case types_1.KNXInterfaceType.TUNNELING: return `KNX Tunneling (${this.address}:${this.port || 3671}) - ${this.name}`; case types_1.KNXInterfaceType.USB: return `KNX USB (${this.devicePath || "auto-detect"}) - ${this.name}`; default: return `KNX Interface - ${this.name}`; } } } exports.KNXInterfaceInformationImpl = KNXInterfaceInformationImpl; /** * Discover available KNX interfaces (network and USB) * @param callback Function called for each discovered interface * @param options Discovery options */ async function discoverInterfaces(callback, options = {}) { const { timeout = 3000, includeUSB = true } = options; // Discover network interfaces const discovery = new discovery_1.KNXNetDiscovery(); try { // Set up discovery event handler discovery.on("deviceFound", (endpoint) => { // Create routing interface info if (endpoint.capabilities & constants_1.KNX_CONSTANTS.DEVICE_CAPABILITIES.ROUTING) { const routingInfo = new KNXInterfaceInformationImpl({ type: types_1.KNXInterfaceType.ROUTING, name: endpoint.name, description: "KNX/IP Routing Interface", address: endpoint.ip, port: endpoint.port, protocol: endpoint.protocol, capabilities: endpoint.capabilities, ...(endpoint.knxAddress && { knxAddress: endpoint.knxAddress }), ...(endpoint.macAddress && { macAddress: endpoint.macAddress }), ...(endpoint.serialNumber && { serialNumber: endpoint.serialNumber }), ...(endpoint.friendlyName && { friendlyName: endpoint.friendlyName }), }); callback(routingInfo); } // Create tunneling interface info if (endpoint.capabilities & constants_1.KNX_CONSTANTS.DEVICE_CAPABILITIES.TUNNELLING) { const tunnelingInfo = new KNXInterfaceInformationImpl({ type: types_1.KNXInterfaceType.TUNNELING, name: endpoint.name, description: "KNX/IP Tunneling Interface", address: endpoint.ip, port: endpoint.port, capabilities: endpoint.capabilities, ...(endpoint.knxAddress && { knxAddress: endpoint.knxAddress }), ...(endpoint.macAddress && { macAddress: endpoint.macAddress }), ...(endpoint.serialNumber && { serialNumber: endpoint.serialNumber }), ...(endpoint.friendlyName && { friendlyName: endpoint.friendlyName }), }); callback(tunnelingInfo); } }); // Start network discovery await discovery.discover({ timeout }); } finally { discovery.close(); } // Discover USB interfaces if (includeUSB) { const usbDevices = usb_1.KNXUSBImpl.getAvailableDevices(); for (const device of usbDevices) { const usbInfo = new KNXInterfaceInformationImpl({ type: types_1.KNXInterfaceType.USB, name: device.product || `USB KNX Interface (${device.vendorId?.toString(16)}:${device.productId?.toString(16)})`, description: "USB KNX Interface", ...(device.path && { devicePath: device.path }), ...(device.vendorId !== undefined && { vendorId: device.vendorId }), ...(device.productId !== undefined && { productId: device.productId }), ...(device.manufacturer && { manufacturer: device.manufacturer }), ...(device.product && { product: device.product }), }); callback(usbInfo); } } } /** * Create a KNX interface based on interface information * @param interfaceInfo The interface information * @param busmonitorMode Whether to enable busmonitor mode (read-only) * @returns A KNXBusInterface instance */ function createInterface(interfaceInfo, busmonitorMode = false) { switch (interfaceInfo.type) { case types_1.KNXInterfaceType.ROUTING: if (busmonitorMode) { throw new Error("Busmonitor mode is not supported for routing interfaces. Use tunneling or USB interfaces instead."); } return new routing_1.KNXNetRoutingImpl(interfaceInfo.address, interfaceInfo.port); case types_1.KNXInterfaceType.TUNNELING: if (!interfaceInfo.address) { throw new Error("Address is required for tunneling interfaces"); } return new tunneling_1.KNXNetTunnelingImpl(interfaceInfo.address, interfaceInfo.port, undefined, // localPort busmonitorMode); case types_1.KNXInterfaceType.USB: const usbOptions = { ...(interfaceInfo.devicePath && { devicePath: interfaceInfo.devicePath, }), busmonitorMode, }; return new usb_1.KNXUSBImpl(usbOptions); default: throw new Error(`Unsupported interface type: ${interfaceInfo.type}`); } } //# sourceMappingURL=interface-discovery.js.map