diff --git a/src/Accessories/ControlUnit.ts b/src/Accessories/ControlUnit.ts index eecf3c1..7c4af58 100644 --- a/src/Accessories/ControlUnit.ts +++ b/src/Accessories/ControlUnit.ts @@ -123,7 +123,6 @@ export class ControlUnit implements IAccessory { * Event handler for SET active characteristic */ private onSetAccessoryActive = async (value: any) => { - this.log(`set active + ${value}`); switch (value) { case 0: this.dataProvider.powerOff(this.name); break; //Turn on with first activity @@ -135,22 +134,21 @@ export class ControlUnit implements IAccessory { * Event handler for GET active characteristic */ private onGetAccessoryActive = async () => { - this.log(`get active`) - //@ts-ignore return this.dataProvider.getIsActive ? Characteristic.Active.Active : Characteristic.Active.Inactive } - //TODO + /** + * Event handler for SET remote key + */ private onSetRemoteKey = async (key: any) => { - this.log(`set remote key + ${key}`); + this.dataProvider.sendKeyPress(this.name, key); } /** * Event handler for SET active identifier characteristic */ private onSetActiveIdentifier = async (identifier: any) => { - this.log(`set active identifier + ${identifier}`); this.dataProvider.startActivity(this.name, this.activities[identifier]); } @@ -158,7 +156,6 @@ export class ControlUnit implements IAccessory { * Event handler for GET active identifier characteristic */ private onGetActiveIdentifier = async () => { - this.log(`get active identifier`); let currentActivity: Activity = this.dataProvider.getIsActive(this.name)!; let identifier: number = 0; if (currentActivity) { @@ -202,7 +199,6 @@ export class ControlUnit implements IAccessory { * Event handler for SET volume characteristic */ private onSetVolumeSelector = async (value: any) => { - this.log(`set volume + ${value}`); switch (value) { case 0: this.dataProvider.volumeUp(this.name); break; case 1: this.dataProvider.volumeDown(this.name); break; @@ -237,7 +233,6 @@ export class ControlUnit implements IAccessory { private configureInputSourceService(): void { let inputs: Array = []; this.activities.forEach((activity: Activity, index: number) => { - this.log(activity.displayName); let inputService = new Service.InputSource(activity.displayName, 'activity' + activity.displayName); inputService .setCharacteristic(Characteristic.Identifier, index) @@ -277,7 +272,6 @@ export class ControlUnit implements IAccessory { * Called by homebridge to gather services for this accessory. */ getServices(): Array { - this.log(`${this.displayName}`) let services: Array = [this.televisionService!, this.televisionSpeakerService!]; this.inputServices.forEach((service: HAPNodeJS.Service | undefined) => { services.push(service!); diff --git a/src/DataProviders/HarmonyDataProvider.ts b/src/DataProviders/HarmonyDataProvider.ts index 48b53c0..f3118b1 100644 --- a/src/DataProviders/HarmonyDataProvider.ts +++ b/src/DataProviders/HarmonyDataProvider.ts @@ -3,6 +3,8 @@ import { DeviceSetupItem } from "../Models/DeviceSetupItem"; import { threadId } from "worker_threads"; import { Input, Matrix, Output } from "../Models/Matrix"; +let Characteristic: HAPNodeJS.Characteristic; + const Harmony = require("harmony-websocket"); interface IDevice { id: string, @@ -17,6 +19,22 @@ 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, @@ -53,7 +71,11 @@ class HarmonyDataProvider { } public powerOn = async (controlUnitName: string, activity: Activity) => { - await this.startActivity(controlUnitName, activity); + //Only power on if not alread on + let currentActivity = this.states[controlUnitName] ? this.states[controlUnitName]!.currentActivity : undefined; + if (!currentActivity) { + await this.startActivity(controlUnitName, activity); + } } public powerOff = async (controlUnitName: string) => { @@ -78,6 +100,7 @@ class HarmonyDataProvider { } public startActivity = async (controlUnitName: string, activity: Activity) => { + this.log(`Starting activity ${activity.displayName} for controlUnit: ${controlUnitName}`) let lastActivity: Activity | undefined = undefined; if (this.states[controlUnitName]) { lastActivity = this.states[controlUnitName]!.currentActivity; @@ -189,8 +212,62 @@ class HarmonyDataProvider { } } - public sendKeyPress = async (controlUnitName: string) => { + 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; + } + } + if (device && device.supportsCommand(commandName)) { + this.sendCommand(device.getCommand(commandName)); + } + } } public getIsActive(controlUnitName: string): Activity | undefined { @@ -217,7 +294,6 @@ class HarmonyDataProvider { deviceCommands.forEach((command: any) => { commands[command.label] = command.action; }); - self.devices[dev.label] = { id: dev.id, name: dev.label,