From ec0c857c97053c39d6def1d64b8750c038836940 Mon Sep 17 00:00:00 2001 From: watsonb8 Date: Thu, 23 Jan 2020 09:28:59 -0500 Subject: [PATCH] First pass done but untested --- src/DataProviders/HarmonyDataProvider.ts | 21 ++++++++++++--------- src/Models/HarmonyDevice.ts | 4 ++++ src/index.ts | 18 +++++++++++++----- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/DataProviders/HarmonyDataProvider.ts b/src/DataProviders/HarmonyDataProvider.ts index 5d4b7da..cf81997 100644 --- a/src/DataProviders/HarmonyDataProvider.ts +++ b/src/DataProviders/HarmonyDataProvider.ts @@ -26,10 +26,10 @@ interface IHarmonyDataProviderProps { class HarmonyDataProvider extends EventEmitter { private _log: any; - private _deviceConfigs: Array; + private _hubsByDevice: { [deviceName: string]: string } = {}; private _hubs: { [hubName: string]: IHub } = {}; - - private _devices: { [name: string]: HarmonyDevice; } = {}; + private _devicesByHub: { [hubName: string]: { [deviceName: string]: HarmonyDevice } } = {}; + // private _devices: { [name: string]: HarmonyDevice; } = {}; private _states: { [controlUnitName: string]: (IActivityState | undefined) } = {}; private _matrix: IMatrix; @@ -43,13 +43,16 @@ class HarmonyDataProvider extends EventEmitter { this._hubs[value.Name] = value; }); this._matrix = props.matrix; - this._deviceConfigs = props.deviceConfigs; + props.deviceConfigs.forEach((deviceConfig: IDeviceConfig) => { + this._hubsByDevice[deviceConfig.Name] = deviceConfig.Hub; + }); + // this._deviceConfigs = props.deviceConfigs; this.connect(); } - public get devices(): { [name: string]: HarmonyDevice; } { - return this._devices; + public get devicesByHub(): { [hubName: string]: { [deviceName: string]: HarmonyDevice } } { + return this._devicesByHub; } /** @@ -107,7 +110,7 @@ class HarmonyDataProvider extends EventEmitter { //Turn on devices await Promise.all(devicesToTurnOn.map(async (device: HarmonyDevice) => { - if (device && device.name && this._devices[device.name]) { + if (device && device.name) { if (!device.on) { this._log(`Turning on device ${device.name}`) await device.powerOn(); @@ -307,7 +310,7 @@ class HarmonyDataProvider extends EventEmitter { deviceCommands.forEach((command: any) => { commands[command.label] = command.action; }); - self._devices[dev.label] = new HarmonyDevice({ + self._devicesByHub[hub.Name][dev.label] = new HarmonyDevice({ id: dev.id, name: dev.label, commands: commands, @@ -333,7 +336,7 @@ class HarmonyDataProvider extends EventEmitter { * @param deviceName The device to retrieve. */ private getDeviceFromName(deviceName: string): HarmonyDevice { - return this._devices[deviceName]; + return this._devicesByHub[this._hubsByDevice[deviceName]][deviceName]; } /** diff --git a/src/Models/HarmonyDevice.ts b/src/Models/HarmonyDevice.ts index 614c414..9a321d7 100644 --- a/src/Models/HarmonyDevice.ts +++ b/src/Models/HarmonyDevice.ts @@ -30,6 +30,10 @@ export class HarmonyDevice { return this._on; } + public get commands(): { [name: string]: ICommand } { + return this._commands; + } + //Define device methods public supportsCommand(commandName: string): boolean { let command = this._commands[commandName]; diff --git a/src/index.ts b/src/index.ts index 9021fc2..59a2ec5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ import * as Accessories from "./Accessories"; import HarmonyDataProvider from "./DataProviders/HarmonyDataProvider"; import * as Config from "./Models/Config"; import { IDevice } from "./Models"; +import { HarmonyDevice } from "./Models/HarmonyDevice"; let Accessory: any; let Homebridge: any; @@ -49,13 +50,20 @@ class HarmonyMatrixPlatform { //Emit devices if requested if (this.config.EmitDevicesOnStartup) { this.dataProvider.on("Ready", () => { - const devices: { [name: string]: IDevice } = this.dataProvider!.devices; - Object.values(devices).forEach((device: IDevice) => { - this.log(`${device.name} : ${device.id}`); - Object.keys(device.commands).forEach((command: string) => { - this.log(` ${command}`); + + const hubs: { [hubName: string]: { [deviceName: string]: HarmonyDevice } } = this.dataProvider!.devicesByHub; + Object.keys(hubs).forEach((hubName: string) => { + const deviceDictionary = hubs[hubName]; + this.log(`${hubName}`) + + Object.values(deviceDictionary).forEach((device: HarmonyDevice) => { + this.log(` ${device.name} : ${device.id}`); + Object.keys(device.commands).forEach((command: string) => { + this.log(` ${command}`); + }); }); }); + }); } }