From ea5b7ba054063d3edbf955aafcefb7e9559bf71e Mon Sep 17 00:00:00 2001 From: watsonb8 Date: Sat, 15 Jun 2019 12:04:20 -0400 Subject: [PATCH] Most (if not all) callbacks are registered --- config.json | 1 + src/Accessories/ControlUnit.ts | 85 ++++++++++++++++++++++-- src/DataProviders/HarmonyDataProvider.ts | 5 ++ src/Util/Callbackify.ts | 27 ++++++++ 4 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 src/DataProviders/HarmonyDataProvider.ts create mode 100644 src/Util/Callbackify.ts diff --git a/config.json b/config.json index bf340fa..735d802 100644 --- a/config.json +++ b/config.json @@ -13,6 +13,7 @@ }, { "platform": "HarmonyHubMatrix", + "hubIp": "192.168.1.14", "Matrix": { "DeviceName": "Gefen AV Switch", "Inputs": [ diff --git a/src/Accessories/ControlUnit.ts b/src/Accessories/ControlUnit.ts index c10f221..8a00cae 100644 --- a/src/Accessories/ControlUnit.ts +++ b/src/Accessories/ControlUnit.ts @@ -1,6 +1,7 @@ import { Activity } from '../Models/Activity'; import { Matrix } from '../Models/Matrix'; import { IAccessory } from './IAccessory'; +import callbackify from '../Util/Callbackify'; let Service: HAPNodeJS.Service; let Characteristic: HAPNodeJS.Characteristic; @@ -57,6 +58,11 @@ export class ControlUnit implements IAccessory { // this.configureAccessoryInformation(); this.configureInputSourceService(); } + /************* + * + * Tv Service + * + *************/ /** * Configure television service @@ -72,8 +78,56 @@ export class ControlUnit implements IAccessory { this.televisionService.setCharacteristic(Characteristic.SleepDiscoveryMode, Characteristic.SleepDiscoveryMode.ALWAYS_DISCOVERABLE); this.televisionService.setCharacteristic(Characteristic.ActiveIdentifier, 1); this.televisionService.setCharacteristic(Characteristic.Active, false); + + //setup listeners + this.televisionService.getCharacteristic(Characteristic.Active) + //@ts-ignore + .on("set", callbackify(this.onSetAccessoryActive)) + //@ts-ignore + .on("get", callbackify(this.onGetAccessoryActive)); + + this.televisionService.getCharacteristic(Characteristic.RemoteKey) + //@ts-ignore + .on("set", callbackify(this.onSetRemoteKey)); + + this.televisionService.getCharacteristic(Characteristic.ActiveIdentifier) + //@ts-ignore + .on("set", callbackify(this.onSetActiveIdentifier)) + //@ts-ignore + .on("get", callbackify(this.onGetActiveIdentifier)); } + //TODO + private onSetAccessoryActive = async (value: any) => { + this.log(`set active + ${value}`); + } + + //TODO + private onGetAccessoryActive = async () => { + this.log(`get active`) + return Characteristic.Active.Active; + } + + //TODO + private onSetRemoteKey = async (key: any) => { + this.log(`set remote key + ${key}`); + } + + private onSetActiveIdentifier = async (identifier: any) => { + this.log(`set active identifier + ${identifier}`); + } + + private onGetActiveIdentifier = async () => { + this.log(`get active identifier`); + return 1; + } + + /****************** + * + * Speaker Service + * + *****************/ + /** * Configure Speaker Service */ @@ -92,8 +146,24 @@ export class ControlUnit implements IAccessory { //@ts-ignore this.televisionService.addLinkedService(this.televisionSpeakerService); } + + //Setup listeners + this.televisionSpeakerService.getCharacteristic(Characteristic.VolumeSelector) + //@ts-ignore + .on("set", callbackify(this.onSetVolumeSelector)); } + //TODO + private onSetVolumeSelector = async (value: any) => { + this.log(`set volume + ${value}`); + } + + /********************* + * + * Information Service + * + ********************/ + /** * Configure information service */ @@ -104,6 +174,12 @@ export class ControlUnit implements IAccessory { .setCharacteristic(Characteristic.Model, 'Heater-Cooler') } + /***************** + * + * Input services + * + *****************/ + /** * Configure input service */ @@ -111,7 +187,6 @@ export class ControlUnit implements IAccessory { let inputs: Array = []; this.activities.forEach((activity: Activity, index: number) => { this.log(activity.displayName); - let uuid: string = Api.hap.uuid.generate(activity.displayName + this.rand(1, 10).toString()); let inputService = new Service.InputSource(activity.displayName, 'activity' + activity.displayName); inputService .setCharacteristic(Characteristic.Identifier, index) @@ -133,6 +208,8 @@ export class ControlUnit implements IAccessory { this.inputServices = inputs; } + + /** * Called by homebridge to gather services for this accessory. */ @@ -144,10 +221,4 @@ export class ControlUnit implements IAccessory { }); return (services); } - - private rand(min: number, max: number): number { - var random = Math.floor(Math.random() * (+max - +min)) + +min; - - return random; - } } \ No newline at end of file diff --git a/src/DataProviders/HarmonyDataProvider.ts b/src/DataProviders/HarmonyDataProvider.ts new file mode 100644 index 0000000..9947787 --- /dev/null +++ b/src/DataProviders/HarmonyDataProvider.ts @@ -0,0 +1,5 @@ +class HarmonyDataProvider { + +} + +export default new HarmonyDataProvider(); \ No newline at end of file diff --git a/src/Util/Callbackify.ts b/src/Util/Callbackify.ts new file mode 100644 index 0000000..f96c852 --- /dev/null +++ b/src/Util/Callbackify.ts @@ -0,0 +1,27 @@ + + +export default function callbackify(func: (...args: any[]) => Promise): Function { + return (...args: any[]) => { + const onlyArgs: any[] = []; + let maybeCallback: Function | null = null; + + for (const arg of args) { + if (typeof arg === 'function') { + maybeCallback = arg; + break; + } + + onlyArgs.push(arg); + } + + if (!maybeCallback) { + throw new Error("Missing callback parameter!"); + } + + const callback = maybeCallback; + + func(...onlyArgs) + .then((data: any) => callback(null, data)) + .catch((err: any) => callback(err)) + } +} \ No newline at end of file