UNPKG

magichome-platform

Version:

discover, control, and receive status for magichome devices

104 lines (103 loc) 4.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ControllerGenerator = void 0; const baseController_1 = require("./baseController"); const platformUtils_1 = require("../utils/platformUtils"); const magichome_core_1 = require("magichome-core"); class ControllerGenerator { constructor() { // public customControllers: Map<string, BaseController>; // public inactiveDeviceQueue: IFailedDeviceProps[] = []; this.interfaceOptions = { timeoutMS: 700 }; this._activeControllers = new Map(); // /** // * class function getActiveDevices // * // * Returns a map of <uniqueId, BaseController> pairs or; // * If provided a valid uniqueId, returns a single BaseController // * @param uniqueId (optional) // * @returns // */ // public getActiveDevice(uniqueId?: string): Map<string, BaseController> | BaseController { // if (uniqueId) { // return this.activeDevices[uniqueId]; // } else { // return this.activeDevices; // } // } /** TODO * change this to a base controller function * check if the device exists, if not create it. If yes, just send a message using the direct command protocol. */ /** * class function sendDirectCommand * * @param directCommand * @param commandOptions (optional) */ // public async sendDirectCommand(directCommand: DirectCommand, commandOptions?: ICommandOptions) { // const customCompleteDevice: ICustomCompleteDevice = { protoDevice: directCommand } // const controller = this.createCustomControllers([customCompleteDevice])[0]; // controller.activeDevice.setAllValues(directCommand, commandOptions); // } } set activeControllers(activeControllers) { this._activeControllers = activeControllers; } get activeControllers() { return this._activeControllers; } async getDevices(subnets) { const deviceBundles = await this.discoverDeviceBundles(subnets); const activeControllers = this.generateControllers(deviceBundles); return activeControllers; } async discoverDeviceBundles(subnets) { const protoDevices = await (0, platformUtils_1.discoverProtoDevices)(subnets); const deviceBundles = await (0, magichome_core_1.generateDeviceBundles)(protoDevices, this.interfaceOptions); return deviceBundles; } generateControllers(deviceBundles) { const activeControllers = this.iterateDevices(deviceBundles); return activeControllers; } /** * class function scanForUniqueIds * This is a function that will scan the network for devices with the provided uniqueIds * @param uniqueIds * @returns */ async scanForUniqueIds(uniqueIds, subnets = []) { const protoDevices = await (0, platformUtils_1.discoverProtoDevices)(subnets); const filteredProtoDevices = protoDevices.filter((protoDevice) => uniqueIds.includes(protoDevice.uniqueId)); const deviceBundles = await (0, magichome_core_1.generateDeviceBundles)(filteredProtoDevices, this.interfaceOptions); const activeControllers = this.iterateDevices(deviceBundles); return activeControllers; } // public generateCustomControllers(ICompleteDevicesInfo: ICompleteDeviceInfo[]): Map<string, BaseController> { // const completeDevices: ICompleteDevice[] = completeCustomDevices(ICompleteDevicesInfo); // const customControllers: Map<string, BaseController> = this.iterateDevices(completeDevices); // this.customControllers = customControllers; // return customControllers; // } iterateDevices(deviceBundles) { const baseControllers = new Map(); for (const deviceBundle of deviceBundles) { const uniqueId = deviceBundle.completeDevice.protoDevice.uniqueId; try { const baseController = new baseController_1.BaseController(deviceBundle); if (!baseController) { console.error("Error creating base controller for device:", deviceBundle.completeDevice.protoDevice); continue; } baseControllers.set(uniqueId, baseController); } catch (error) { console.error("Error creating base controller for device:", deviceBundle.completeDevice.protoDevice, error); continue; } } return baseControllers; } } exports.ControllerGenerator = ControllerGenerator;