Successful multi hub

This commit is contained in:
watsonb8 2020-01-24 21:31:43 -05:00
parent ec0c857c97
commit 97c0f625db
4 changed files with 106 additions and 72 deletions

View File

@ -113,11 +113,11 @@ export class DeviceButton implements IAccessory {
//change state if stateful
if (this._buttonInfo.IsStateful && this._buttonState != newState) {
this._buttonState = newState
await this._dataProvider.sendCommand(this._deviceCommand);
//TODO await this._dataProvider.sendCommand(this._deviceCommand);
} else if (!this._buttonInfo.IsStateful) {
//Send the number of configured key presses
for (let i = 0; i < this._buttonInfo.NumberOfKeyPresses; i++) {
await this._dataProvider.sendCommand(this._deviceCommand);
//TODO await this._dataProvider.sendCommand(this._deviceCommand);
}
this._switchService.getCharacteristic(Characteristic.On).updateValue(false);
return callback(new Error("Normal Response"));

View File

@ -2,12 +2,12 @@ import { IActivity } from "../Models/Config/IActivity";
import { IDeviceSetupItem } from "../Models/Config/IDeviceSetupItem";
import { IInput, IMatrix, IOutput } from "../Models/Config/IMatrix";
import { RemoteKey } from '../Accessories/ControlUnit';
import { sleep } from '../Util/Sleep';
import { EventEmitter } from "events";
import { IDevice, ICommand } from '../Models/IDevice';
import { ICommand } from '../Models/IDevice';
import { IHub } from "../Models/Config/IHub";
import { IDeviceConfig } from "../Models/Config/IDeviceConfig";
import { HarmonyDevice } from "../Models/HarmonyDevice";
import { HarmonyHub } from "../Models/HarmonyHub";
let Characteristic: HAPNodeJS.Characteristic;
@ -27,9 +27,8 @@ interface IHarmonyDataProviderProps {
class HarmonyDataProvider extends EventEmitter {
private _log: any;
private _hubsByDevice: { [deviceName: string]: string } = {};
private _hubs: { [hubName: string]: IHub } = {};
private _devicesByHub: { [hubName: string]: { [deviceName: string]: HarmonyDevice } } = {};
// private _devices: { [name: string]: HarmonyDevice; } = {};
private _hubs: { [hubName: string]: HarmonyHub } = {};
// private _devicesByHub: { [hubName: string]: { [deviceName: string]: HarmonyDevice } } = {};
private _states: { [controlUnitName: string]: (IActivityState | undefined) } = {};
private _matrix: IMatrix;
@ -37,23 +36,18 @@ class HarmonyDataProvider extends EventEmitter {
constructor(props: IHarmonyDataProviderProps) {
super();
this._log = props.log;
//Construct hubs
props.hubs.forEach((value: IHub) => {
value.Harmony = new Harmony();
this._hubs[value.Name] = value;
});
this._matrix = props.matrix;
props.deviceConfigs.forEach((deviceConfig: IDeviceConfig) => {
this._hubsByDevice[deviceConfig.Name] = deviceConfig.Hub;
});
// this._deviceConfigs = props.deviceConfigs;
this.connect();
this.connect(props.hubs);
}
public get devicesByHub(): { [hubName: string]: { [deviceName: string]: HarmonyDevice } } {
return this._devicesByHub;
}
// public get devicesByHub(): { [hubName: string]: { [deviceName: string]: HarmonyDevice } } {
// return this._devicesByHub;
// }
/**
* Power on all devices in an activity.
@ -285,50 +279,20 @@ class HarmonyDataProvider extends EventEmitter {
}
}
/**
* Connect to harmony and receive device info
*/
private connect = async () => {
Object.values(this._hubs).forEach((value: IHub) => {
value.Harmony.connect(value.Ip);
});
let self = this;
setTimeout(async function () {
Object.keys(self._hubs).forEach(async (key: string) => {
let hub: IHub = self._hubs[key];
//Gather devices
let devices: any = await hub.Harmony.getDevices();
try {
await Promise.all(
//Add each to dictionary
devices.map(async (dev: any) => {
//get commands
let commands: { [name: string]: ICommand } = {};
let deviceCommands: any = await hub.Harmony.getDeviceCommands(dev.id);
deviceCommands.forEach((command: any) => {
commands[command.label] = command.action;
});
self._devicesByHub[hub.Name][dev.label] = new HarmonyDevice({
id: dev.id,
name: dev.label,
commands: commands,
log: self._log,
harmony: hub.Harmony
});
}));
self._log(`Harmony data provider ready`);
self.emit("Ready");
} catch (err) {
self._log(`ERROR - error connecting to harmony: ${err}`);
}
});
}, 1000);
private connect = async (hubs: Array<IHub>) => {
for (let i = 0; i < hubs.length; i++) {
const hub = hubs[i];
const newHarmonyHub = new HarmonyHub(hub.Ip, this._log);
this._hubs[hub.Name] = newHarmonyHub;
await newHarmonyHub.initialize();
}
// await Promise.all(
// hubs.map(async (hub: IHub): Promise<void> => {
// const newHarmonyHub = new HarmonyHub(hub.Ip, this._log);
// this._hubs[hub.Name] = newHarmonyHub;
// await newHarmonyHub.initialize();
// })
// )
}
/**
@ -336,7 +300,14 @@ class HarmonyDataProvider extends EventEmitter {
* @param deviceName The device to retrieve.
*/
private getDeviceFromName(deviceName: string): HarmonyDevice {
return this._devicesByHub[this._hubsByDevice[deviceName]][deviceName];
let device: HarmonyDevice | undefined;
try {
device = this._hubs[this._hubsByDevice[deviceName]].getDeviceByName(deviceName);
} catch (err) {
this._log(`Error retrieving device from hub: ${err}`);
}
return device!;
}
/**

63
src/Models/HarmonyHub.ts Normal file
View File

@ -0,0 +1,63 @@
import { HarmonyDevice } from './HarmonyDevice';
const Harmony = require("harmony-websocket");
import { ICommand } from './IDevice';
import { EventEmitter } from 'events';
export class HarmonyHub extends EventEmitter {
private _devices: { [deviceName: string]: HarmonyDevice } = {}
private _ip: string;
private _harmony: any;
private _log: any;
constructor(ipAddress: string, log: any) {
super();
this._ip = ipAddress;
this._log = log;
}
public get devices(): { [deviceName: string]: HarmonyDevice } {
return this._devices;
}
public getDeviceByName = (deviceName: string): HarmonyDevice => {
return this._devices[deviceName];
}
public initialize = async () => {
this._harmony = new Harmony();
await this._harmony.connect(this._ip);
//Gather devices
let devices: any = await this._harmony.getDevices();
try {
await Promise.all(
//Add each to dictionary
devices.map(async (dev: any) => {
//get commands
let commands: { [name: string]: ICommand } = {};
let deviceCommands: any = await this._harmony.getDeviceCommands(dev.id);
deviceCommands.forEach((command: any) => {
commands[command.label] = command.action;
});
this._devices[dev.label] = new HarmonyDevice({
id: dev.id,
name: dev.label,
commands: commands,
log: this._log,
harmony: this._harmony
});
}));
this._log(`Harmony data provider ready`);
this.emit("Ready");
} catch (err) {
this._log(`ERROR - error connecting to harmony: ${err}`);
}
}
private connect = async (): Promise<void> => {
await this._harmony.Connect(this._ip);
}
}

View File

@ -51,18 +51,18 @@ class HarmonyMatrixPlatform {
if (this.config.EmitDevicesOnStartup) {
this.dataProvider.on("Ready", () => {
const hubs: { [hubName: string]: { [deviceName: string]: HarmonyDevice } } = this.dataProvider!.devicesByHub;
Object.keys(hubs).forEach((hubName: string) => {
const deviceDictionary = hubs[hubName];
this.log(`${hubName}`)
// TODO 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}`);
});
});
});
// Object.values(deviceDictionary).forEach((device: HarmonyDevice) => {
// this.log(` ${device.name} : ${device.id}`);
// Object.keys(device.commands).forEach((command: string) => {
// this.log(` ${command}`);
// });
// });
// });
});
}