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

@ -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));