Code cleanup. Added comments.

This commit is contained in:
watsonb8 2019-06-17 15:59:15 -04:00
parent 5040435760
commit 8bb65b5b3c
7 changed files with 86 additions and 32 deletions

View File

@ -9,6 +9,25 @@ let Characteristic: HAPNodeJS.Characteristic;
let Api: any; let Api: any;
let homebridge: any; let homebridge: any;
/**
* Enum describing remote key presses from homebridge.
*/
export enum RemoteKey {
REWIND = 0,
FAST_FORWARD = 1,
NEXT_TRACK = 2,
PREVIOUS_TRACK = 3,
ARROW_UP = 4,
ARROW_DOWN = 5,
ARROW_LEFT = 6,
ARROW_RIGHT = 7,
SELECT = 8,
BACK = 9,
EXIT = 10,
PLAY_PAUSE = 11,
INFORMATION = 15,
}
export interface IControlUnitProps { export interface IControlUnitProps {
dataProvider: HarmonyDataProvider, dataProvider: HarmonyDataProvider,
displayName: string, displayName: string,
@ -266,8 +285,6 @@ export class ControlUnit implements IAccessory {
}); });
} }
/** /**
* Called by homebridge to gather services for this accessory. * Called by homebridge to gather services for this accessory.
*/ */

View File

@ -1,3 +1,7 @@
/**
* Interface to describe homebridge required elements.
*/
export interface IAccessory { export interface IAccessory {
/** /**
* Required by homebridge. * Required by homebridge.

View File

@ -1,7 +1,7 @@
import { Activity } from "../Models/Activity"; import { Activity } from "../Models/Activity";
import { DeviceSetupItem } from "../Models/DeviceSetupItem"; import { DeviceSetupItem } from "../Models/DeviceSetupItem";
import { threadId } from "worker_threads";
import { Input, Matrix, Output } from "../Models/Matrix"; import { Input, Matrix, Output } from "../Models/Matrix";
import { RemoteKey } from '../Accessories/ControlUnit';
let Characteristic: HAPNodeJS.Characteristic; let Characteristic: HAPNodeJS.Characteristic;
@ -19,22 +19,6 @@ interface IActivityState {
currentActivity: Activity currentActivity: Activity
} }
export enum RemoteKey {
REWIND = 0,
FAST_FORWARD = 1,
NEXT_TRACK = 2,
PREVIOUS_TRACK = 3,
ARROW_UP = 4,
ARROW_DOWN = 5,
ARROW_LEFT = 6,
ARROW_RIGHT = 7,
SELECT = 8,
BACK = 9,
EXIT = 10,
PLAY_PAUSE = 11,
INFORMATION = 15,
}
interface IHarmonyDataProviderProps { interface IHarmonyDataProviderProps {
hubAddress: string, hubAddress: string,
log: any, log: any,
@ -70,6 +54,9 @@ class HarmonyDataProvider {
this.connect(); this.connect();
} }
/**
* Power on all devices in an activity.
*/
public powerOn = async (controlUnitName: string, activity: Activity) => { public powerOn = async (controlUnitName: string, activity: Activity) => {
//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;
@ -78,6 +65,9 @@ class HarmonyDataProvider {
} }
} }
/**
* Power off all devices in an activity that aren't being used.
*/
public powerOff = async (controlUnitName: string) => { public powerOff = async (controlUnitName: string) => {
if (!this.states[controlUnitName]) { if (!this.states[controlUnitName]) {
return; return;
@ -99,6 +89,9 @@ class HarmonyDataProvider {
this.states[controlUnitName] = undefined; this.states[controlUnitName] = undefined;
} }
/**
* Start an activity
*/
public startActivity = async (controlUnitName: string, activity: Activity) => { public startActivity = async (controlUnitName: string, activity: Activity) => {
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: Activity | undefined = undefined;
@ -192,6 +185,9 @@ class HarmonyDataProvider {
this.states[controlUnitName] = { currentActivity: activity }; this.states[controlUnitName] = { currentActivity: activity };
} }
/**
* Turn the volume up for the current running activity.
*/
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]) {
@ -202,6 +198,9 @@ class HarmonyDataProvider {
} }
} }
/**
* Volume down for current running activity.
*/
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]) {
@ -212,52 +211,49 @@ class HarmonyDataProvider {
} }
} }
/**
* Send key press for current activity.
*
* @param controlUnitName The name of the control unit to act on.
* @param key The key to send.
*/
public sendKeyPress = async (controlUnitName: string, key: any) => { public sendKeyPress = async (controlUnitName: string, key: any) => {
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.controlDeviceId);
switch (key) { switch (key) {
//@ts-ignore
case RemoteKey.ARROW_UP: { case RemoteKey.ARROW_UP: {
commandName = "Direction Up"; commandName = "Direction Up";
break; break;
} }
//@ts-ignore
case RemoteKey.ARROW_DOWN: { case RemoteKey.ARROW_DOWN: {
commandName = "Direction Down"; commandName = "Direction Down";
break; break;
} }
//@ts-ignore
case RemoteKey.ARROW_LEFT: { case RemoteKey.ARROW_LEFT: {
commandName = "Direction Left"; commandName = "Direction Left";
break; break;
} }
//@ts-ignore
case RemoteKey.ARROW_RIGHT: { case RemoteKey.ARROW_RIGHT: {
commandName = "Direction Right"; commandName = "Direction Right";
break; break;
} }
//@ts-ignore
case RemoteKey.SELECT: { case RemoteKey.SELECT: {
commandName = "Select"; commandName = "Select";
break; break;
} }
//@ts-ignore
case RemoteKey.PLAY_PAUSE: { case RemoteKey.PLAY_PAUSE: {
commandName = "Pause"; commandName = "Pause";
break; break;
} }
//@ts-ignore
case RemoteKey.INFORMATION: { case RemoteKey.INFORMATION: {
commandName = "Menu"; commandName = "Menu";
break; break;
} }
//@ts-ignore
case RemoteKey.BACK: { case RemoteKey.BACK: {
commandName = "Back"; commandName = "Back";
break; break;
} }
//@ts-ignore
case RemoteKey.EXIT: { case RemoteKey.EXIT: {
commandName = "Back"; commandName = "Back";
break; break;
@ -270,6 +266,10 @@ class HarmonyDataProvider {
} }
} }
/**
* Return if a control unit is active
* @param controlUnitName
*/
public getIsActive(controlUnitName: string): Activity | undefined { public getIsActive(controlUnitName: string): Activity | undefined {
return this.states[controlUnitName] ? this.states[controlUnitName]!.currentActivity : undefined; return this.states[controlUnitName] ? this.states[controlUnitName]!.currentActivity : undefined;
} }
@ -318,6 +318,9 @@ class HarmonyDataProvider {
}, 1000); }, 1000);
} }
/**
* Power off a device (Power toggle if no power off).
*/
private powerOffDevice = async (device: IDevice) => { private powerOffDevice = async (device: IDevice) => {
let powerOffCommand: string = "Power Off"; let powerOffCommand: string = "Power Off";
let powerToggleCommand: string = "Power Toggle"; let powerToggleCommand: string = "Power Toggle";
@ -330,6 +333,9 @@ class HarmonyDataProvider {
} }
} }
/**
* Power on a device (Power toggle if no power on).
*/
private powerOnDevice = async (device: IDevice) => { private powerOnDevice = async (device: IDevice) => {
let powerOnCommand: string = "Power On"; let powerOnCommand: string = "Power On";
let powerToggleCommand: string = "Power Toggle"; let powerToggleCommand: string = "Power Toggle";
@ -342,10 +348,19 @@ class HarmonyDataProvider {
} }
} }
/**
* Get the IDevice by name.
* @param deviceName The device to retrieve.
*/
private getDeviceFromName(deviceName: string): IDevice { private getDeviceFromName(deviceName: string): IDevice {
return this.devices[deviceName]; return this.devices[deviceName];
} }
/**
* Helper function to make sure no control unit depends on device list.
* @param devicesToTurnOn The list of devices to modify.
* @param controlUnitName The name of the control unit in question.
*/
private sanitizeDeviceList(devicesToTurnOn: Array<IDevice>, controlUnitName: string): Array<IDevice> { private sanitizeDeviceList(devicesToTurnOn: Array<IDevice>, controlUnitName: string): Array<IDevice> {
for (let controlUnitKey in this.states) { for (let controlUnitKey in this.states) {
//Skip self //Skip self
@ -368,6 +383,10 @@ class HarmonyDataProvider {
return devicesToTurnOn; return devicesToTurnOn;
} }
/**
* Send a command to the harmony hub.
* @param command The command to send.
*/
private sendCommand = async (command: string) => { private sendCommand = async (command: string) => {
try { try {
let response = await this.harmony.sendCommand(JSON.stringify(command)); let response = await this.harmony.sendCommand(JSON.stringify(command));

View File

@ -4,6 +4,9 @@ export interface IDeviceSetupItemProps {
input: string input: string
} }
/**
* Data model to hold device setup items.
*/
export class DeviceSetupItem { export class DeviceSetupItem {
private _deviceId: string = ""; private _deviceId: string = "";
private _input: string = ""; private _input: string = "";

View File

@ -14,6 +14,9 @@ export interface Output {
outputDevice: string, outputDevice: string,
} }
/**
* Data model to hold matrix information.
*/
export class Matrix { export class Matrix {
private _inputs: Array<Input> = []; private _inputs: Array<Input> = [];
private _outputs: Array<Output> = []; private _outputs: Array<Output> = [];

View File

@ -1,5 +1,8 @@
/**
* Helper function to convert callbacks into promises
* @param func
*/
export default function callbackify(func: (...args: any[]) => Promise<any>): Function { export default function callbackify(func: (...args: any[]) => Promise<any>): Function {
return (...args: any[]) => { return (...args: any[]) => {
const onlyArgs: any[] = []; const onlyArgs: any[] = [];

View File

@ -7,6 +7,10 @@ import HarmonyDataProvider from "./DataProviders/HarmonyDataProvider";
let Accessory: any; let Accessory: any;
let Homebridge: any; let Homebridge: any;
/**
* Main entry.
* @param homebridge
*/
export default function (homebridge: any) { export default function (homebridge: any) {
Homebridge = homebridge; Homebridge = homebridge;
Accessory = homebridge.platformAccessory; Accessory = homebridge.platformAccessory;
@ -32,9 +36,13 @@ class HarmonyMatrixPlatform {
this.api.on('didFinishLaunching', this.didFinishLaunching.bind(this)); this.api.on('didFinishLaunching', this.didFinishLaunching.bind(this));
} }
/**
* Handler for didFinishLaunching
*/
didFinishLaunching() { didFinishLaunching() {
this.log(`Publishing external accessories`); this.log(`Publishing external accessories`);
//This is required in order to have multiple tv remotes on one platform
this.externalAccessories.forEach((accessory: ControlUnit) => { this.externalAccessories.forEach((accessory: ControlUnit) => {
this.api.publishExternalAccessories("HarmonyMatrixPlatform", [accessory.platformAccessory]); this.api.publishExternalAccessories("HarmonyMatrixPlatform", [accessory.platformAccessory]);
}) })
@ -126,9 +134,6 @@ class HarmonyMatrixPlatform {
this.log(`INFO - Added activity '${configActivity["DisplayName"]}'`); this.log(`INFO - Added activity '${configActivity["DisplayName"]}'`);
}); });
// let accessory = new Accessory(configControlUnit["DisplayName"],
// Homebridge.hap.uuid.generate(configControlUnit["DisplayName"], Homebridge.hap.Accessory.Categories.TELEVISION));
let controlUnit: ControlUnit = new ControlUnit({ let controlUnit: ControlUnit = new ControlUnit({
dataProvider: dataProvider, dataProvider: dataProvider,
displayName: configControlUnit["DisplayName"], displayName: configControlUnit["DisplayName"],