Compare commits
35 Commits
Author | SHA1 | Date | |
---|---|---|---|
31c0a0856a | |||
7907497360 | |||
3ac6ffb8a3 | |||
6f7fc4c2af | |||
97c0f625db | |||
ec0c857c97 | |||
ba1a1685ae | |||
5ddd1e1af0 | |||
e9693435ce | |||
a8fddae8c5 | |||
3759d91d1f | |||
b004e3a966 | |||
0b08302656 | |||
7ec92f28cb | |||
3a1428aa47 | |||
672362a9a4 | |||
020a2fc240 | |||
588205e507 | |||
80ac6423e5 | |||
fb260c5532 | |||
f903c40f9c | |||
5c69e7b11f | |||
a67674b3d3 | |||
f682c0e380 | |||
14312908bf | |||
152d73e9f9 | |||
727c8ceba7 | |||
39f4a7c1bc | |||
73cba09edb | |||
9b48614dd1 | |||
04024d565d | |||
7638644935 | |||
f0eb57e4dc | |||
0ea4188487 | |||
8bb65b5b3c |
16
.vscode/launch.json
vendored
Normal file
16
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "Launch Program",
|
||||
"preLaunchTask": "build and install",
|
||||
"program": "/Users/brandonwatson/.npm-global/bin/homebridge",
|
||||
"sourceMaps": true,
|
||||
}
|
||||
]
|
||||
}
|
17
.vscode/tasks.json
vendored
Normal file
17
.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
// See https://go.microsoft.com/fwlink/?LinkId=733558
|
||||
// for the documentation about the tasks.json format
|
||||
"version": "2.0.0",
|
||||
"tasks": [
|
||||
{
|
||||
"type": "npm",
|
||||
"script": "build",
|
||||
"problemMatcher": []
|
||||
},
|
||||
{
|
||||
"type": "shell",
|
||||
"label": "build and install",
|
||||
"command": "npm run build&&sudo npm install -g --unsafe-perm ."
|
||||
}
|
||||
]
|
||||
}
|
36
config.json
36
config.json
@ -1,7 +1,7 @@
|
||||
{
|
||||
"bridge": {
|
||||
"name": "Homebridge",
|
||||
"username": "CC:22:3D:E3:CE:42",
|
||||
"username": "CC:22:3D:E3:CE:74",
|
||||
"port": 51826,
|
||||
"pin": "031-45-154"
|
||||
},
|
||||
@ -58,11 +58,11 @@
|
||||
"DeviceSetupList": [
|
||||
{
|
||||
"DeviceName": "LG TV",
|
||||
"Input": "HDMI1"
|
||||
"Input": "Hdmi1"
|
||||
},
|
||||
{
|
||||
"DeviceName": "JVC AV Receiver",
|
||||
"Input": "DVD"
|
||||
"Input": "Dvd"
|
||||
},
|
||||
{
|
||||
"DeviceName": "Chromecast",
|
||||
@ -83,11 +83,11 @@
|
||||
},
|
||||
{
|
||||
"DeviceName": "LG TV",
|
||||
"Input": "HDMI1"
|
||||
"Input": "Hdmi1"
|
||||
},
|
||||
{
|
||||
"DeviceName": "JVC AV Receiver",
|
||||
"Input": "DVD"
|
||||
"Input": "Dvd"
|
||||
}
|
||||
],
|
||||
"VolumeDevice": "JVC AV Receiver",
|
||||
@ -104,11 +104,11 @@
|
||||
},
|
||||
{
|
||||
"DeviceName": "LG TV",
|
||||
"Input": "HDMI1"
|
||||
"Input": "Hdmi1"
|
||||
},
|
||||
{
|
||||
"DeviceName": "JVC AV Receiver",
|
||||
"Input": "DVD"
|
||||
"Input": "Dvd"
|
||||
}
|
||||
],
|
||||
"VolumeDevice": "JVC AV Receiver",
|
||||
@ -125,11 +125,11 @@
|
||||
},
|
||||
{
|
||||
"DeviceName": "LG TV",
|
||||
"Input": "HDMI1"
|
||||
"Input": "Hdmi1"
|
||||
},
|
||||
{
|
||||
"DeviceName": "JVC AV Receiver",
|
||||
"Input": "DVD"
|
||||
"Input": "Dvd"
|
||||
}
|
||||
],
|
||||
"VolumeDevice": "JVC AV Receiver",
|
||||
@ -142,7 +142,7 @@
|
||||
"DeviceSetupList": [
|
||||
{
|
||||
"DeviceName": "JVC AV Receiver",
|
||||
"Input": "DVD"
|
||||
"Input": "CD"
|
||||
}
|
||||
],
|
||||
"VolumeDevice": "JVC AV Receiver",
|
||||
@ -160,7 +160,7 @@
|
||||
"DeviceSetupList": [
|
||||
{
|
||||
"DeviceName": "Vizio TV",
|
||||
"Input": "HDMI1"
|
||||
"Input": "Hdmi1"
|
||||
},
|
||||
{
|
||||
"DeviceName": "Harman Kardon AV Receiver",
|
||||
@ -185,7 +185,7 @@
|
||||
},
|
||||
{
|
||||
"DeviceName": "Vizio TV",
|
||||
"Input": "HDMI1"
|
||||
"Input": "Hdmi1"
|
||||
},
|
||||
{
|
||||
"DeviceName": "Harman Kardon AV Receiver",
|
||||
@ -206,11 +206,11 @@
|
||||
},
|
||||
{
|
||||
"DeviceName": "Vizio TV",
|
||||
"Input": "HDMI1"
|
||||
"Input": "Hdmi1"
|
||||
},
|
||||
{
|
||||
"DeviceName": "Harman Kardon AV Receiver",
|
||||
"Input": "DVD"
|
||||
"Input": "Dvd"
|
||||
}
|
||||
],
|
||||
"VolumeDevice": "Harman Kardon AV Receiver",
|
||||
@ -227,7 +227,7 @@
|
||||
},
|
||||
{
|
||||
"DeviceName": "Vizio TV",
|
||||
"Input": "HDMI1"
|
||||
"Input": "Hdmi1"
|
||||
},
|
||||
{
|
||||
"DeviceName": "Harman Kardon AV Receiver",
|
||||
@ -253,7 +253,7 @@
|
||||
},
|
||||
{
|
||||
"DeviceName": "Westinghouse TV",
|
||||
"Input": "HDMI1"
|
||||
"Input": "Hdmi1"
|
||||
}
|
||||
],
|
||||
"VolumeDevice": "Westinghouse TV",
|
||||
@ -270,7 +270,7 @@
|
||||
},
|
||||
{
|
||||
"DeviceName": "Westinghouse TV",
|
||||
"Input": "HDMI1"
|
||||
"Input": "Hdmi1"
|
||||
}
|
||||
],
|
||||
"VolumeDevice": "Westinghouse TV",
|
||||
@ -283,7 +283,7 @@
|
||||
"DeviceSetupList": [
|
||||
{
|
||||
"DeviceName": "Westinghouse TV",
|
||||
"Input": "HDMI1"
|
||||
"Input": "Hdmi1"
|
||||
},
|
||||
{
|
||||
"DeviceName": "Microsoft Xbox One",
|
||||
|
22
deploy.sh
Executable file
22
deploy.sh
Executable file
@ -0,0 +1,22 @@
|
||||
#!/bin/bash
|
||||
remote_user="bmw"
|
||||
remote_server="192.168.1.31"
|
||||
deploy_location="/home/bmw/homebridge-harmony-control"
|
||||
homebridge_location="/var/lib/homebridge/"
|
||||
|
||||
#build
|
||||
tsc --build
|
||||
#copy files to remote machine
|
||||
scp -r bin $remote_user@$remote_server:$deploy_location
|
||||
scp package.json $remote_user@$remote_server:$deploy_location
|
||||
|
||||
#install package
|
||||
ssh -t $remote_user@$remote_server "sudo npm install -g --unsafe-perm $deploy_location"
|
||||
|
||||
#restart service
|
||||
ssh -t
|
||||
ssh -t $remote_user@$remote_server "sudo systemctl restart homebridge.service"
|
||||
ssh -t $remote_user@$remote_server "sudo systemctl status homebridge.service"
|
||||
|
||||
echo done
|
||||
exit
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "homebridge-harmony-control",
|
||||
"version": "1.0.0",
|
||||
"version": "1.0.7",
|
||||
"description": "Homebridge platform to control smart home equipment by room.",
|
||||
"main": "bin/index.js",
|
||||
"scripts": {
|
||||
|
@ -1,5 +1,4 @@
|
||||
import { Activity } from '../Models/Activity';
|
||||
import { Matrix } from '../Models/Matrix';
|
||||
import * as Config from "../Models/Config"
|
||||
import { IAccessory } from './IAccessory';
|
||||
import callbackify from '../Util/Callbackify';
|
||||
import HarmonyDataProvider from '../DataProviders/HarmonyDataProvider';
|
||||
@ -9,10 +8,29 @@ let Characteristic: HAPNodeJS.Characteristic;
|
||||
let Api: any;
|
||||
let homebridge: any;
|
||||
|
||||
/**
|
||||
* Enum describing remote key presses from homebridge.
|
||||
*/
|
||||
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,
|
||||
}
|
||||
|
||||
export interface IControlUnitProps {
|
||||
dataProvider: HarmonyDataProvider,
|
||||
displayName: string,
|
||||
activities: Array<Activity>,
|
||||
activities: Array<Config.IActivity>,
|
||||
api: any,
|
||||
log: any,
|
||||
homebridge: any,
|
||||
@ -22,24 +40,21 @@ export interface IControlUnitProps {
|
||||
* ControlUnit accessory
|
||||
*/
|
||||
export class ControlUnit implements IAccessory {
|
||||
//Required by homebridge
|
||||
name: string = "";
|
||||
|
||||
|
||||
//fields
|
||||
private log: any = {};
|
||||
private displayName: string = "";
|
||||
|
||||
//Service fields
|
||||
private televisionService: HAPNodeJS.Service | undefined;
|
||||
private televisionSpeakerService: HAPNodeJS.Service | undefined;
|
||||
private informationService: HAPNodeJS.Service | undefined;
|
||||
private inputServices: Array<HAPNodeJS.Service | undefined> = [];
|
||||
private _televisionService: HAPNodeJS.Service | undefined;
|
||||
private _televisionSpeakerService: HAPNodeJS.Service | undefined;
|
||||
private _infoService: HAPNodeJS.Service;
|
||||
private _inputServices: Array<HAPNodeJS.Service | undefined> = [];
|
||||
|
||||
//Harmony fields
|
||||
private activities: Array<Activity> = [];
|
||||
private dataProvider: HarmonyDataProvider;
|
||||
|
||||
public platformAccessory: any;
|
||||
private _activities: Array<Config.IActivity> = [];
|
||||
private _dataProvider: HarmonyDataProvider;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
@ -51,19 +66,28 @@ export class ControlUnit implements IAccessory {
|
||||
Api = props.api;
|
||||
Service = props.api.hap.Service;
|
||||
Characteristic = props.api.hap.Characteristic;
|
||||
this.name = props.displayName;
|
||||
this.displayName = props.displayName;
|
||||
this.name = this.displayName;
|
||||
|
||||
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);
|
||||
this.platformAccessory = new homebridge.platformAccessory(this.displayName, this.generateUUID(), homebridge.hap.Accessory.Categories.TELEVISION);
|
||||
|
||||
//@ts-ignore
|
||||
this._infoService = new Service.AccessoryInformation();
|
||||
this._infoService.setCharacteristic(Characteristic.Manufacturer, "Brandon Watson")
|
||||
this._infoService.setCharacteristic(Characteristic.Model, "Matrix Output Television")
|
||||
this._infoService.setCharacteristic(Characteristic.SerialNumber, "123-456-789");
|
||||
|
||||
//Configure services
|
||||
this.configureTvService();
|
||||
|
||||
this.configureTvSpeakerService();
|
||||
|
||||
|
||||
// this.configureAccessoryInformation();
|
||||
this.configureInputSourceService();
|
||||
|
||||
@ -76,10 +100,15 @@ export class ControlUnit implements IAccessory {
|
||||
//@ts-ignore
|
||||
if (service.linked) {
|
||||
//@ts-ignore
|
||||
this.televisionService!.addLinkedService(service);
|
||||
this._televisionService!.addLinkedService(service);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//Required by homebridge
|
||||
name: string = "";
|
||||
public platformAccessory: any;
|
||||
|
||||
/*************
|
||||
*
|
||||
* Tv Service
|
||||
@ -90,29 +119,31 @@ export class ControlUnit implements IAccessory {
|
||||
* Configure television service
|
||||
*/
|
||||
private configureTvService(): void {
|
||||
this.televisionService = new Service.Television(
|
||||
this._televisionService = new Service.Television(
|
||||
this.displayName,
|
||||
'tvService'
|
||||
)
|
||||
|
||||
this.televisionService.setCharacteristic(Characteristic.ConfiguredName, this.displayName);
|
||||
this._televisionService.setCharacteristic(Characteristic.ConfiguredName, this.displayName);
|
||||
//@ts-ignore
|
||||
this.televisionService.setCharacteristic(Characteristic.SleepDiscoveryMode, Characteristic.SleepDiscoveryMode.ALWAYS_DISCOVERABLE);
|
||||
this.televisionService.setCharacteristic(Characteristic.ActiveIdentifier, 1);
|
||||
this.televisionService.setCharacteristic(Characteristic.Active, false);
|
||||
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)
|
||||
this._televisionService.getCharacteristic(Characteristic.Active)
|
||||
//@ts-ignore
|
||||
.on("set", callbackify(this.onSetAccessoryActive))
|
||||
//@ts-ignore
|
||||
.on("get", callbackify(this.onGetAccessoryActive));
|
||||
|
||||
this.televisionService.getCharacteristic(Characteristic.RemoteKey)
|
||||
//Set remote characteristics if is external
|
||||
this._televisionService.getCharacteristic(Characteristic.RemoteKey)
|
||||
//@ts-ignore
|
||||
.on("set", callbackify(this.onSetRemoteKey));
|
||||
|
||||
this.televisionService.getCharacteristic(Characteristic.ActiveIdentifier)
|
||||
|
||||
this._televisionService.getCharacteristic(Characteristic.ActiveIdentifier)
|
||||
//@ts-ignore
|
||||
.on("set", callbackify(this.onSetActiveIdentifier))
|
||||
//@ts-ignore
|
||||
@ -124,9 +155,9 @@ export class ControlUnit implements IAccessory {
|
||||
*/
|
||||
private onSetAccessoryActive = async (value: any) => {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -135,31 +166,31 @@ export class ControlUnit implements IAccessory {
|
||||
*/
|
||||
private onGetAccessoryActive = async () => {
|
||||
//@ts-ignore
|
||||
return this.dataProvider.getIsActive ? Characteristic.Active.Active : Characteristic.Active.Inactive
|
||||
return this._dataProvider.getIsActive(this.name) ? Characteristic.Active.Active : Characteristic.Active.Inactive
|
||||
}
|
||||
|
||||
/**
|
||||
* Event handler for SET remote key
|
||||
*/
|
||||
private onSetRemoteKey = async (key: any) => {
|
||||
this.dataProvider.sendKeyPress(this.name, key);
|
||||
this._dataProvider.sendKeyPress(this.name, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Event handler for SET active identifier characteristic
|
||||
*/
|
||||
private onSetActiveIdentifier = async (identifier: any) => {
|
||||
this.dataProvider.startActivity(this.name, this.activities[identifier]);
|
||||
this._dataProvider.startActivity(this.name, this._activities[identifier]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Event handler for GET active identifier characteristic
|
||||
*/
|
||||
private onGetActiveIdentifier = async () => {
|
||||
let currentActivity: Activity = this.dataProvider.getIsActive(this.name)!;
|
||||
let currentActivity: Config.IActivity = this._dataProvider.getIsActive(this.name)!;
|
||||
let identifier: number = 0;
|
||||
if (currentActivity) {
|
||||
identifier = this.activities.findIndex(e => e.displayName === currentActivity.displayName);
|
||||
identifier = this._activities.findIndex(e => e.DisplayName === currentActivity.DisplayName);
|
||||
}
|
||||
return identifier;
|
||||
}
|
||||
@ -174,23 +205,23 @@ export class ControlUnit implements IAccessory {
|
||||
* Configure Speaker Service
|
||||
*/
|
||||
private configureTvSpeakerService(): void {
|
||||
this.televisionSpeakerService = new Service.TelevisionSpeaker(
|
||||
this._televisionSpeakerService = new Service.TelevisionSpeaker(
|
||||
this.displayName,
|
||||
'tvSpeakerService'
|
||||
);
|
||||
this.televisionSpeakerService.setCharacteristic(Characteristic.Name, this.displayName);
|
||||
this._televisionSpeakerService.setCharacteristic(Characteristic.Name, this.displayName);
|
||||
//@ts-ignore
|
||||
this.televisionSpeakerService.setCharacteristic(Characteristic.Active, Characteristic.Active.ACTIVE);
|
||||
this._televisionSpeakerService.setCharacteristic(Characteristic.Active, Characteristic.Active.ACTIVE);
|
||||
//@ts-ignore
|
||||
this.televisionSpeakerService.setCharacteristic(Characteristic.VolumeControlType, Characteristic.VolumeControlType.ABSOLUTE);
|
||||
this.televisionSpeakerService.subtype = this.displayName + "Volume";
|
||||
if (this.televisionService) {
|
||||
this._televisionSpeakerService.setCharacteristic(Characteristic.VolumeControlType, Characteristic.VolumeControlType.ABSOLUTE);
|
||||
this._televisionSpeakerService.subtype = this.displayName + "Volume";
|
||||
if (this._televisionService) {
|
||||
//@ts-ignore
|
||||
this.televisionService.addLinkedService(this.televisionSpeakerService);
|
||||
this._televisionService.addLinkedService(this._televisionSpeakerService);
|
||||
}
|
||||
|
||||
//Setup listeners
|
||||
this.televisionSpeakerService.getCharacteristic(Characteristic.VolumeSelector)
|
||||
this._televisionSpeakerService.getCharacteristic(Characteristic.VolumeSelector)
|
||||
//@ts-ignore
|
||||
.on("set", callbackify(this.onSetVolumeSelector));
|
||||
}
|
||||
@ -200,27 +231,11 @@ export class ControlUnit implements IAccessory {
|
||||
*/
|
||||
private onSetVolumeSelector = async (value: any) => {
|
||||
switch (value) {
|
||||
case 0: this.dataProvider.volumeUp(this.name); break;
|
||||
case 1: this.dataProvider.volumeDown(this.name); break;
|
||||
case 0: this._dataProvider.volumeUp(this.name); break;
|
||||
case 1: this._dataProvider.volumeDown(this.name); break;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************
|
||||
*
|
||||
* Information Service
|
||||
*
|
||||
********************/
|
||||
|
||||
/**
|
||||
* Configure information service
|
||||
*/
|
||||
private configureAccessoryInformation(): void {
|
||||
this.informationService = new Service.AccessoryInformation(this.displayName, 'information');
|
||||
this.informationService
|
||||
.setCharacteristic(Characteristic.Manufacturer, 'Loftux Carwings')
|
||||
.setCharacteristic(Characteristic.Model, 'Heater-Cooler')
|
||||
}
|
||||
|
||||
/*****************
|
||||
*
|
||||
* Input services
|
||||
@ -232,13 +247,13 @@ export class ControlUnit implements IAccessory {
|
||||
*/
|
||||
private configureInputSourceService(): void {
|
||||
let inputs: Array<HAPNodeJS.Service> = [];
|
||||
this.activities.forEach((activity: Activity, index: number) => {
|
||||
let inputService = new Service.InputSource(activity.displayName, 'activity' + activity.displayName);
|
||||
this._activities.forEach((activity: Config.IActivity, index: number) => {
|
||||
let inputService = new Service.InputSource(activity.DisplayName, 'activity' + activity.DisplayName);
|
||||
inputService
|
||||
.setCharacteristic(Characteristic.Identifier, index)
|
||||
.setCharacteristic(
|
||||
Characteristic.ConfiguredName,
|
||||
activity.displayName)
|
||||
activity.DisplayName)
|
||||
.setCharacteristic(
|
||||
Characteristic.IsConfigured,
|
||||
//@ts-ignore
|
||||
@ -248,10 +263,10 @@ export class ControlUnit implements IAccessory {
|
||||
.setCharacteristic(Characteristic.InputSourceType, Characteristic.InputSourceType.HDMI);
|
||||
|
||||
//@ts-ignore
|
||||
this.televisionService!.addLinkedService(inputService);
|
||||
this._televisionService!.addLinkedService(inputService);
|
||||
inputs.push(inputService);
|
||||
});
|
||||
this.inputServices = inputs;
|
||||
this._inputServices = inputs;
|
||||
}
|
||||
|
||||
private generateUUID(): string { // Public Domain/MIT
|
||||
@ -266,14 +281,12 @@ export class ControlUnit implements IAccessory {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Called by homebridge to gather services for this accessory.
|
||||
*/
|
||||
getServices(): Array<HAPNodeJS.Service> {
|
||||
let services: Array<HAPNodeJS.Service> = [this.televisionService!, this.televisionSpeakerService!];
|
||||
this.inputServices.forEach((service: HAPNodeJS.Service | undefined) => {
|
||||
let services: Array<HAPNodeJS.Service> = [this._infoService, this._televisionService!, this._televisionSpeakerService!];
|
||||
this._inputServices.forEach((service: HAPNodeJS.Service | undefined) => {
|
||||
services.push(service!);
|
||||
});
|
||||
return (services);
|
||||
|
155
src/Accessories/DeviceButton.ts
Normal file
155
src/Accessories/DeviceButton.ts
Normal file
@ -0,0 +1,155 @@
|
||||
import HarmonyDataProvider from "../DataProviders/HarmonyDataProvider";
|
||||
import { IDeviceButton } from "../Models/Config";
|
||||
import { IAccessory } from "./IAccessory";
|
||||
import { ICommand } from "../Models";
|
||||
import { HarmonyDevice } from "../Models/HarmonyDevice";
|
||||
|
||||
let Service: HAPNodeJS.Service;
|
||||
let Characteristic: HAPNodeJS.Characteristic;
|
||||
|
||||
export interface IDeviceButtonProps {
|
||||
dataProvider: HarmonyDataProvider,
|
||||
buttonName: string,
|
||||
displayName: string,
|
||||
deviceInfo: IDeviceButton,
|
||||
api: any,
|
||||
log: any,
|
||||
homebridge: any,
|
||||
}
|
||||
|
||||
export class DeviceButton implements IAccessory {
|
||||
private _api: any;
|
||||
private _homebridge: any;
|
||||
private _log: any = {};
|
||||
|
||||
//Service fields
|
||||
private _switchService: HAPNodeJS.Service;
|
||||
private _infoService: HAPNodeJS.Service;
|
||||
|
||||
private _buttonInfo: IDeviceButton;
|
||||
|
||||
private _dataProvider: HarmonyDataProvider;
|
||||
|
||||
private _device!: HarmonyDevice;
|
||||
|
||||
private _buttonState: boolean;
|
||||
|
||||
constructor(props: IDeviceButtonProps) {
|
||||
//Assign class variables
|
||||
this._log = props.log;
|
||||
this._api = props.api;
|
||||
Service = props.api.hap.Service;
|
||||
Characteristic = props.api.hap.Characteristic;
|
||||
this.name = props.displayName;
|
||||
this._homebridge = props.homebridge;
|
||||
|
||||
this._buttonInfo = props.deviceInfo;
|
||||
|
||||
this._dataProvider = props.dataProvider;
|
||||
|
||||
this._buttonState = false;
|
||||
|
||||
if (this._buttonInfo.NumberOfKeyPresses && this._buttonInfo.IsStateful) {
|
||||
throw new Error("A button cannot be stateful and be pressed more than once");
|
||||
}
|
||||
|
||||
//Assign default number of key presses
|
||||
if (!this._buttonInfo.NumberOfKeyPresses) {
|
||||
this._buttonInfo.NumberOfKeyPresses = 1;
|
||||
}
|
||||
|
||||
this.platformAccessory = new this._homebridge.platformAccessory(this.name, this.generateUUID(), this._homebridge.hap.Accessory.Categories.SWITCH);
|
||||
|
||||
//@ts-ignore
|
||||
this._infoService = new Service.AccessoryInformation();
|
||||
this._infoService.setCharacteristic(Characteristic.Manufacturer, "Brandon Watson")
|
||||
this._infoService.setCharacteristic(Characteristic.Model, "Device Button")
|
||||
this._infoService.setCharacteristic(Characteristic.SerialNumber, "123-456-789");
|
||||
|
||||
this._switchService = new Service.Switch(
|
||||
this.name,
|
||||
'switchService'
|
||||
)
|
||||
|
||||
this._switchService.getCharacteristic(Characteristic.On)
|
||||
//@ts-ignore
|
||||
.on("set", this.onSwitchSet)
|
||||
.updateValue(this._buttonState)
|
||||
.on("get", this.onSwitchGet);
|
||||
}
|
||||
|
||||
/**
|
||||
* Required by homebridge.
|
||||
*/
|
||||
public name: string;
|
||||
|
||||
public platformAccessory: any;
|
||||
|
||||
/**
|
||||
* Called by homebridge to gather services.
|
||||
*/
|
||||
public getServices = (): Array<HAPNodeJS.Service> => {
|
||||
return [this._infoService, this._switchService!];
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for switch set event
|
||||
* @param callback The callback function to call when complete
|
||||
*/
|
||||
private onSwitchSet = async (newState: boolean, callback: (error?: Error | null | undefined) => void) => {
|
||||
if (!this._buttonInfo.IsStateful && newState === this._buttonState) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
//Get device command if we don't have it
|
||||
if (!this._device) {
|
||||
this._device = this._dataProvider.getDeviceFromName(this._buttonInfo.DeviceName);
|
||||
}
|
||||
|
||||
//Execute command
|
||||
if (this._device) {
|
||||
//change state if stateful
|
||||
if (this._buttonInfo.IsStateful && this._buttonState != newState) {
|
||||
this._buttonState = newState
|
||||
await this._device.sendCommand(this._buttonInfo.ButtonName);
|
||||
} else if (!this._buttonInfo.IsStateful) {
|
||||
//Send the number of configured key presses
|
||||
for (let i = 0; i < this._buttonInfo.NumberOfKeyPresses; i++) {
|
||||
await this._device.sendCommand(this._buttonInfo.ButtonName);
|
||||
}
|
||||
this._switchService.getCharacteristic(Characteristic.On).updateValue(false);
|
||||
return callback(new Error("Normal Response"));
|
||||
}
|
||||
}
|
||||
return callback();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for switch get event
|
||||
* @param callback The callback function to call when complete
|
||||
*/
|
||||
private onSwitchGet = (callback: (error: Error | null, value: boolean) => void) => {
|
||||
//Only return state if button is stateful
|
||||
if (this._buttonInfo.IsStateful) {
|
||||
return callback(null, this._buttonState);
|
||||
} else {
|
||||
return callback(null, false)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to generate a UUID
|
||||
*/
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
@ -1,3 +1,7 @@
|
||||
|
||||
/**
|
||||
* Interface to describe homebridge required elements.
|
||||
*/
|
||||
export interface IAccessory {
|
||||
/**
|
||||
* Required by homebridge.
|
||||
|
3
src/Accessories/index.ts
Normal file
3
src/Accessories/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export { ControlUnit } from './ControlUnit';
|
||||
export { DeviceButton } from './DeviceButton';
|
||||
export { IAccessory } from './IAccessory';
|
@ -1,169 +1,164 @@
|
||||
import { Activity } from "../Models/Activity";
|
||||
import { DeviceSetupItem } from "../Models/DeviceSetupItem";
|
||||
import { threadId } from "worker_threads";
|
||||
import { Input, Matrix, Output } from "../Models/Matrix";
|
||||
import { IActivity } from "../Models/Config/IActivity";
|
||||
import { IDeviceSetupItem } from "../Models/Config/IDeviceSetupItem";
|
||||
import { IInput, IMatrix, IOutput } from "../Models/Config/IMatrix";
|
||||
import { RemoteKey } from '../Accessories/ControlUnit';
|
||||
import { EventEmitter } from "events";
|
||||
import { ICommand } from '../Models/IDevice';
|
||||
import { IHub } from "../Models/Config/IHub";
|
||||
import { IDeviceConfig } from "../Models/Config/IDeviceConfig";
|
||||
import { HarmonyDevice } from "../Models/HarmonyDevice";
|
||||
import { HarmonyHub } from "../Models/HarmonyHub";
|
||||
|
||||
let Characteristic: HAPNodeJS.Characteristic;
|
||||
|
||||
const Harmony = require("harmony-websocket");
|
||||
interface IDevice {
|
||||
id: string,
|
||||
name: string,
|
||||
supportsCommand(commandName: string): boolean,
|
||||
getCommand(commandName: string): string,
|
||||
commands: { [name: string]: string };
|
||||
on: boolean;
|
||||
}
|
||||
|
||||
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,
|
||||
currentActivity: IActivity
|
||||
}
|
||||
|
||||
interface IHarmonyDataProviderProps {
|
||||
hubAddress: string,
|
||||
hubs: Array<IHub>;
|
||||
deviceConfigs: Array<IDeviceConfig>;
|
||||
log: any,
|
||||
matrix: Matrix
|
||||
matrix: IMatrix
|
||||
}
|
||||
|
||||
class HarmonyDataProvider {
|
||||
private harmony: any;
|
||||
private log: any;
|
||||
private hubAddress: string = "";
|
||||
private connected: boolean = false;
|
||||
class HarmonyDataProvider extends EventEmitter {
|
||||
private _log: any;
|
||||
private _hubsByDevice: { [deviceName: string]: string } = {};
|
||||
private _hubs: { [hubName: string]: HarmonyHub } = {};
|
||||
// private _devicesByHub: { [hubName: string]: { [deviceName: string]: HarmonyDevice } } = {};
|
||||
private _states: { [controlUnitName: string]: (IActivityState | undefined) } = {};
|
||||
|
||||
private devices: { [name: string]: IDevice; } = {};
|
||||
private states: { [controlUnitName: string]: (IActivityState | undefined) } = {};
|
||||
|
||||
private matrix: Matrix;
|
||||
private _matrix: IMatrix;
|
||||
|
||||
constructor(props: IHarmonyDataProviderProps) {
|
||||
this.log = props.log;
|
||||
this.hubAddress = props.hubAddress;
|
||||
this.matrix = props.matrix;
|
||||
|
||||
this.harmony = new Harmony();
|
||||
|
||||
//Listeners
|
||||
this.harmony.on('open', () => {
|
||||
this.connected = true;
|
||||
});
|
||||
this.harmony.on('close', () => {
|
||||
this.connected = false;
|
||||
super();
|
||||
this._log = props.log;
|
||||
this._matrix = props.matrix;
|
||||
props.deviceConfigs.forEach((deviceConfig: IDeviceConfig) => {
|
||||
this._hubsByDevice[deviceConfig.Name] = deviceConfig.Hub;
|
||||
});
|
||||
// this._deviceConfigs = props.deviceConfigs;
|
||||
|
||||
this.connect();
|
||||
this.connect(props.hubs);
|
||||
}
|
||||
|
||||
public powerOn = async (controlUnitName: string, activity: Activity) => {
|
||||
// public get devicesByHub(): { [hubName: string]: { [deviceName: string]: HarmonyDevice } } {
|
||||
// return this._devicesByHub;
|
||||
// }
|
||||
|
||||
public get hubs(): { [hubName: string]: HarmonyHub } {
|
||||
return this._hubs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Power on all devices in an activity.
|
||||
*/
|
||||
public powerOn = async (controlUnitName: string, activity: IActivity) => {
|
||||
//Only power on if not alread on
|
||||
let currentActivity = this.states[controlUnitName] ? this.states[controlUnitName]!.currentActivity : undefined;
|
||||
let currentActivity = this._states[controlUnitName] ? this._states[controlUnitName]!.currentActivity : undefined;
|
||||
if (!currentActivity) {
|
||||
await this.startActivity(controlUnitName, activity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Power off all devices in an activity that aren't being used.
|
||||
*/
|
||||
public powerOff = async (controlUnitName: string) => {
|
||||
if (!this.states[controlUnitName]) {
|
||||
if (!this._states[controlUnitName]) {
|
||||
return;
|
||||
}
|
||||
//Build potential list of devices to turn off
|
||||
let devicesToTurnOff: Array<IDevice> = this.states[controlUnitName]!.currentActivity.deviceSetupItems
|
||||
.map((value: DeviceSetupItem): IDevice => {
|
||||
return this.getDeviceFromName(value.deviceName);
|
||||
let devicesToTurnOff: Array<HarmonyDevice> = this._states[controlUnitName]!.currentActivity.DeviceSetupList
|
||||
.map((value: IDeviceSetupItem): HarmonyDevice => {
|
||||
return this.getDeviceFromName(value.DeviceName);
|
||||
});
|
||||
|
||||
//Resolve device conflicts with other controlUnits
|
||||
devicesToTurnOff = this.sanitizeDeviceList(devicesToTurnOff, controlUnitName);
|
||||
|
||||
//Turn off devices
|
||||
devicesToTurnOff.forEach((device: IDevice) => {
|
||||
this.powerOffDevice(device);
|
||||
devicesToTurnOff.forEach(async (device: HarmonyDevice) => {
|
||||
if (device) {
|
||||
await device.powerOff();
|
||||
}
|
||||
});
|
||||
|
||||
this.states[controlUnitName] = undefined;
|
||||
this._states[controlUnitName] = undefined;
|
||||
}
|
||||
|
||||
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;
|
||||
/**
|
||||
* Start an activity
|
||||
*/
|
||||
public startActivity = async (controlUnitName: string, activity: IActivity) => {
|
||||
this._log(`Starting activity ${activity.DisplayName} for controlUnit: ${controlUnitName}`)
|
||||
let lastActivity: IActivity | undefined = undefined;
|
||||
if (this._states[controlUnitName]) {
|
||||
lastActivity = this._states[controlUnitName]!.currentActivity;
|
||||
}
|
||||
|
||||
//Build potential list of devices to to turn on
|
||||
let devicesToTurnOn: Array<IDevice> = activity.deviceSetupItems.map((value: DeviceSetupItem): IDevice => {
|
||||
return this.getDeviceFromName(value.deviceName);
|
||||
let devicesToTurnOn: Array<HarmonyDevice> = activity.DeviceSetupList.map((value: IDeviceSetupItem): HarmonyDevice => {
|
||||
return this.getDeviceFromName(value.DeviceName);
|
||||
});
|
||||
|
||||
//Resolve device conflicts with other controlUnits
|
||||
devicesToTurnOn = this.sanitizeDeviceList(devicesToTurnOn, controlUnitName);
|
||||
|
||||
//Turn on devices
|
||||
await Promise.all(devicesToTurnOn.map(async (device: IDevice) => {
|
||||
if (device && device.name && this.devices[device.name]) {
|
||||
await Promise.all(devicesToTurnOn.map(async (device: HarmonyDevice) => {
|
||||
if (device && device.name) {
|
||||
if (!device.on) {
|
||||
this.log(`Turning on device ${device.name}`)
|
||||
await this.powerOnDevice(device);
|
||||
this._log(`Turning on device ${device.name}`)
|
||||
await device.powerOn();
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
//Assign correct input
|
||||
await Promise.all(
|
||||
activity.deviceSetupItems.map(async (value: DeviceSetupItem) => {
|
||||
let device: IDevice = this.getDeviceFromName(value.deviceName);
|
||||
activity.DeviceSetupList.map(async (value: IDeviceSetupItem) => {
|
||||
let device: HarmonyDevice = this.getDeviceFromName(value.DeviceName);
|
||||
|
||||
if (device && device.supportsCommand(`Input${value.input}`)) {
|
||||
let command: string = device.getCommand(`Input${value.input}`);
|
||||
await this.sendCommand(command);
|
||||
if (device && device.supportsCommand(`Input${value.Input}`)) {
|
||||
await device.sendCommand(`Input${value.Input}`)
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
if (activity.useMatrix) {
|
||||
if (activity.UseMatrix) {
|
||||
//get input and output
|
||||
let input: Input = this.matrix.inputs.filter(e => e.inputDevice === activity.controlDeviceId)[0];
|
||||
let output: Output = this.matrix.outputs.filter(e => e.outputDevice === activity.outputDeviceId)[0];
|
||||
let input: IInput = this._matrix.Inputs.filter(e => e.InputDevice === activity.ControlDevice)[0];
|
||||
let output: IOutput = this._matrix.Outputs.filter(e => e.OutputDevice === activity.OutputDevice)[0];
|
||||
|
||||
let inputCommandName: string = `In ${input.inputNumber}`;
|
||||
let outputCommandName: string = `Out ${output.outputLetter}`;
|
||||
let inputCommandName: string = `In ${input.InputNumber}`;
|
||||
let outputCommandName: string = `Out ${output.OutputLetter}`;
|
||||
|
||||
let matrixDevice: IDevice = this.getDeviceFromName(this.matrix.deviceName);
|
||||
let matrixDevice: HarmonyDevice = this.getDeviceFromName(this._matrix.DeviceName);
|
||||
|
||||
//Route hdmi
|
||||
if (matrixDevice.supportsCommand(inputCommandName) && matrixDevice.supportsCommand(outputCommandName)) {
|
||||
await this.sendCommand(matrixDevice.getCommand(inputCommandName));
|
||||
await this.sendCommand(matrixDevice.getCommand(outputCommandName));
|
||||
await matrixDevice.sendCommand(outputCommandName);
|
||||
await matrixDevice.sendCommand(inputCommandName);
|
||||
await matrixDevice.sendCommand(outputCommandName);
|
||||
await matrixDevice.sendCommand(inputCommandName);
|
||||
}
|
||||
}
|
||||
|
||||
//Build potential list of devices to turn off
|
||||
if (lastActivity) {
|
||||
let devicesToTurnOff: Array<IDevice> = lastActivity.deviceSetupItems.map((value: DeviceSetupItem): IDevice => {
|
||||
return this.getDeviceFromName(value.deviceName);
|
||||
let devicesToTurnOff: Array<HarmonyDevice> = lastActivity.DeviceSetupList.map((value: IDeviceSetupItem): HarmonyDevice => {
|
||||
return this.getDeviceFromName(value.DeviceName);
|
||||
});
|
||||
|
||||
//remove devices that will be used for next activity from list
|
||||
//delete array[index] is stupid because it just nulls out the index. But now i have to deal with nulls
|
||||
devicesToTurnOff.forEach((device: IDevice, index: number) => {
|
||||
if (device && device.name && activity.deviceSetupItems.some(e => {
|
||||
return (e && e.deviceName === device.name)
|
||||
devicesToTurnOff.forEach((device: HarmonyDevice, index: number) => {
|
||||
if (device && device.name && activity.DeviceSetupList.some(e => {
|
||||
return (e && e.DeviceName === device.name)
|
||||
})) {
|
||||
delete devicesToTurnOff[index];
|
||||
}
|
||||
@ -172,15 +167,15 @@ class HarmonyDataProvider {
|
||||
//Resolve device conflicts with other controlUnits
|
||||
devicesToTurnOff = this.sanitizeDeviceList(devicesToTurnOff, controlUnitName);
|
||||
|
||||
this.log(`Sanatized devices to turn off: ${JSON.stringify(devicesToTurnOff.map(e => e ? e.name : ""))}`);
|
||||
this._log(`Sanatized devices to turn off: ${JSON.stringify(devicesToTurnOff.map(e => e ? e.name : ""))}`);
|
||||
|
||||
await Promise.all(
|
||||
//Turn off devices
|
||||
devicesToTurnOff.map(async (device: IDevice) => {
|
||||
devicesToTurnOff.map(async (device: HarmonyDevice) => {
|
||||
if (device) {
|
||||
if (device.on) {
|
||||
this.log(`Turning off device ${device.name}`)
|
||||
await this.powerOffDevice(device);
|
||||
this._log(`Turning off device ${device.name}`)
|
||||
await device.powerOff();
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -189,176 +184,157 @@ class HarmonyDataProvider {
|
||||
}
|
||||
|
||||
//Assign current activity
|
||||
this.states[controlUnitName] = { currentActivity: activity };
|
||||
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]) {
|
||||
let volumeDevice: IDevice = this.getDeviceFromName(this.states[controlUnitName]!.currentActivity.volumeDeviceId);
|
||||
if (volumeDevice.supportsCommand(volumeUpCommand)) {
|
||||
this.sendCommand(volumeDevice.getCommand(volumeUpCommand));
|
||||
}
|
||||
if (this._states[controlUnitName]) {
|
||||
let volumeDevice: HarmonyDevice = this.getDeviceFromName(this._states[controlUnitName]!.currentActivity.VolumeDevice);
|
||||
await volumeDevice.sendCommand(volumeUpCommand);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Volume down for current running activity.
|
||||
*/
|
||||
public volumeDown = async (controlUnitName: string) => {
|
||||
let volumeDownCommand: string = "Volume Down"
|
||||
if (this.states[controlUnitName]) {
|
||||
let volumeDevice: IDevice = this.getDeviceFromName(this.states[controlUnitName]!.currentActivity.volumeDeviceId);
|
||||
if (volumeDevice.supportsCommand(volumeDownCommand)) {
|
||||
this.sendCommand(volumeDevice.getCommand(volumeDownCommand));
|
||||
}
|
||||
if (this._states[controlUnitName]) {
|
||||
let volumeDevice: HarmonyDevice = this.getDeviceFromName(this._states[controlUnitName]!.currentActivity.VolumeDevice);
|
||||
await volumeDevice.sendCommand(volumeDownCommand);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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]) {
|
||||
if (this._states[controlUnitName]) {
|
||||
let commandName: string = "";
|
||||
let device: IDevice = this.getDeviceFromName(this.states[controlUnitName]!.currentActivity.controlDeviceId);
|
||||
|
||||
let device: HarmonyDevice = this.getDeviceFromName(this._states[controlUnitName]!.currentActivity.ControlDevice);
|
||||
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));
|
||||
await device.sendCommand(commandName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public getIsActive(controlUnitName: string): Activity | undefined {
|
||||
return this.states[controlUnitName] ? this.states[controlUnitName]!.currentActivity : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to harmony and receive device info
|
||||
* Return if a control unit is active
|
||||
* @param controlUnitName
|
||||
*/
|
||||
private connect = async () => {
|
||||
await this.harmony.connect(this.hubAddress);
|
||||
let self = this;
|
||||
public getIsActive(controlUnitName: string): IActivity | undefined {
|
||||
return this._states[controlUnitName] ? this._states[controlUnitName]!.currentActivity : undefined;
|
||||
}
|
||||
|
||||
setTimeout(async function () {
|
||||
if (self.connected) {
|
||||
let devices: any = await self.harmony.getDevices();
|
||||
/**
|
||||
* Get the IDevice by name.
|
||||
* @param deviceName The device to retrieve.
|
||||
*/
|
||||
public getDeviceFromName(deviceName: string): HarmonyDevice {
|
||||
let device: HarmonyDevice | undefined;
|
||||
try {
|
||||
await Promise.all(
|
||||
//Add each to dictionary
|
||||
devices.map(async (dev: any) => {
|
||||
//get commands
|
||||
let commands: { [name: string]: string } = {};
|
||||
let deviceCommands: any = await self.harmony.getDeviceCommands(dev.id);
|
||||
deviceCommands.forEach((command: any) => {
|
||||
commands[command.label] = command.action;
|
||||
});
|
||||
self.devices[dev.label] = {
|
||||
id: dev.id,
|
||||
name: dev.label,
|
||||
commands: commands,
|
||||
on: false,
|
||||
//Define device methods
|
||||
supportsCommand(commandName: string): boolean {
|
||||
let command = commands[commandName];
|
||||
return (command) ? true : false;
|
||||
},
|
||||
getCommand(commandName: string): string {
|
||||
return commands[commandName];
|
||||
}
|
||||
}
|
||||
}));
|
||||
self.log(`Harmony data provider ready`);
|
||||
|
||||
device = this._hubs[this._hubsByDevice[deviceName]].getDeviceByName(deviceName);
|
||||
} catch (err) {
|
||||
self.log(`ERROR - error connecting to harmony: ${err}`);
|
||||
}
|
||||
}
|
||||
}, 1000);
|
||||
this._log(`Error retrieving device from hub: ${err}`);
|
||||
}
|
||||
|
||||
private powerOffDevice = async (device: IDevice) => {
|
||||
let powerOffCommand: string = "Power Off";
|
||||
let powerToggleCommand: string = "Power Toggle";
|
||||
if (device && device.supportsCommand(powerOffCommand)) {
|
||||
await this.sendCommand(device.getCommand(powerOffCommand));
|
||||
device.on = false;
|
||||
} else if (device && device.supportsCommand(powerToggleCommand)) {
|
||||
await this.sendCommand(device.getCommand(powerToggleCommand));
|
||||
device.on = false;
|
||||
}
|
||||
return device!;
|
||||
}
|
||||
|
||||
private powerOnDevice = async (device: IDevice) => {
|
||||
let powerOnCommand: string = "Power On";
|
||||
let powerToggleCommand: string = "Power Toggle";
|
||||
if (device && device.supportsCommand(powerOnCommand)) {
|
||||
await this.sendCommand(device.getCommand(powerOnCommand));
|
||||
device.on = true;
|
||||
} else if (device && device.supportsCommand(powerToggleCommand)) {
|
||||
await this.sendCommand(device.getCommand(powerToggleCommand));
|
||||
device.on = true;
|
||||
// /**
|
||||
// * Gets device button commands
|
||||
// * @param deviceCommandName The device command name
|
||||
// * @param deviceName The device name
|
||||
// */
|
||||
// public getCommand(deviceCommandName: string, deviceName: string): ICommand | undefined {
|
||||
// const device: HarmonyDevice = this.getDeviceFromName(deviceName);
|
||||
// if (device && device.supportsCommand(deviceCommandName)) {
|
||||
// return device.getCommand(deviceCommandName);
|
||||
// } else {
|
||||
// return undefined;
|
||||
// }
|
||||
// }
|
||||
|
||||
private connect = async (hubs: Array<IHub>) => {
|
||||
let readyCount = 0;
|
||||
await Promise.all(
|
||||
hubs.map(async (hub: IHub): Promise<void> => {
|
||||
const newHarmonyHub = new HarmonyHub(hub.Name, hub.Ip, this._log);
|
||||
this._hubs[hub.Name] = newHarmonyHub;
|
||||
newHarmonyHub.on("Ready", () => {
|
||||
readyCount++;
|
||||
if (readyCount === Object.keys(this._hubs).length) {
|
||||
this.emit("Ready");
|
||||
}
|
||||
})
|
||||
await newHarmonyHub.initialize();
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
private getDeviceFromName(deviceName: string): IDevice {
|
||||
return this.devices[deviceName];
|
||||
}
|
||||
|
||||
private sanitizeDeviceList(devicesToTurnOn: Array<IDevice>, controlUnitName: string): Array<IDevice> {
|
||||
for (let controlUnitKey in this.states) {
|
||||
/**
|
||||
* 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<HarmonyDevice>, controlUnitName: string): Array<HarmonyDevice> {
|
||||
for (let controlUnitKey in this._states) {
|
||||
//Skip self
|
||||
if (controlUnitKey === controlUnitName) {
|
||||
continue;
|
||||
}
|
||||
let currentOtherState: IActivityState = this.states[controlUnitKey]!;
|
||||
let currentOtherState: IActivityState = this._states[controlUnitKey]!;
|
||||
|
||||
if (currentOtherState) {
|
||||
currentOtherState.currentActivity.deviceSetupItems.forEach((value: DeviceSetupItem) => {
|
||||
currentOtherState.currentActivity.DeviceSetupList.forEach((value: IDeviceSetupItem) => {
|
||||
//there are devices to remove
|
||||
if (devicesToTurnOn.some(e => e && e.name === value.deviceName)) {
|
||||
let deviceToRemove: IDevice = devicesToTurnOn.filter(i => i.name === value.deviceName)[0];
|
||||
if (devicesToTurnOn.some(e => e && e.name === value.DeviceName)) {
|
||||
let deviceToRemove: HarmonyDevice = devicesToTurnOn.filter(i => i.name === value.DeviceName)[0];
|
||||
delete devicesToTurnOn[devicesToTurnOn.indexOf(deviceToRemove)];
|
||||
}
|
||||
});
|
||||
@ -367,16 +343,6 @@ class HarmonyDataProvider {
|
||||
|
||||
return devicesToTurnOn;
|
||||
}
|
||||
|
||||
private sendCommand = async (command: string) => {
|
||||
try {
|
||||
let response = await this.harmony.sendCommand(JSON.stringify(command));
|
||||
// this.log(`Sent command: ${JSON.stringify(command)} response: ${JSON.stringify(response)}`);
|
||||
} catch (err) {
|
||||
this.log(`ERROR - error sending command to harmony: ${err}`);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export default HarmonyDataProvider;
|
@ -1,69 +0,0 @@
|
||||
import { DeviceSetupItem } from './DeviceSetupItem';
|
||||
/**
|
||||
* Input properties.
|
||||
*/
|
||||
export interface IActivityProps {
|
||||
deviceList: Array<DeviceSetupItem>,
|
||||
controlDeviceId: string,
|
||||
volumeDeviceId: string,
|
||||
outputDeviceId: string,
|
||||
displayName: string
|
||||
useMatrix: boolean,
|
||||
}
|
||||
|
||||
/**
|
||||
* Data model class to hold activity related information.
|
||||
*/
|
||||
export class Activity {
|
||||
private _volumeDeviceId: string = "";
|
||||
private _outputDeviceId: string = "";
|
||||
private _controlDeviceId: string = "";
|
||||
private _displayName: string = "";
|
||||
private _deviceSetupItems: Array<DeviceSetupItem>;
|
||||
private _useMatrix: boolean = false;
|
||||
|
||||
constructor(props: IActivityProps) {
|
||||
this._controlDeviceId = props.controlDeviceId;
|
||||
this._outputDeviceId = props.outputDeviceId;
|
||||
this._volumeDeviceId = props.volumeDeviceId;
|
||||
this._displayName = props.displayName;
|
||||
this._deviceSetupItems = props.deviceList;
|
||||
this._useMatrix = props.useMatrix
|
||||
}
|
||||
|
||||
/**
|
||||
* The device associated with main control.
|
||||
*/
|
||||
public get controlDeviceId(): string {
|
||||
return this._controlDeviceId;
|
||||
};
|
||||
|
||||
/**
|
||||
* The device associated with the volume control.
|
||||
*/
|
||||
public get volumeDeviceId(): string {
|
||||
return this._volumeDeviceId
|
||||
};
|
||||
|
||||
/**
|
||||
* The device associated with output.
|
||||
*/
|
||||
public get outputDeviceId(): string {
|
||||
return this._outputDeviceId;
|
||||
};
|
||||
|
||||
/**
|
||||
* The display name of the activity.
|
||||
*/
|
||||
public get displayName(): string {
|
||||
return this._displayName;
|
||||
}
|
||||
|
||||
public get deviceSetupItems(): Array<DeviceSetupItem> {
|
||||
return this._deviceSetupItems
|
||||
}
|
||||
|
||||
public get useMatrix(): boolean {
|
||||
return this._useMatrix;
|
||||
}
|
||||
}
|
11
src/Models/Config/IActivity.ts
Normal file
11
src/Models/Config/IActivity.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { IDeviceSetupItem } from './IDeviceSetupItem';
|
||||
|
||||
export interface IActivity {
|
||||
OutputDevice: string;
|
||||
VolumeDevice: string;
|
||||
ControlDevice: string;
|
||||
DisplayName: string;
|
||||
DeviceSetupList: Array<IDeviceSetupItem>;
|
||||
UseMatrix: boolean;
|
||||
|
||||
}
|
20
src/Models/Config/IConfig.ts
Normal file
20
src/Models/Config/IConfig.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { IMatrix } from "./IMatrix";
|
||||
import { IActivity } from "./IActivity";
|
||||
import { IDeviceButton } from "./IDeviceButton";
|
||||
import { IDeviceConfig } from "./IDeviceConfig";
|
||||
import { IHub } from './IHub';
|
||||
|
||||
export interface IControlUnit {
|
||||
DisplayName: string;
|
||||
Activities: Array<IActivity>;
|
||||
}
|
||||
|
||||
export interface IConfig {
|
||||
hubIp: string;
|
||||
EmitDevicesOnStartup: boolean;
|
||||
Matrix: IMatrix;
|
||||
ControlUnits: Array<IControlUnit>;
|
||||
DeviceButtons: Array<IDeviceButton>;
|
||||
Devices: Array<IDeviceConfig>;
|
||||
Hubs: Array<IHub>;
|
||||
}
|
7
src/Models/Config/IDeviceButton.ts
Normal file
7
src/Models/Config/IDeviceButton.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export interface IDeviceButton {
|
||||
DeviceName: string;
|
||||
ButtonName: string;
|
||||
DisplayName: string;
|
||||
NumberOfKeyPresses: number;
|
||||
IsStateful: boolean;
|
||||
}
|
4
src/Models/Config/IDeviceConfig.ts
Normal file
4
src/Models/Config/IDeviceConfig.ts
Normal file
@ -0,0 +1,4 @@
|
||||
export interface IDeviceConfig {
|
||||
Name: string;
|
||||
Hub: string;
|
||||
}
|
5
src/Models/Config/IDeviceSetupItem.ts
Normal file
5
src/Models/Config/IDeviceSetupItem.ts
Normal file
@ -0,0 +1,5 @@
|
||||
|
||||
export interface IDeviceSetupItem {
|
||||
DeviceName: string;
|
||||
Input: string;
|
||||
}
|
5
src/Models/Config/IHub.ts
Normal file
5
src/Models/Config/IHub.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export interface IHub {
|
||||
Name: string;
|
||||
Ip: string;
|
||||
Harmony: any;
|
||||
}
|
15
src/Models/Config/IMatrix.ts
Normal file
15
src/Models/Config/IMatrix.ts
Normal file
@ -0,0 +1,15 @@
|
||||
export interface IInput {
|
||||
InputNumber: string,
|
||||
InputDevice: string,
|
||||
}
|
||||
|
||||
export interface IOutput {
|
||||
OutputLetter: string,
|
||||
OutputDevice: string,
|
||||
}
|
||||
|
||||
export interface IMatrix {
|
||||
Inputs: Array<IInput>;
|
||||
Outputs: Array<IOutput>;
|
||||
DeviceName: string;
|
||||
}
|
5
src/Models/Config/index.ts
Normal file
5
src/Models/Config/index.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export * from './IActivity';
|
||||
export * from './IConfig';
|
||||
export * from './IDeviceButton';
|
||||
export * from './IDeviceSetupItem';
|
||||
export * from './IMatrix';
|
@ -1,22 +0,0 @@
|
||||
|
||||
export interface IDeviceSetupItemProps {
|
||||
deviceName: string,
|
||||
input: string
|
||||
}
|
||||
|
||||
export class DeviceSetupItem {
|
||||
private _deviceId: string = "";
|
||||
private _input: string = "";
|
||||
constructor(props: IDeviceSetupItemProps) {
|
||||
this._deviceId = props.deviceName;
|
||||
this._input = props.input;
|
||||
}
|
||||
|
||||
public get deviceName() {
|
||||
return this._deviceId;
|
||||
}
|
||||
|
||||
public get input() {
|
||||
return this._input;
|
||||
}
|
||||
}
|
94
src/Models/HarmonyDevice.ts
Normal file
94
src/Models/HarmonyDevice.ts
Normal file
@ -0,0 +1,94 @@
|
||||
import { ICommand } from "./IDevice";
|
||||
import { sleep } from "../Util/Sleep";
|
||||
|
||||
export interface IHarmonyDeviceProps {
|
||||
id: string;
|
||||
name: string;
|
||||
harmony: any;
|
||||
log: any;
|
||||
commands: { [name: string]: ICommand };
|
||||
}
|
||||
|
||||
export class HarmonyDevice {
|
||||
private _harmony: any;
|
||||
private _log: any;
|
||||
private _commands: { [name: string]: ICommand } = {};
|
||||
private _on: boolean;
|
||||
|
||||
constructor(props: IHarmonyDeviceProps) {
|
||||
this.id = props.id;
|
||||
this.name = props.name;
|
||||
this._harmony = props.harmony;
|
||||
this._on = false;
|
||||
this._commands = props.commands;
|
||||
}
|
||||
|
||||
public id: string;
|
||||
public name: string;
|
||||
|
||||
public get on(): boolean {
|
||||
return this._on;
|
||||
}
|
||||
|
||||
public get commands(): { [name: string]: ICommand } {
|
||||
return this._commands;
|
||||
}
|
||||
|
||||
//Define device methods
|
||||
public supportsCommand(commandName: string): boolean {
|
||||
let command = this._commands[commandName];
|
||||
return (command) ? true : false;
|
||||
}
|
||||
|
||||
public getCommand(commandName: string): ICommand {
|
||||
return this._commands[commandName];
|
||||
}
|
||||
|
||||
public async powerOn(): Promise<void> {
|
||||
let powerOnCommand: string = "Power On";
|
||||
let powerToggleCommand: string = "Power Toggle";
|
||||
if (this.supportsCommand(powerOnCommand)) {
|
||||
await this.sendCommand(powerOnCommand);
|
||||
this._on = true;
|
||||
} else if (this.supportsCommand(powerToggleCommand)) {
|
||||
await this.sendCommand(powerToggleCommand);
|
||||
this._on = true;
|
||||
}
|
||||
}
|
||||
|
||||
public async powerOff(): Promise<void> {
|
||||
let powerOffCommand: string = "Power Off";
|
||||
let powerToggleCommand: string = "Power Toggle";
|
||||
if (this.supportsCommand(powerOffCommand)) {
|
||||
await this.sendCommand(powerOffCommand);
|
||||
this._on = false;
|
||||
} else if (this.supportsCommand(powerToggleCommand)) {
|
||||
await this.sendCommand(powerToggleCommand);
|
||||
this._on = false;
|
||||
}
|
||||
}
|
||||
|
||||
public async sendCommand(commandName: string): Promise<void> {
|
||||
let command!: ICommand;
|
||||
if (this.supportsCommand(commandName)) {
|
||||
command = this.getCommand(commandName);
|
||||
}
|
||||
|
||||
try {
|
||||
//Execute command
|
||||
//HACK to fix Harmon Kardon receiver not turning off
|
||||
if (command.command === "PowerOff") {
|
||||
for (let i = 0; i < 2; i++) {
|
||||
await this._harmony.sendCommand(JSON.stringify(command));
|
||||
}
|
||||
|
||||
}
|
||||
await this._harmony.sendCommand(JSON.stringify(command));
|
||||
|
||||
//Sleep
|
||||
await sleep(800);
|
||||
} catch (err) {
|
||||
this._log(`ERROR - error sending command to harmony: ${err}`);
|
||||
}
|
||||
}
|
||||
}
|
68
src/Models/HarmonyHub.ts
Normal file
68
src/Models/HarmonyHub.ts
Normal file
@ -0,0 +1,68 @@
|
||||
import { HarmonyDevice } from './HarmonyDevice';
|
||||
const Harmony = require("harmony-websocket");
|
||||
import { ICommand } from './IDevice';
|
||||
import { EventEmitter } from 'events';
|
||||
|
||||
|
||||
|
||||
export class HarmonyHub extends EventEmitter {
|
||||
private _devices: { [deviceName: string]: HarmonyDevice } = {}
|
||||
private _ip: string;
|
||||
private _harmony: any;
|
||||
private _log: any;
|
||||
private _name: string;
|
||||
|
||||
constructor(hubName: string, ipAddress: string, log: any) {
|
||||
super();
|
||||
this._ip = ipAddress;
|
||||
this._log = log;
|
||||
this._name = hubName;
|
||||
}
|
||||
|
||||
public get devices(): { [deviceName: string]: HarmonyDevice } {
|
||||
return this._devices;
|
||||
}
|
||||
|
||||
public get hubName(): string {
|
||||
return this._name;
|
||||
}
|
||||
|
||||
public getDeviceByName = (deviceName: string): HarmonyDevice => {
|
||||
return this._devices[deviceName];
|
||||
}
|
||||
|
||||
public initialize = async () => {
|
||||
this._harmony = new Harmony();
|
||||
await this._harmony.connect(this._ip);
|
||||
|
||||
//Gather devices
|
||||
let devices: any = await this._harmony.getDevices();
|
||||
try {
|
||||
await Promise.all(
|
||||
//Add each to dictionary
|
||||
devices.map(async (dev: any) => {
|
||||
//get commands
|
||||
let commands: { [name: string]: ICommand } = {};
|
||||
let deviceCommands: any = await this._harmony.getDeviceCommands(dev.id);
|
||||
deviceCommands.forEach((command: any) => {
|
||||
commands[command.label] = command.action;
|
||||
});
|
||||
this._devices[dev.label] = new HarmonyDevice({
|
||||
id: dev.id,
|
||||
name: dev.label,
|
||||
commands: commands,
|
||||
log: this._log,
|
||||
harmony: this._harmony
|
||||
});
|
||||
}));
|
||||
this.emit("Ready");
|
||||
|
||||
} catch (err) {
|
||||
this._log(`ERROR - error connecting to harmony: ${err}`);
|
||||
}
|
||||
}
|
||||
|
||||
private connect = async (): Promise<void> => {
|
||||
await this._harmony.Connect(this._ip);
|
||||
}
|
||||
}
|
19
src/Models/IDevice.ts
Normal file
19
src/Models/IDevice.ts
Normal file
@ -0,0 +1,19 @@
|
||||
export interface ICommand {
|
||||
command?: string,
|
||||
deviceId?: string,
|
||||
type?: string
|
||||
}
|
||||
|
||||
export interface IDevice {
|
||||
id: string;
|
||||
name: string;
|
||||
harmony: any;
|
||||
log: any
|
||||
supportsCommand(commandName: string): boolean,
|
||||
getCommand(commandName: string): ICommand,
|
||||
powerOn(): Promise<void>;
|
||||
powerOff(): Promise<void>;
|
||||
sendCommand(commandName: string): Promise<void>;
|
||||
commands: { [name: string]: ICommand };
|
||||
on: boolean;
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
export interface IMatrixProps {
|
||||
inputs: Array<Input>,
|
||||
outputs: Array<Output>,
|
||||
deviceName: string,
|
||||
}
|
||||
|
||||
export interface Input {
|
||||
inputNumber: string,
|
||||
inputDevice: string,
|
||||
}
|
||||
|
||||
export interface Output {
|
||||
outputLetter: string,
|
||||
outputDevice: string,
|
||||
}
|
||||
|
||||
export class Matrix {
|
||||
private _inputs: Array<Input> = [];
|
||||
private _outputs: Array<Output> = [];
|
||||
private _deviceName: string;
|
||||
|
||||
constructor(props: IMatrixProps) {
|
||||
this._inputs = props.inputs;
|
||||
this._outputs = props.outputs;
|
||||
this._deviceName = props.deviceName;
|
||||
}
|
||||
|
||||
public get inputs(): Array<Input> {
|
||||
return this._inputs
|
||||
}
|
||||
|
||||
public get outputs(): Array<Output> {
|
||||
return this._outputs;
|
||||
}
|
||||
|
||||
public get deviceName(): string {
|
||||
return this._deviceName;
|
||||
}
|
||||
}
|
1
src/Models/index.ts
Normal file
1
src/Models/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './IDevice';
|
@ -1,5 +1,8 @@
|
||||
|
||||
|
||||
/**
|
||||
* Helper function to convert callbacks into promises
|
||||
* @param func
|
||||
*/
|
||||
export default function callbackify(func: (...args: any[]) => Promise<any>): Function {
|
||||
return (...args: any[]) => {
|
||||
const onlyArgs: any[] = [];
|
||||
|
3
src/Util/Sleep.ts
Normal file
3
src/Util/Sleep.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export function sleep(ms: number) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
178
src/index.ts
178
src/index.ts
@ -1,12 +1,17 @@
|
||||
import { ControlUnit } from "./Accessories/ControlUnit";
|
||||
import { Activity } from "./Models/Activity";
|
||||
import { DeviceSetupItem } from "./Models/DeviceSetupItem";
|
||||
import { Input, Output, Matrix } from "./Models/Matrix";
|
||||
import * as Accessories from "./Accessories";
|
||||
import HarmonyDataProvider from "./DataProviders/HarmonyDataProvider";
|
||||
import * as Config from "./Models/Config";
|
||||
import { IDevice } from "./Models";
|
||||
import { HarmonyDevice } from "./Models/HarmonyDevice";
|
||||
import { HarmonyHub } from "./Models/HarmonyHub";
|
||||
|
||||
let Accessory: any;
|
||||
let Homebridge: any;
|
||||
|
||||
/**
|
||||
* Main entry.
|
||||
* @param homebridge
|
||||
*/
|
||||
export default function (homebridge: any) {
|
||||
Homebridge = homebridge;
|
||||
Accessory = homebridge.platformAccessory;
|
||||
@ -20,9 +25,10 @@ export default function (homebridge: any) {
|
||||
|
||||
class HarmonyMatrixPlatform {
|
||||
log: any = {};
|
||||
config: any = {};
|
||||
config: Config.IConfig;
|
||||
api: any;
|
||||
externalAccessories: Array<any> = [];
|
||||
dataProvider: HarmonyDataProvider | null;
|
||||
accessoryList: Array<Accessories.IAccessory> = [];
|
||||
|
||||
constructor(log: any, config: any, api: any) {
|
||||
this.log = log;
|
||||
@ -30,123 +36,87 @@ class HarmonyMatrixPlatform {
|
||||
this.api = api;
|
||||
this.log('INFO - Registering Harmony Matrix Platform');
|
||||
this.api.on('didFinishLaunching', this.didFinishLaunching.bind(this));
|
||||
|
||||
this.dataProvider = null;
|
||||
|
||||
if (this.config) {
|
||||
//construct data provider
|
||||
this.dataProvider = new HarmonyDataProvider({
|
||||
hubs: this.config.Hubs,
|
||||
deviceConfigs: this.config.Devices,
|
||||
matrix: this.config.Matrix,
|
||||
log: this.log
|
||||
});
|
||||
|
||||
//Emit devices if requested
|
||||
|
||||
this.dataProvider.on("Ready", () => {
|
||||
this.log("All hubs connected");
|
||||
if (this.config.EmitDevicesOnStartup) {
|
||||
const hubs = this.dataProvider!.hubs;
|
||||
Object.values(hubs).forEach((hub: HarmonyHub) => {
|
||||
const deviceDictionary = hub.devices;
|
||||
this.log(`${hub.hubName}`)
|
||||
|
||||
Object.values(deviceDictionary).forEach((device: HarmonyDevice) => {
|
||||
this.log(` ${device.name} : ${device.id}`);
|
||||
Object.keys(device.commands).forEach((command: string) => {
|
||||
this.log(` ${command}`);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for didFinishLaunching
|
||||
* Happens after constructor
|
||||
*/
|
||||
didFinishLaunching() {
|
||||
this.log(`Publishing external accessories`);
|
||||
|
||||
this.externalAccessories.forEach((accessory: ControlUnit) => {
|
||||
//This is required in order to have multiple tv remotes on one platform
|
||||
this.accessoryList.forEach((accessory: Accessories.IAccessory) => {
|
||||
if (accessory instanceof Accessories.ControlUnit) {
|
||||
this.api.publishExternalAccessories("HarmonyMatrixPlatform", [accessory.platformAccessory]);
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by homebridge to gather accessories.
|
||||
* @param callback
|
||||
*/
|
||||
accessories(callback: (accessories: Array<ControlUnit>) => void) {
|
||||
//Parse ip
|
||||
let hubIp: string = this.config["hubIp"];
|
||||
accessories(callback: (accessories: Array<Accessories.IAccessory>) => void) {
|
||||
|
||||
//Parse matrix
|
||||
let configInputs: any = this.config["Matrix"]["Inputs"];
|
||||
let configOutputs: any = this.config["Matrix"]["Outputs"];
|
||||
let matrixName: string = this.config["Matrix"]["DeviceName"];
|
||||
|
||||
let inputs: Array<Input> = [];
|
||||
let outputs: Array<Output> = [];
|
||||
|
||||
configInputs.forEach((configInput: any) => {
|
||||
let inputDevice: string = configInput["InputDevice"];
|
||||
let inputNumber: string = configInput["InputNumber"];
|
||||
|
||||
this.log(`INFO - Added input to matrix '${inputDevice}'`);
|
||||
|
||||
inputs.push({
|
||||
inputDevice: inputDevice,
|
||||
inputNumber: inputNumber
|
||||
});
|
||||
});
|
||||
configOutputs.forEach((configOutput: any) => {
|
||||
let outputDevice: string = configOutput["OutputDevice"];
|
||||
let outputLetter: string = configOutput["OutputLetter"];
|
||||
|
||||
this.log(`INFO - Added output to matrix '${outputDevice}'`);
|
||||
|
||||
outputs.push({
|
||||
outputDevice: outputDevice,
|
||||
outputLetter: outputLetter
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
let matrix = new Matrix({
|
||||
inputs: inputs,
|
||||
outputs: outputs,
|
||||
deviceName: matrixName,
|
||||
});
|
||||
|
||||
//construct data provider
|
||||
let dataProvider = new HarmonyDataProvider({
|
||||
hubAddress: hubIp,
|
||||
matrix: matrix,
|
||||
log: this.log
|
||||
});
|
||||
|
||||
//Parse control units
|
||||
let configControlUnits: any = this.config["ControlUnits"];
|
||||
let controlUnits: Array<ControlUnit> = [];
|
||||
configControlUnits.forEach((configControlUnit: any) => {
|
||||
//Parse activities list
|
||||
let configActivities: any = configControlUnit["Activities"];
|
||||
let activities: Array<Activity> = [];
|
||||
configActivities.forEach((configActivity: any) => {
|
||||
//parse devices
|
||||
let configDevices: any = configActivity["DeviceSetupList"];
|
||||
let devices: Array<DeviceSetupItem> = [];
|
||||
configDevices.forEach((configDevice: any) => {
|
||||
//Add device
|
||||
devices.push(new DeviceSetupItem({
|
||||
deviceName: configDevice["DeviceName"],
|
||||
input: configDevice["Input"]
|
||||
}));
|
||||
this.log(`INFO - Added device '${configDevice["DeviceName"]}' for activity '${configActivity["DisplayName"]}'`);
|
||||
});
|
||||
|
||||
//Add activity
|
||||
activities.push(new Activity({
|
||||
volumeDeviceId: configActivity["VolumeDevice"],
|
||||
controlDeviceId: configActivity["ControlDevice"],
|
||||
outputDeviceId: configActivity["OutputDevice"],
|
||||
displayName: configActivity["DisplayName"],
|
||||
useMatrix: configActivity["UseMatrix"],
|
||||
deviceList: devices
|
||||
}));
|
||||
this.log(`INFO - Added activity '${configActivity["DisplayName"]}'`);
|
||||
});
|
||||
|
||||
// let accessory = new Accessory(configControlUnit["DisplayName"],
|
||||
// Homebridge.hap.uuid.generate(configControlUnit["DisplayName"], Homebridge.hap.Accessory.Categories.TELEVISION));
|
||||
|
||||
let controlUnit: ControlUnit = new ControlUnit({
|
||||
dataProvider: dataProvider,
|
||||
displayName: configControlUnit["DisplayName"],
|
||||
//Add control units
|
||||
this.config.ControlUnits.forEach((unit: Config.IControlUnit) => {
|
||||
this.accessoryList.push(new Accessories.ControlUnit({
|
||||
dataProvider: this.dataProvider!,
|
||||
displayName: unit.DisplayName,
|
||||
api: this.api,
|
||||
log: this.log,
|
||||
activities: activities,
|
||||
homebridge: Homebridge
|
||||
activities: unit.Activities,
|
||||
homebridge: Homebridge,
|
||||
}));
|
||||
});
|
||||
|
||||
//@ts-ignore
|
||||
let accessory = controlUnit as homebridge.platformAccessory;
|
||||
//Add device buttons
|
||||
this.config.DeviceButtons.forEach((button: Config.IDeviceButton) => {
|
||||
this.accessoryList.push(new Accessories.DeviceButton({
|
||||
dataProvider: this.dataProvider!,
|
||||
buttonName: button.ButtonName,
|
||||
displayName: button.DisplayName,
|
||||
deviceInfo: button,
|
||||
api: this.api,
|
||||
log: this.log,
|
||||
homebridge: Homebridge,
|
||||
|
||||
//Add control unit
|
||||
controlUnits.push(accessory);
|
||||
|
||||
this.log(`INFO - Added ControlUnit`);
|
||||
}))
|
||||
});
|
||||
this.externalAccessories = controlUnits;
|
||||
callback(controlUnits);
|
||||
callback(this.accessoryList);
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@
|
||||
"declaration": true,
|
||||
"outDir": "./bin",
|
||||
"strict": true,
|
||||
"sourceMap": true,
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
|
Reference in New Issue
Block a user