External devices (remote control support) building blocks added.

This commit is contained in:
watsonb8 2019-06-16 21:12:57 -04:00
parent 06fb2009bc
commit e49af64a18
2 changed files with 66 additions and 6 deletions

View File

@ -7,6 +7,7 @@ import HarmonyDataProvider from '../DataProviders/HarmonyDataProvider';
let Service: HAPNodeJS.Service; let Service: HAPNodeJS.Service;
let Characteristic: HAPNodeJS.Characteristic; let Characteristic: HAPNodeJS.Characteristic;
let Api: any; let Api: any;
let homebridge: any;
export interface IControlUnitProps { export interface IControlUnitProps {
dataProvider: HarmonyDataProvider, dataProvider: HarmonyDataProvider,
@ -14,6 +15,7 @@ export interface IControlUnitProps {
activities: Array<Activity>, activities: Array<Activity>,
api: any, api: any,
log: any, log: any,
homebridge: any,
} }
/** /**
@ -37,6 +39,8 @@ export class ControlUnit implements IAccessory {
private activities: Array<Activity> = []; private activities: Array<Activity> = [];
private dataProvider: HarmonyDataProvider; private dataProvider: HarmonyDataProvider;
public platformAccessory: any;
/** /**
* Constructor * Constructor
* @param props Input properties * @param props Input properties
@ -53,12 +57,28 @@ export class ControlUnit implements IAccessory {
this.activities = props.activities; this.activities = props.activities;
this.dataProvider = props.dataProvider; this.dataProvider = props.dataProvider;
homebridge = props.homebridge;
this.platformAccessory = new homebridge.platformAccessory(this.name, this.generateUUID(), homebridge.hap.Accessory.Categories.TELEVISION);
//Configure services //Configure services
this.configureTvService(); this.configureTvService();
this.configureTvSpeakerService(); this.configureTvSpeakerService();
// this.configureAccessoryInformation(); // this.configureAccessoryInformation();
this.configureInputSourceService(); this.configureInputSourceService();
//Configure external services
this.getServices().forEach(service => {
try {
this.platformAccessory.addService(service);
} catch (error) { }
//@ts-ignore
if (service.linked) {
//@ts-ignore
this.televisionService!.addLinkedService(service);
}
});
} }
/************* /*************
* *
@ -106,6 +126,7 @@ export class ControlUnit implements IAccessory {
this.log(`set active + ${value}`); this.log(`set active + ${value}`);
switch (value) { switch (value) {
case 0: this.dataProvider.powerOff(this.name); break; case 0: this.dataProvider.powerOff(this.name); break;
//Turn on with first activity
case 1: this.dataProvider.powerOn(this.name, this.activities[0]); break; case 1: this.dataProvider.powerOn(this.name, this.activities[0]); break;
} }
} }
@ -162,9 +183,9 @@ export class ControlUnit implements IAccessory {
); );
this.televisionSpeakerService.setCharacteristic(Characteristic.Name, this.displayName); this.televisionSpeakerService.setCharacteristic(Characteristic.Name, this.displayName);
//@ts-ignore //@ts-ignore
this.televisionSpeakerService.setCharacteristic(Characteristic.Active, Characteristic.Active.Active); this.televisionSpeakerService.setCharacteristic(Characteristic.Active, Characteristic.Active.ACTIVE);
//@ts-ignore //@ts-ignore
this.televisionSpeakerService.setCharacteristic(Characteristic.VolumeControlType, Characteristic.VolumeControlType.Absolute); this.televisionSpeakerService.setCharacteristic(Characteristic.VolumeControlType, Characteristic.VolumeControlType.ABSOLUTE);
this.televisionSpeakerService.subtype = this.displayName + "Volume"; this.televisionSpeakerService.subtype = this.displayName + "Volume";
if (this.televisionService) { if (this.televisionService) {
//@ts-ignore //@ts-ignore
@ -232,6 +253,18 @@ export class ControlUnit implements IAccessory {
this.inputServices = inputs; this.inputServices = inputs;
} }
private generateUUID(): string { // Public Domain/MIT
var d = new Date().getTime();
if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
d += performance.now(); //use high-precision timer if available
}
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}
/** /**

View File

@ -4,7 +4,12 @@ import { DeviceSetupItem } from "./Models/DeviceSetupItem";
import { Input, Output, Matrix } from "./Models/Matrix"; import { Input, Output, Matrix } from "./Models/Matrix";
import HarmonyDataProvider from "./DataProviders/HarmonyDataProvider"; import HarmonyDataProvider from "./DataProviders/HarmonyDataProvider";
let Accessory: any;
let Homebridge: any;
export default function (homebridge: any) { export default function (homebridge: any) {
Homebridge = homebridge;
Accessory = homebridge.platformAccessory;
homebridge.registerPlatform( homebridge.registerPlatform(
'homebridge-harmony-watson', 'homebridge-harmony-watson',
'HarmonyHubMatrix', 'HarmonyHubMatrix',
@ -17,12 +22,23 @@ class HarmonyMatrixPlatform {
log: any = {}; log: any = {};
config: any = {}; config: any = {};
api: any; api: any;
externalAccessories: Array<any> = [];
constructor(log: any, config: any, api: any) { constructor(log: any, config: any, api: any) {
this.log = log; this.log = log;
this.config = config; this.config = config;
this.api = api; this.api = api;
this.log('INFO - Registering Harmony Matrix Platform'); this.log('INFO - Registering Harmony Matrix Platform');
this.api.on('didFinishLaunching', this.didFinishLaunching.bind(this));
}
didFinishLaunching() {
this.log(`Publishing external accessories`);
this.externalAccessories.forEach((accessory: ControlUnit) => {
this.api.publishExternalAccessories("HarmonyMatrixPlatform", [accessory.platformAccessory]);
})
} }
/** /**
@ -75,7 +91,7 @@ class HarmonyMatrixPlatform {
let dataProvider = new HarmonyDataProvider({ let dataProvider = new HarmonyDataProvider({
hubAddress: hubIp, hubAddress: hubIp,
matrix: matrix, matrix: matrix,
log: this.log, log: this.log
}); });
//Parse control units //Parse control units
@ -110,16 +126,27 @@ class HarmonyMatrixPlatform {
this.log(`INFO - Added activity '${configActivity["DisplayName"]}'`); this.log(`INFO - Added activity '${configActivity["DisplayName"]}'`);
}); });
//Add control unit // let accessory = new Accessory(configControlUnit["DisplayName"],
controlUnits.push(new ControlUnit({ // Homebridge.hap.uuid.generate(configControlUnit["DisplayName"], Homebridge.hap.Accessory.Categories.TELEVISION));
let controlUnit: ControlUnit = new ControlUnit({
dataProvider: dataProvider, dataProvider: dataProvider,
displayName: configControlUnit["DisplayName"], displayName: configControlUnit["DisplayName"],
api: this.api, api: this.api,
log: this.log, log: this.log,
activities: activities, activities: activities,
})); homebridge: Homebridge
});
//@ts-ignore
let accessory = controlUnit as homebridge.platformAccessory;
//Add control unit
controlUnits.push(accessory);
this.log(`INFO - Added ControlUnit`); this.log(`INFO - Added ControlUnit`);
}); });
this.externalAccessories = controlUnits;
callback(controlUnits); callback(controlUnits);
} }
} }