diff --git a/src/Accessories/ControlUnit.ts b/src/Accessories/ControlUnit.ts index 7c4af58..2bcd7f4 100644 --- a/src/Accessories/ControlUnit.ts +++ b/src/Accessories/ControlUnit.ts @@ -9,6 +9,25 @@ let Characteristic: HAPNodeJS.Characteristic; let Api: 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 { dataProvider: HarmonyDataProvider, displayName: string, @@ -266,8 +285,6 @@ export class ControlUnit implements IAccessory { }); } - - /** * Called by homebridge to gather services for this accessory. */ diff --git a/src/Accessories/IAccessory.ts b/src/Accessories/IAccessory.ts index 77c8a8c..681a8b9 100644 --- a/src/Accessories/IAccessory.ts +++ b/src/Accessories/IAccessory.ts @@ -1,3 +1,7 @@ + +/** + * Interface to describe homebridge required elements. + */ export interface IAccessory { /** * Required by homebridge. diff --git a/src/DataProviders/HarmonyDataProvider.ts b/src/DataProviders/HarmonyDataProvider.ts index f3118b1..66db933 100644 --- a/src/DataProviders/HarmonyDataProvider.ts +++ b/src/DataProviders/HarmonyDataProvider.ts @@ -1,7 +1,7 @@ import { Activity } from "../Models/Activity"; import { DeviceSetupItem } from "../Models/DeviceSetupItem"; -import { threadId } from "worker_threads"; import { Input, Matrix, Output } from "../Models/Matrix"; +import { RemoteKey } from '../Accessories/ControlUnit'; let Characteristic: HAPNodeJS.Characteristic; @@ -19,22 +19,6 @@ interface IActivityState { 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 { hubAddress: string, log: any, @@ -70,6 +54,9 @@ class HarmonyDataProvider { this.connect(); } + /** + * Power on all devices in an activity. + */ public powerOn = async (controlUnitName: string, activity: Activity) => { //Only power on if not alread on 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) => { if (!this.states[controlUnitName]) { return; @@ -99,6 +89,9 @@ class HarmonyDataProvider { this.states[controlUnitName] = undefined; } + /** + * Start an activity + */ public startActivity = async (controlUnitName: string, activity: Activity) => { this.log(`Starting activity ${activity.displayName} for controlUnit: ${controlUnitName}`) let lastActivity: Activity | undefined = undefined; @@ -192,6 +185,9 @@ class HarmonyDataProvider { this.states[controlUnitName] = { currentActivity: activity }; } + /** + * Turn the volume up for the current running activity. + */ public volumeUp = async (controlUnitName: string) => { let volumeUpCommand: string = "Volume Up" if (this.states[controlUnitName]) { @@ -202,6 +198,9 @@ class HarmonyDataProvider { } } + /** + * Volume down for current running activity. + */ public volumeDown = async (controlUnitName: string) => { let volumeDownCommand: string = "Volume Down" 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) => { if (this.states[controlUnitName]) { let commandName: string = ""; let device: IDevice = this.getDeviceFromName(this.states[controlUnitName]!.currentActivity.controlDeviceId); switch (key) { - //@ts-ignore case RemoteKey.ARROW_UP: { commandName = "Direction Up"; break; } - //@ts-ignore case RemoteKey.ARROW_DOWN: { commandName = "Direction Down"; break; } - //@ts-ignore case RemoteKey.ARROW_LEFT: { commandName = "Direction Left"; break; } - //@ts-ignore case RemoteKey.ARROW_RIGHT: { commandName = "Direction Right"; break; } - //@ts-ignore case RemoteKey.SELECT: { commandName = "Select"; break; } - //@ts-ignore case RemoteKey.PLAY_PAUSE: { commandName = "Pause"; break; } - //@ts-ignore case RemoteKey.INFORMATION: { commandName = "Menu"; break; } - //@ts-ignore case RemoteKey.BACK: { commandName = "Back"; break; } - //@ts-ignore case RemoteKey.EXIT: { commandName = "Back"; break; @@ -270,6 +266,10 @@ class HarmonyDataProvider { } } + /** + * Return if a control unit is active + * @param controlUnitName + */ public getIsActive(controlUnitName: string): Activity | undefined { return this.states[controlUnitName] ? this.states[controlUnitName]!.currentActivity : undefined; } @@ -318,6 +318,9 @@ class HarmonyDataProvider { }, 1000); } + /** + * Power off a device (Power toggle if no power off). + */ private powerOffDevice = async (device: IDevice) => { let powerOffCommand: string = "Power Off"; 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) => { let powerOnCommand: string = "Power On"; 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 { 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, controlUnitName: string): Array { for (let controlUnitKey in this.states) { //Skip self @@ -368,6 +383,10 @@ class HarmonyDataProvider { return devicesToTurnOn; } + /** + * Send a command to the harmony hub. + * @param command The command to send. + */ private sendCommand = async (command: string) => { try { let response = await this.harmony.sendCommand(JSON.stringify(command)); diff --git a/src/Models/DeviceSetupItem.ts b/src/Models/DeviceSetupItem.ts index c763270..e9e4f4e 100644 --- a/src/Models/DeviceSetupItem.ts +++ b/src/Models/DeviceSetupItem.ts @@ -4,6 +4,9 @@ export interface IDeviceSetupItemProps { input: string } +/** + * Data model to hold device setup items. + */ export class DeviceSetupItem { private _deviceId: string = ""; private _input: string = ""; diff --git a/src/Models/Matrix.ts b/src/Models/Matrix.ts index 15b5f92..d067976 100644 --- a/src/Models/Matrix.ts +++ b/src/Models/Matrix.ts @@ -14,6 +14,9 @@ export interface Output { outputDevice: string, } +/** + * Data model to hold matrix information. + */ export class Matrix { private _inputs: Array = []; private _outputs: Array = []; diff --git a/src/Util/Callbackify.ts b/src/Util/Callbackify.ts index f96c852..4bf521f 100644 --- a/src/Util/Callbackify.ts +++ b/src/Util/Callbackify.ts @@ -1,5 +1,8 @@ - +/** + * Helper function to convert callbacks into promises + * @param func + */ export default function callbackify(func: (...args: any[]) => Promise): Function { return (...args: any[]) => { const onlyArgs: any[] = []; diff --git a/src/index.ts b/src/index.ts index 7986564..cb21140 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,6 +7,10 @@ import HarmonyDataProvider from "./DataProviders/HarmonyDataProvider"; let Accessory: any; let Homebridge: any; +/** + * Main entry. + * @param homebridge + */ export default function (homebridge: any) { Homebridge = homebridge; Accessory = homebridge.platformAccessory; @@ -32,9 +36,13 @@ class HarmonyMatrixPlatform { this.api.on('didFinishLaunching', this.didFinishLaunching.bind(this)); } + /** + * Handler for didFinishLaunching + */ didFinishLaunching() { this.log(`Publishing external accessories`); + //This is required in order to have multiple tv remotes on one platform this.externalAccessories.forEach((accessory: ControlUnit) => { this.api.publishExternalAccessories("HarmonyMatrixPlatform", [accessory.platformAccessory]); }) @@ -126,9 +134,6 @@ class HarmonyMatrixPlatform { 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({ dataProvider: dataProvider, displayName: configControlUnit["DisplayName"],