Simplified config parsing

This commit is contained in:
watsonb8 2020-01-01 00:17:37 -05:00
parent fb260c5532
commit 80ac6423e5
7 changed files with 91 additions and 265 deletions

View File

@ -1,5 +1,5 @@
import { Activity } from '../Models/Activity'; import { IActivity } from '../Models/Activity';
import { Matrix } from '../Models/Matrix'; import { IMatrix } from '../Models/Matrix';
import { IAccessory } from './IAccessory'; import { IAccessory } from './IAccessory';
import callbackify from '../Util/Callbackify'; import callbackify from '../Util/Callbackify';
import HarmonyDataProvider from '../DataProviders/HarmonyDataProvider'; import HarmonyDataProvider from '../DataProviders/HarmonyDataProvider';
@ -31,7 +31,7 @@ export enum RemoteKey {
export interface IControlUnitProps { export interface IControlUnitProps {
dataProvider: HarmonyDataProvider, dataProvider: HarmonyDataProvider,
displayName: string, displayName: string,
activities: Array<Activity>, activities: Array<IActivity>,
api: any, api: any,
log: any, log: any,
homebridge: any, homebridge: any,
@ -55,7 +55,7 @@ export class ControlUnit implements IAccessory {
private inputServices: Array<HAPNodeJS.Service | undefined> = []; private inputServices: Array<HAPNodeJS.Service | undefined> = [];
//Harmony fields //Harmony fields
private activities: Array<Activity> = []; private activities: Array<IActivity> = [];
private dataProvider: HarmonyDataProvider; private dataProvider: HarmonyDataProvider;
public platformAccessory: any; public platformAccessory: any;
@ -180,10 +180,10 @@ export class ControlUnit implements IAccessory {
* Event handler for GET active identifier characteristic * Event handler for GET active identifier characteristic
*/ */
private onGetActiveIdentifier = async () => { private onGetActiveIdentifier = async () => {
let currentActivity: Activity = this.dataProvider.getIsActive(this.name)!; let currentActivity: IActivity = this.dataProvider.getIsActive(this.name)!;
let identifier: number = 0; let identifier: number = 0;
if (currentActivity) { if (currentActivity) {
identifier = this.activities.findIndex(e => e.displayName === currentActivity.displayName); identifier = this.activities.findIndex(e => e.DisplayName === currentActivity.DisplayName);
} }
return identifier; return identifier;
} }
@ -240,13 +240,13 @@ export class ControlUnit implements IAccessory {
*/ */
private configureInputSourceService(): void { private configureInputSourceService(): void {
let inputs: Array<HAPNodeJS.Service> = []; let inputs: Array<HAPNodeJS.Service> = [];
this.activities.forEach((activity: Activity, index: number) => { this.activities.forEach((activity: IActivity, index: number) => {
let inputService = new Service.InputSource(activity.displayName, 'activity' + activity.displayName); let inputService = new Service.InputSource(activity.DisplayName, 'activity' + activity.DisplayName);
inputService inputService
.setCharacteristic(Characteristic.Identifier, index) .setCharacteristic(Characteristic.Identifier, index)
.setCharacteristic( .setCharacteristic(
Characteristic.ConfiguredName, Characteristic.ConfiguredName,
activity.displayName) activity.DisplayName)
.setCharacteristic( .setCharacteristic(
Characteristic.IsConfigured, Characteristic.IsConfigured,
//@ts-ignore //@ts-ignore

View File

@ -1,6 +1,6 @@
import { Activity } from "../Models/Activity"; import { IActivity } from "../Models/Activity";
import { DeviceSetupItem } from "../Models/DeviceSetupItem"; import { IDeviceSetupItem } from "../Models/DeviceSetupItem";
import { Input, Matrix, Output } from "../Models/Matrix"; import { IInput, IMatrix, IOutput } from "../Models/Matrix";
import { RemoteKey } from '../Accessories/ControlUnit'; import { RemoteKey } from '../Accessories/ControlUnit';
import { sleep } from '../Util/Sleep'; import { sleep } from '../Util/Sleep';
@ -17,13 +17,13 @@ interface IDevice {
} }
interface IActivityState { interface IActivityState {
currentActivity: Activity currentActivity: IActivity
} }
interface IHarmonyDataProviderProps { interface IHarmonyDataProviderProps {
hubAddress: string, hubAddress: string,
log: any, log: any,
matrix: Matrix matrix: IMatrix
} }
class HarmonyDataProvider { class HarmonyDataProvider {
@ -35,7 +35,7 @@ class HarmonyDataProvider {
private devices: { [name: string]: IDevice; } = {}; private devices: { [name: string]: IDevice; } = {};
private states: { [controlUnitName: string]: (IActivityState | undefined) } = {}; private states: { [controlUnitName: string]: (IActivityState | undefined) } = {};
private matrix: Matrix; private matrix: IMatrix;
constructor(props: IHarmonyDataProviderProps) { constructor(props: IHarmonyDataProviderProps) {
this.log = props.log; this.log = props.log;
@ -58,7 +58,7 @@ class HarmonyDataProvider {
/** /**
* Power on all devices in an activity. * Power on all devices in an activity.
*/ */
public powerOn = async (controlUnitName: string, activity: Activity) => { public powerOn = async (controlUnitName: string, activity: IActivity) => {
//Only power on if not alread on //Only power on if not alread on
let currentActivity = this.states[controlUnitName] ? this.states[controlUnitName]!.currentActivity : undefined; let currentActivity = this.states[controlUnitName] ? this.states[controlUnitName]!.currentActivity : undefined;
if (!currentActivity) { if (!currentActivity) {
@ -74,9 +74,9 @@ class HarmonyDataProvider {
return; return;
} }
//Build potential list of devices to turn off //Build potential list of devices to turn off
let devicesToTurnOff: Array<IDevice> = this.states[controlUnitName]!.currentActivity.deviceSetupItems let devicesToTurnOff: Array<IDevice> = this.states[controlUnitName]!.currentActivity.DeviceSetupList
.map((value: DeviceSetupItem): IDevice => { .map((value: IDeviceSetupItem): IDevice => {
return this.getDeviceFromName(value.deviceName); return this.getDeviceFromName(value.DeviceName);
}); });
//Resolve device conflicts with other controlUnits //Resolve device conflicts with other controlUnits
@ -93,16 +93,16 @@ class HarmonyDataProvider {
/** /**
* Start an activity * Start an activity
*/ */
public startActivity = async (controlUnitName: string, activity: Activity) => { public startActivity = async (controlUnitName: string, activity: IActivity) => {
this.log(`Starting activity ${activity.displayName} for controlUnit: ${controlUnitName}`) this.log(`Starting activity ${activity.DisplayName} for controlUnit: ${controlUnitName}`)
let lastActivity: Activity | undefined = undefined; let lastActivity: IActivity | undefined = undefined;
if (this.states[controlUnitName]) { if (this.states[controlUnitName]) {
lastActivity = this.states[controlUnitName]!.currentActivity; lastActivity = this.states[controlUnitName]!.currentActivity;
} }
//Build potential list of devices to to turn on //Build potential list of devices to to turn on
let devicesToTurnOn: Array<IDevice> = activity.deviceSetupItems.map((value: DeviceSetupItem): IDevice => { let devicesToTurnOn: Array<IDevice> = activity.DeviceSetupList.map((value: IDeviceSetupItem): IDevice => {
return this.getDeviceFromName(value.deviceName); return this.getDeviceFromName(value.DeviceName);
}); });
//Resolve device conflicts with other controlUnits //Resolve device conflicts with other controlUnits
@ -120,25 +120,25 @@ class HarmonyDataProvider {
//Assign correct input //Assign correct input
await Promise.all( await Promise.all(
activity.deviceSetupItems.map(async (value: DeviceSetupItem) => { activity.DeviceSetupList.map(async (value: IDeviceSetupItem) => {
let device: IDevice = this.getDeviceFromName(value.deviceName); let device: IDevice = this.getDeviceFromName(value.DeviceName);
if (device && device.supportsCommand(`Input${value.input}`)) { if (device && device.supportsCommand(`Input${value.Input}`)) {
let command: string = device.getCommand(`Input${value.input}`); let command: string = device.getCommand(`Input${value.Input}`);
await this.sendCommand(command); await this.sendCommand(command);
} }
}) })
); );
if (activity.useMatrix) { if (activity.UseMatrix) {
//get input and output //get input and output
let input: Input = this.matrix.inputs.filter(e => e.inputDevice === activity.controlDeviceId)[0]; let input: IInput = this.matrix.Inputs.filter(e => e.InputDevice === activity.ControlDevice)[0];
let output: Output = this.matrix.outputs.filter(e => e.outputDevice === activity.outputDeviceId)[0]; let output: IOutput = this.matrix.Outputs.filter(e => e.OutputDevice === activity.OutputDevice)[0];
let inputCommandName: string = `In ${input.inputNumber}`; let inputCommandName: string = `In ${input.InputNumber}`;
let outputCommandName: string = `Out ${output.outputLetter}`; let outputCommandName: string = `Out ${output.OutputLetter}`;
let matrixDevice: IDevice = this.getDeviceFromName(this.matrix.deviceName); let matrixDevice: IDevice = this.getDeviceFromName(this.matrix.DeviceName);
//Route hdmi //Route hdmi
if (matrixDevice.supportsCommand(inputCommandName) && matrixDevice.supportsCommand(outputCommandName)) { if (matrixDevice.supportsCommand(inputCommandName) && matrixDevice.supportsCommand(outputCommandName)) {
@ -151,15 +151,15 @@ class HarmonyDataProvider {
//Build potential list of devices to turn off //Build potential list of devices to turn off
if (lastActivity) { if (lastActivity) {
let devicesToTurnOff: Array<IDevice> = lastActivity.deviceSetupItems.map((value: DeviceSetupItem): IDevice => { let devicesToTurnOff: Array<IDevice> = lastActivity.DeviceSetupList.map((value: IDeviceSetupItem): IDevice => {
return this.getDeviceFromName(value.deviceName); return this.getDeviceFromName(value.DeviceName);
}); });
//remove devices that will be used for next activity from list //remove devices that will be used for next activity from list
//delete array[index] is stupid because it just nulls out the index. But now i have to deal with nulls //delete array[index] is stupid because it just nulls out the index. But now i have to deal with nulls
devicesToTurnOff.forEach((device: IDevice, index: number) => { devicesToTurnOff.forEach((device: IDevice, index: number) => {
if (device && device.name && activity.deviceSetupItems.some(e => { if (device && device.name && activity.DeviceSetupList.some(e => {
return (e && e.deviceName === device.name) return (e && e.DeviceName === device.name)
})) { })) {
delete devicesToTurnOff[index]; delete devicesToTurnOff[index];
} }
@ -194,7 +194,7 @@ class HarmonyDataProvider {
public volumeUp = async (controlUnitName: string) => { public volumeUp = async (controlUnitName: string) => {
let volumeUpCommand: string = "Volume Up" let volumeUpCommand: string = "Volume Up"
if (this.states[controlUnitName]) { if (this.states[controlUnitName]) {
let volumeDevice: IDevice = this.getDeviceFromName(this.states[controlUnitName]!.currentActivity.volumeDeviceId); let volumeDevice: IDevice = this.getDeviceFromName(this.states[controlUnitName]!.currentActivity.VolumeDevice);
if (volumeDevice.supportsCommand(volumeUpCommand)) { if (volumeDevice.supportsCommand(volumeUpCommand)) {
this.sendCommand(volumeDevice.getCommand(volumeUpCommand)); this.sendCommand(volumeDevice.getCommand(volumeUpCommand));
} }
@ -207,7 +207,7 @@ class HarmonyDataProvider {
public volumeDown = async (controlUnitName: string) => { public volumeDown = async (controlUnitName: string) => {
let volumeDownCommand: string = "Volume Down" let volumeDownCommand: string = "Volume Down"
if (this.states[controlUnitName]) { if (this.states[controlUnitName]) {
let volumeDevice: IDevice = this.getDeviceFromName(this.states[controlUnitName]!.currentActivity.volumeDeviceId); let volumeDevice: IDevice = this.getDeviceFromName(this.states[controlUnitName]!.currentActivity.VolumeDevice);
if (volumeDevice.supportsCommand(volumeDownCommand)) { if (volumeDevice.supportsCommand(volumeDownCommand)) {
this.sendCommand(volumeDevice.getCommand(volumeDownCommand)); this.sendCommand(volumeDevice.getCommand(volumeDownCommand));
} }
@ -224,7 +224,7 @@ class HarmonyDataProvider {
if (this.states[controlUnitName]) { if (this.states[controlUnitName]) {
let commandName: string = ""; let commandName: string = "";
let device: IDevice = this.getDeviceFromName(this.states[controlUnitName]!.currentActivity.controlDeviceId); let device: IDevice = this.getDeviceFromName(this.states[controlUnitName]!.currentActivity.ControlDevice);
switch (key) { switch (key) {
case RemoteKey.ARROW_UP: { case RemoteKey.ARROW_UP: {
commandName = "Direction Up"; commandName = "Direction Up";
@ -274,7 +274,7 @@ class HarmonyDataProvider {
* Return if a control unit is active * Return if a control unit is active
* @param controlUnitName * @param controlUnitName
*/ */
public getIsActive(controlUnitName: string): Activity | undefined { public getIsActive(controlUnitName: string): IActivity | undefined {
return this.states[controlUnitName] ? this.states[controlUnitName]!.currentActivity : undefined; return this.states[controlUnitName] ? this.states[controlUnitName]!.currentActivity : undefined;
} }
@ -374,10 +374,10 @@ class HarmonyDataProvider {
let currentOtherState: IActivityState = this.states[controlUnitKey]!; let currentOtherState: IActivityState = this.states[controlUnitKey]!;
if (currentOtherState) { if (currentOtherState) {
currentOtherState.currentActivity.deviceSetupItems.forEach((value: DeviceSetupItem) => { currentOtherState.currentActivity.DeviceSetupList.forEach((value: IDeviceSetupItem) => {
//there are devices to remove //there are devices to remove
if (devicesToTurnOn.some(e => e && e.name === value.deviceName)) { if (devicesToTurnOn.some(e => e && e.name === value.DeviceName)) {
let deviceToRemove: IDevice = devicesToTurnOn.filter(i => i.name === value.deviceName)[0]; let deviceToRemove: IDevice = devicesToTurnOn.filter(i => i.name === value.DeviceName)[0];
delete devicesToTurnOn[devicesToTurnOn.indexOf(deviceToRemove)]; delete devicesToTurnOn[devicesToTurnOn.indexOf(deviceToRemove)];
} }
}); });

View File

@ -1,69 +1,11 @@
import { DeviceSetupItem } from './DeviceSetupItem'; import { IDeviceSetupItem } from './DeviceSetupItem';
/**
* Input properties.
*/
export interface IActivityProps {
deviceList: Array<DeviceSetupItem>,
controlDeviceId: string,
volumeDeviceId: string,
outputDeviceId: string,
displayName: string
useMatrix: boolean,
}
/** export interface IActivity {
* Data model class to hold activity related information. OutputDevice: string;
*/ VolumeDevice: string;
export class Activity { ControlDevice: string;
private _volumeDeviceId: string = ""; DisplayName: string;
private _outputDeviceId: string = ""; DeviceSetupList: Array<IDeviceSetupItem>;
private _controlDeviceId: string = ""; UseMatrix: boolean;
private _displayName: string = "";
private _deviceSetupItems: Array<DeviceSetupItem>;
private _useMatrix: boolean = false;
constructor(props: IActivityProps) {
this._controlDeviceId = props.controlDeviceId;
this._outputDeviceId = props.outputDeviceId;
this._volumeDeviceId = props.volumeDeviceId;
this._displayName = props.displayName;
this._deviceSetupItems = props.deviceList;
this._useMatrix = props.useMatrix
}
/**
* The device associated with main control.
*/
public get controlDeviceId(): string {
return this._controlDeviceId;
};
/**
* The device associated with the volume control.
*/
public get volumeDeviceId(): string {
return this._volumeDeviceId
};
/**
* The device associated with output.
*/
public get outputDeviceId(): string {
return this._outputDeviceId;
};
/**
* The display name of the activity.
*/
public get displayName(): string {
return this._displayName;
}
public get deviceSetupItems(): Array<DeviceSetupItem> {
return this._deviceSetupItems
}
public get useMatrix(): boolean {
return this._useMatrix;
}
} }

View File

@ -1,25 +1,5 @@
export interface IDeviceSetupItemProps { export interface IDeviceSetupItem {
deviceName: string, DeviceName: string;
input: string Input: string;
}
/**
* Data model to hold device setup items.
*/
export class DeviceSetupItem {
private _deviceId: string = "";
private _input: string = "";
constructor(props: IDeviceSetupItemProps) {
this._deviceId = props.deviceName;
this._input = props.input;
}
public get deviceName() {
return this._deviceId;
}
public get input() {
return this._input;
}
} }

13
src/Models/IConfig.ts Normal file
View File

@ -0,0 +1,13 @@
import { IMatrix } from "./Matrix";
import { IActivity } from "./Activity";
export interface IControlUnit {
DisplayName: string;
Activities: Array<IActivity>;
}
export interface IConfig {
hubIp: string;
Matrix: IMatrix
ControlUnits: Array<IControlUnit>
}

View File

@ -1,42 +1,15 @@
export interface IMatrixProps { export interface IInput {
inputs: Array<Input>, InputNumber: string,
outputs: Array<Output>, InputDevice: string,
deviceName: string,
} }
export interface Input { export interface IOutput {
inputNumber: string, OutputLetter: string,
inputDevice: string, OutputDevice: string,
} }
export interface Output { export interface IMatrix {
outputLetter: string, Inputs: Array<IInput>;
outputDevice: string, Outputs: Array<IOutput>;
} DeviceName: string;
/**
* Data model to hold matrix information.
*/
export class Matrix {
private _inputs: Array<Input> = [];
private _outputs: Array<Output> = [];
private _deviceName: string;
constructor(props: IMatrixProps) {
this._inputs = props.inputs;
this._outputs = props.outputs;
this._deviceName = props.deviceName;
}
public get inputs(): Array<Input> {
return this._inputs
}
public get outputs(): Array<Output> {
return this._outputs;
}
public get deviceName(): string {
return this._deviceName;
}
} }

View File

@ -1,8 +1,9 @@
import { ControlUnit } from "./Accessories/ControlUnit"; import { ControlUnit } from "./Accessories/ControlUnit";
import { Activity } from "./Models/Activity"; import { IActivity } from "./Models/Activity";
import { DeviceSetupItem } from "./Models/DeviceSetupItem"; import { IDeviceSetupItem } from "./Models/DeviceSetupItem";
import { Input, Output, Matrix } from "./Models/Matrix"; import { IInput, IOutput, IMatrix } from "./Models/Matrix";
import HarmonyDataProvider from "./DataProviders/HarmonyDataProvider"; import HarmonyDataProvider from "./DataProviders/HarmonyDataProvider";
import { IConfig, IControlUnit } from "./Models/IConfig";
let Accessory: any; let Accessory: any;
let Homebridge: any; let Homebridge: any;
@ -24,7 +25,7 @@ export default function (homebridge: any) {
class HarmonyMatrixPlatform { class HarmonyMatrixPlatform {
log: any = {}; log: any = {};
config: any = {}; config: IConfig;
api: any; api: any;
dataProvider: HarmonyDataProvider | null; dataProvider: HarmonyDataProvider | null;
controlUnits: Array<ControlUnit> = []; controlUnits: Array<ControlUnit> = [];
@ -37,7 +38,6 @@ class HarmonyMatrixPlatform {
this.api.on('didFinishLaunching', this.didFinishLaunching.bind(this)); this.api.on('didFinishLaunching', this.didFinishLaunching.bind(this));
this.dataProvider = null; this.dataProvider = null;
this.log("This is new");
} }
/** /**
@ -51,7 +51,6 @@ class HarmonyMatrixPlatform {
this.controlUnits.forEach((accessory: ControlUnit) => { this.controlUnits.forEach((accessory: ControlUnit) => {
this.api.publishExternalAccessories("HarmonyMatrixPlatform", [accessory.platformAccessory]); this.api.publishExternalAccessories("HarmonyMatrixPlatform", [accessory.platformAccessory]);
}) })
} }
/** /**
@ -59,103 +58,22 @@ class HarmonyMatrixPlatform {
* @param callback * @param callback
*/ */
accessories(callback: (accessories: Array<ControlUnit>) => void) { accessories(callback: (accessories: Array<ControlUnit>) => void) {
//Parse ip
let hubIp: string = this.config["hubIp"];
//Parse matrix
let configInputs: any = this.config["Matrix"]["Inputs"];
let configOutputs: any = this.config["Matrix"]["Outputs"];
let matrixName: string = this.config["Matrix"]["DeviceName"];
let inputs: Array<Input> = [];
let outputs: Array<Output> = [];
configInputs.forEach((configInput: any) => {
let inputDevice: string = configInput["InputDevice"];
let inputNumber: string = configInput["InputNumber"];
this.log(`INFO - Added input to matrix '${inputDevice}'`);
inputs.push({
inputDevice: inputDevice,
inputNumber: inputNumber
});
});
configOutputs.forEach((configOutput: any) => {
let outputDevice: string = configOutput["OutputDevice"];
let outputLetter: string = configOutput["OutputLetter"];
this.log(`INFO - Added output to matrix '${outputDevice}'`);
outputs.push({
outputDevice: outputDevice,
outputLetter: outputLetter
});
});
let matrix = new Matrix({
inputs: inputs,
outputs: outputs,
deviceName: matrixName,
});
//construct data provider //construct data provider
this.dataProvider = new HarmonyDataProvider({ this.dataProvider = new HarmonyDataProvider({
hubAddress: hubIp, hubAddress: this.config.hubIp,
matrix: matrix, matrix: this.config.Matrix,
log: this.log log: this.log
}); });
//Parse control units this.config.ControlUnits.forEach((unit: IControlUnit) => {
let configControlUnits: any = this.config["ControlUnits"]; this.controlUnits.push(new ControlUnit({
configControlUnits.forEach((configControlUnit: any) => {
//Parse activities list
let configActivities: any = configControlUnit["Activities"];
let activities: Array<Activity> = [];
configActivities.forEach((configActivity: any) => {
//parse devices
let configDevices: any = configActivity["DeviceSetupList"];
let devices: Array<DeviceSetupItem> = [];
configDevices.forEach((configDevice: any) => {
//Add device
devices.push(new DeviceSetupItem({
deviceName: configDevice["DeviceName"],
input: configDevice["Input"]
}));
this.log(`INFO - Added device '${configDevice["DeviceName"]}' for activity '${configActivity["DisplayName"]}'`);
});
//Add activity
activities.push(new Activity({
volumeDeviceId: configActivity["VolumeDevice"],
controlDeviceId: configActivity["ControlDevice"],
outputDeviceId: configActivity["OutputDevice"],
displayName: configActivity["DisplayName"],
useMatrix: configActivity["UseMatrix"] === "true" ? true : false,
deviceList: devices
}));
this.log(`INFO - Added activity '${configActivity["DisplayName"]}'`);
});
let controlUnit: ControlUnit = new ControlUnit({
dataProvider: this.dataProvider!, dataProvider: this.dataProvider!,
displayName: configControlUnit["DisplayName"], displayName: unit.DisplayName,
api: this.api, api: this.api,
log: this.log, log: this.log,
activities: activities, activities: unit.Activities,
homebridge: Homebridge, homebridge: Homebridge,
}); }));
//@ts-ignore
let accessory = controlUnit as homebridge.platformAccessory;
//@ts-ignore
//Add control unit
this.controlUnits.push(accessory);
this.log(`INFO - Added ControlUnit`);
}); });
callback(this.controlUnits); callback(this.controlUnits);
} }