Most (if not all) callbacks are registered

This commit is contained in:
watsonb8 2019-06-15 12:04:20 -04:00
parent 0ff8ce8622
commit ea5b7ba054
4 changed files with 111 additions and 7 deletions

View File

@ -13,6 +13,7 @@
},
{
"platform": "HarmonyHubMatrix",
"hubIp": "192.168.1.14",
"Matrix": {
"DeviceName": "Gefen AV Switch",
"Inputs": [

View File

@ -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<HAPNodeJS.Service> = [];
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;
}
}

View File

@ -0,0 +1,5 @@
class HarmonyDataProvider {
}
export default new HarmonyDataProvider();

27
src/Util/Callbackify.ts Normal file
View File

@ -0,0 +1,27 @@
export default function callbackify(func: (...args: any[]) => Promise<any>): 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))
}
}