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 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.
*/

View File

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

View File

@ -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<IDevice>, controlUnitName: string): Array<IDevice> {
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));

View File

@ -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 = "";

View File

@ -14,6 +14,9 @@ export interface Output {
outputDevice: string,
}
/**
* Data model to hold matrix information.
*/
export class Matrix {
private _inputs: Array<Input> = [];
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 {
return (...args: any[]) => {
const onlyArgs: any[] = [];

View File

@ -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"],