First pass done but untested

This commit is contained in:
watsonb8 2020-01-23 09:28:59 -05:00
parent ba1a1685ae
commit ec0c857c97
3 changed files with 29 additions and 14 deletions

View File

@ -26,10 +26,10 @@ interface IHarmonyDataProviderProps {
class HarmonyDataProvider extends EventEmitter {
private _log: any;
private _deviceConfigs: Array<IDeviceConfig>;
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];
}
/**

View File

@ -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];

View File

@ -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}`);
});
});
});
});
}
}