UNPKG

@pmouli/isy-matter-server

Version:

Service to expose an ISY device as a Matter Border router

78 lines 3.04 kB
import { Family } from '../Definitions/Global/Families.js'; import { ISYDevice } from '../ISYDevice.js'; import { cleanNodeInfo } from '../Model/NodeInfo.js'; import { InsteonDeviceFactory } from './Insteon/InsteonDeviceFactory.js'; import { NodeFactory } from './NodeFactory.js'; import { ZigBeeDeviceFactory } from './ZigBee/ZigBeeDeviceFactory.js'; export class DeviceFactory { static async createAll(isy, ...nodeInfos) { const devices = await Promise.all(nodeInfos.map(async (node) => { return await this.create(isy, node); })); for (const device of devices) { if (ISYDevice.isComposite(device)) { for (const child of this.nodeMap.get(device.node.address) ?? []) { device.addNode(child, isy); } } } return devices; } static nodeMap = new Map(); static async addChildNodes(isy) { isy.logger.debug('Adding child nodes. Composite Device Count: ' + DeviceFactory.nodeMap.size); for (const [address, nodeInfos] of DeviceFactory.nodeMap) { isy.logger.debug('Adding child nodes for: ' + address + '. Child Count: ' + nodeInfos.length); const parent = isy.getDevice(address); if (parent && ISYDevice.isComposite(parent)) { for (const nodeInfo of nodeInfos) { await parent.addNode(nodeInfo, isy); } await parent.applyNodeDefs(); } else { isy.logger.debug('Parent not found for: ' + address); for (const nodeInfo of nodeInfos) { try { await NodeFactory.create(nodeInfo, isy); } catch { } } } } } static async create(isy, nodeInfo) { const cinfo = cleanNodeInfo(nodeInfo); if (cinfo.pnode != cinfo.address) { let nodeInfos = DeviceFactory.nodeMap.get(cinfo.pnode) ?? []; nodeInfos.push(cinfo); DeviceFactory.nodeMap.set(nodeInfo.pnode, nodeInfos); return null; } let device = null; switch (cinfo.family) { case Family.Insteon: //@ts-ignore device = await InsteonDeviceFactory.create(isy, cinfo); break; case Family.ZigBee: device = (await ZigBeeDeviceFactory.create(isy, cinfo)); break; default: device = null; break; } if (!device) { device = (await NodeFactory.create(cinfo, isy)); } /*if (!device) { return new GenericNode(isy, nodeInfo) as any; }*/ return device; } } (function (DeviceFactory) { DeviceFactory.Insteon = InsteonDeviceFactory; DeviceFactory.ZigBee = ZigBeeDeviceFactory; })(DeviceFactory || (DeviceFactory = {})); //# sourceMappingURL=DeviceFactory.js.map