Renamed files. Refactored control unit (untested)
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Brandon Watson 2021-12-28 16:37:40 -05:00
parent 1a51e4d8c5
commit c65853220e
27 changed files with 707 additions and 249 deletions

View File

@ -1,8 +1,11 @@
import { PlatformAccessory, RemoteController, Service } from "homebridge"; import { PlatformAccessory, RemoteController, Service } from "homebridge";
import HarmonyDataProvider from "../DataProviders/HarmonyDataProvider"; import HarmonyDataProvider from "../DataProviders/harmonyDataProvider";
import { IActivity } from "../Models/Config"; import { IActivity } from "../Models/Config";
import { Platform } from "../platform"; import { Platform } from "../platform";
import callbackify from "../Util/Callbackify"; import { ActivityService } from "../Services/activityService";
import { CommandService } from "../Services/commandService";
import { VolumeService } from "../Services/volumeService";
import callbackify from "../Util/callbackify";
/** /**
* Enum describing remote key presses from homebridge. * Enum describing remote key presses from homebridge.
@ -27,7 +30,9 @@ export class ControlUnit {
constructor( constructor(
private readonly _platform: Platform, private readonly _platform: Platform,
private readonly _accessory: PlatformAccessory, private readonly _accessory: PlatformAccessory,
private _dataProvider: HarmonyDataProvider, private _activityService: ActivityService,
private _commandService: CommandService,
private _volumeService: VolumeService,
private _activities: Array<IActivity> private _activities: Array<IActivity>
) { ) {
this._accessory this._accessory
@ -129,11 +134,11 @@ export class ControlUnit {
private onSetAccessoryActive = async (value: any) => { private onSetAccessoryActive = async (value: any) => {
switch (value) { switch (value) {
case 0: case 0:
this._dataProvider.powerOff(this._accessory.displayName); this._activityService.stopCurrentActivity(this._accessory.displayName);
break; break;
//Turn on with first activity //Turn on with first activity
case 1: case 1:
this._dataProvider.powerOn( this._activityService.startActivity(
this._accessory.displayName, this._accessory.displayName,
this._activities[0] this._activities[0]
); );
@ -155,14 +160,14 @@ export class ControlUnit {
* Event handler for SET remote key * Event handler for SET remote key
*/ */
private onSetRemoteKey = async (key: any) => { private onSetRemoteKey = async (key: any) => {
this._dataProvider.sendKeyPress(this._accessory.displayName, key); this._commandService.sendKeyPress(this._accessory.displayName, key);
}; };
/** /**
* Event handler for SET active identifier characteristic * Event handler for SET active identifier characteristic
*/ */
private onSetActiveIdentifier = async (identifier: any) => { private onSetActiveIdentifier = async (identifier: any) => {
this._dataProvider.startActivity( this._activityService.startActivity(
this._accessory.displayName, this._accessory.displayName,
this._activities[identifier] this._activities[identifier]
); );
@ -172,7 +177,7 @@ export class ControlUnit {
* Event handler for GET active identifier characteristic * Event handler for GET active identifier characteristic
*/ */
private onGetActiveIdentifier = async () => { private onGetActiveIdentifier = async () => {
let currentActivity: IActivity = this._dataProvider.getIsActive( let currentActivity: IActivity = this._activityService.getIsActive(
this._accessory.displayName this._accessory.displayName
)!; )!;
let identifier: number = 0; let identifier: number = 0;
@ -230,10 +235,10 @@ export class ControlUnit {
private onSetVolumeSelector = async (value: any) => { private onSetVolumeSelector = async (value: any) => {
switch (value) { switch (value) {
case 0: case 0:
this._dataProvider.volumeUp(this._accessory.displayName); this._volumeService.volumeUp(this._accessory.displayName);
break; break;
case 1: case 1:
this._dataProvider.volumeDown(this._accessory.displayName); this._volumeService.volumeDown(this._accessory.displayName);
break; break;
} }
}; };

View File

@ -1,7 +1,7 @@
import { PlatformAccessory, Service } from "homebridge"; import { PlatformAccessory, Service } from "homebridge";
import HarmonyDataProvider from "../DataProviders/HarmonyDataProvider"; import HarmonyDataProvider from "../DataProviders/harmonyDataProvider";
import { IDeviceButton } from "../Models/Config"; import { IDeviceButton } from "../Models/Config";
import { HarmonyDevice } from "../Models/HarmonyDevice"; import { HarmonyDevice } from "../Models/harmonyDevice";
import { Platform } from "../platform"; import { Platform } from "../platform";
export class DeviceButton { export class DeviceButton {

View File

@ -5,9 +5,9 @@ import {
PlatformAccessory, PlatformAccessory,
Service, Service,
} from "homebridge"; } from "homebridge";
import HarmonyDataProvider from "../DataProviders/HarmonyDataProvider"; import HarmonyDataProvider from "../DataProviders/harmonyDataProvider";
import { ISequence } from "../Models/Config/ISequence"; import { ISequence } from "../Models/Config/sequence";
import { HarmonyDevice } from "../Models/HarmonyDevice"; import { HarmonyDevice } from "../Models/harmonyDevice";
import { Platform } from "../platform"; import { Platform } from "../platform";
import { sleep } from "../Util"; import { sleep } from "../Util";

View File

@ -1,2 +1,2 @@
export { ControlUnit } from './ControlUnit'; export { ControlUnit } from "./controlUnit";
export { DeviceButton } from './DeviceButton'; export { DeviceButton } from "./deviceButton";

View File

@ -1,12 +1,12 @@
import { IActivity } from "../Models/Config/IActivity"; import { IActivity } from "../Models/Config/activity";
import { IDeviceSetupItem } from "../Models/Config/IDeviceSetupItem"; import { IDeviceSetupItem } from "../Models/Config/deviceSetupItem";
import { IInput, IMatrix, IOutput } from "../Models/Config/IMatrix"; import { IInput, IMatrix, IOutput } from "../Models/Config/matrix";
import { RemoteKey } from "../Accessories/ControlUnit"; import { RemoteKey } from "../Accessories/controlUnit";
import { EventEmitter } from "events"; import { EventEmitter } from "events";
import { IHub } from "../Models/Config/IHub"; import { IHub } from "../Models/Config/hub";
import { IDeviceConfig } from "../Models/Config/IDeviceConfig"; import { IDeviceConfig } from "../Models/Config/deviceConfig";
import { HarmonyDevice } from "../Models/HarmonyDevice"; import { HarmonyDevice } from "../Models/harmonyDevice";
import { HarmonyHub } from "../Models/HarmonyHub"; import { HarmonyHub } from "../Models/harmonyHub";
import { IConfig } from "../Models/Config"; import { IConfig } from "../Models/Config";
import { inject, injectable } from "tsyringe"; import { inject, injectable } from "tsyringe";
import { Logger, Logging } from "homebridge"; import { Logger, Logging } from "homebridge";
@ -47,6 +47,7 @@ class HarmonyDataProvider extends EventEmitter {
// this._deviceConfigs = props.deviceConfigs; // this._deviceConfigs = props.deviceConfigs;
this.connect(_config.Hubs); this.connect(_config.Hubs);
this.emitInfo();
} }
// public get devicesByHub(): { [hubName: string]: { [deviceName: string]: HarmonyDevice } } { // public get devicesByHub(): { [hubName: string]: { [deviceName: string]: HarmonyDevice } } {
@ -402,6 +403,24 @@ class HarmonyDataProvider extends EventEmitter {
return devicesToTurnOn; return devicesToTurnOn;
} }
private emitInfo(): void {
//Emit devices if requested
this._log.info("All hubs connected");
if (this._config.EmitDevicesOnStartup) {
const hubs = this.hubs;
Object.values(hubs).forEach((hub: HarmonyHub) => {
const deviceDictionary = hub.devices;
this._log.info(`${hub.hubName}`);
Object.values(deviceDictionary).forEach((device: HarmonyDevice) => {
this._log.info(` ${device.name} : ${device.id}`);
Object.keys(device.commands).forEach((command: string) => {
this._log.info(` ${command}`);
});
});
});
}
}
} }
export default HarmonyDataProvider; export default HarmonyDataProvider;

View File

@ -0,0 +1,105 @@
import { Logging } from "homebridge";
import { inject } from "tsyringe";
import { IConfig, IMatrix } from "../Models/Config";
import { IDeviceConfig } from "../Models/Config/deviceConfig";
import { IHub } from "../Models/Config/hub";
import { HarmonyDevice } from "../Models/harmonyDevice";
import { HarmonyHub } from "../Models/harmonyHub";
export class HarmonyDataProvider2 {
private _matrix: IMatrix;
private _hubs: { [hubName: string]: HarmonyHub } = {};
private _hubsByDevice: { [deviceName: string]: string } = {};
constructor(
@inject("IConfig") private _config: IConfig,
@inject("log") private _log: Logging
) {
this._matrix = _config.Matrix;
_config.Devices.forEach((deviceConfig: IDeviceConfig) => {
this._hubsByDevice[deviceConfig.Name] = deviceConfig.Hub;
});
// this._deviceConfigs = props.deviceConfigs;
this.connect(_config.Hubs);
this.emitInfo();
}
public async turnOnDevices(devices: Array<HarmonyDevice>): Promise<void> {
await Promise.all(
devices.map(async (device: HarmonyDevice) => {
if (device && device.name) {
if (!device.on) {
this._log.info(`Turning on device ${device.name}`);
await device.powerOn();
}
}
})
);
}
public async turnOffDevices(devices: Array<HarmonyDevice>) {
await Promise.all(
//Turn off devices
devices.map(async (device: HarmonyDevice) => {
if (device) {
if (device.on) {
this._log.info(`Turning off device ${device.name}`);
await device.powerOff();
}
}
})
);
}
/**
* Get the IDevice by name.
* @param deviceName The device to retrieve.
*/
public getDeviceFromName(deviceName: string): HarmonyDevice {
let device: HarmonyDevice | undefined;
try {
device =
this._hubs[this._hubsByDevice[deviceName]].getDeviceByName(deviceName);
} catch (err) {
this._log.info(`Error retrieving device from hub: ${err}`);
}
return device!;
}
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.info);
this._hubs[hub.Name] = newHarmonyHub;
newHarmonyHub.on("Ready", () => {
readyCount++;
if (readyCount === Object.keys(this._hubs).length) {
// this.emit("Ready");
}
});
await newHarmonyHub.initialize();
})
);
};
private emitInfo(): void {
//Emit devices if requested
this._log.info("All hubs connected");
if (this._config.EmitDevicesOnStartup) {
const hubs = this._hubs;
Object.values(hubs).forEach((hub: HarmonyHub) => {
const deviceDictionary = hub.devices;
this._log.info(`${hub.hubName}`);
Object.values(deviceDictionary).forEach((device: HarmonyDevice) => {
this._log.info(` ${device.name} : ${device.id}`);
Object.keys(device.commands).forEach((command: string) => {
this._log.info(` ${command}`);
});
});
});
}
}
}

View File

@ -0,0 +1,31 @@
import { inject, injectable } from "tsyringe";
import { IActivityState } from "../Models/activityState";
import { IActivity } from "../Models/Config";
@injectable()
export class StateDataProvider {
private _states: {
[controlUnitName: string]: IActivityState | undefined;
} = {};
public updateState(activity: IActivity, controlUnitName: string): void {
this._states[controlUnitName] = { currentActivity: activity };
}
public removeState(controlUnitName: string): void {
this._states[controlUnitName] = undefined;
}
public getState(controlUnitName: string) {
if (!this._states[controlUnitName]) {
return undefined;
}
return this._states[controlUnitName]!.currentActivity;
}
public get states(): {
[controlUnitName: string]: IActivityState | undefined;
} {
return this._states;
}
}

View File

@ -1,11 +0,0 @@
import { IDeviceSetupItem } from './IDeviceSetupItem';
export interface IActivity {
OutputDevice: string;
VolumeDevice: string;
ControlDevice: string;
DisplayName: string;
DeviceSetupList: Array<IDeviceSetupItem>;
UseMatrix: boolean;
}

View File

@ -0,0 +1,10 @@
import { IDeviceSetupItem } from "./deviceSetupItem";
export interface IActivity {
OutputDevice: string;
VolumeDevice: string;
ControlDevice: string;
DisplayName: string;
DeviceSetupList: Array<IDeviceSetupItem>;
UseMatrix: boolean;
}

View File

@ -1,9 +1,9 @@
import { IMatrix } from "./IMatrix"; import { IMatrix } from "./matrix";
import { IActivity } from "./IActivity"; import { IActivity } from "./activity";
import { IDeviceButton } from "./IDeviceButton"; import { IDeviceButton } from "./deviceButton";
import { IDeviceConfig } from "./IDeviceConfig"; import { IDeviceConfig } from "./deviceConfig";
import { IHub } from "./IHub"; import { IHub } from "./hub";
import { ISequence } from "./ISequence"; import { ISequence } from "./sequence";
export interface IControlUnit { export interface IControlUnit {
DisplayName: string; DisplayName: string;

View File

@ -1,5 +1,5 @@
export * from './IActivity'; export * from "./activity";
export * from './IConfig'; export * from "./config";
export * from './IDeviceButton'; export * from "./deviceButton";
export * from './IDeviceSetupItem'; export * from "./deviceSetupItem";
export * from './IMatrix'; export * from "./matrix";

View File

@ -1,5 +1,5 @@
import { ICommand } from "./IDevice"; import { ICommand } from "./device";
import { sleep } from "../Util/Sleep"; import { sleep } from "../Util/sleep";
export interface IHarmonyDeviceProps { export interface IHarmonyDeviceProps {
id: string; id: string;
@ -37,7 +37,7 @@ export class HarmonyDevice {
//Define device methods //Define device methods
public supportsCommand(commandName: string): boolean { public supportsCommand(commandName: string): boolean {
let command = this._commands[commandName]; let command = this._commands[commandName];
return (command) ? true : false; return command ? true : false;
} }
public getCommand(commandName: string): ICommand { public getCommand(commandName: string): ICommand {
@ -81,7 +81,6 @@ export class HarmonyDevice {
for (let i = 0; i < 2; i++) { for (let i = 0; i < 2; i++) {
await this._harmony.sendCommand(JSON.stringify(command)); await this._harmony.sendCommand(JSON.stringify(command));
} }
} }
await this._harmony.sendCommand(JSON.stringify(command)); await this._harmony.sendCommand(JSON.stringify(command));

View File

@ -1,12 +1,10 @@
import { HarmonyDevice } from './HarmonyDevice'; import { HarmonyDevice } from "./harmonyDevice";
const Harmony = require("harmony-websocket"); const Harmony = require("harmony-websocket");
import { ICommand } from './IDevice'; import { ICommand } from "./device";
import { EventEmitter } from 'events'; import { EventEmitter } from "events";
export class HarmonyHub extends EventEmitter { export class HarmonyHub extends EventEmitter {
private _devices: { [deviceName: string]: HarmonyDevice } = {} private _devices: { [deviceName: string]: HarmonyDevice } = {};
private _ip: string; private _ip: string;
private _harmony: any; private _harmony: any;
private _log: any; private _log: any;
@ -29,16 +27,16 @@ export class HarmonyHub extends EventEmitter {
public getDeviceByName = (deviceName: string): HarmonyDevice => { public getDeviceByName = (deviceName: string): HarmonyDevice => {
return this._devices[deviceName]; return this._devices[deviceName];
} };
public initialize = async () => { public initialize = async () => {
this._harmony = new Harmony(); this._harmony = new Harmony();
await this._harmony.connect(this._ip); await this._harmony.connect(this._ip);
this._harmony.on('stateDigest', (data: any) => { this._harmony.on("stateDigest", (data: any) => {
console.log(data); console.log(data);
}); });
this._harmony.on('automationState', (data: any) => { this._harmony.on("automationState", (data: any) => {
console.log(data); console.log(data);
}); });
@ -50,7 +48,9 @@ export class HarmonyHub extends EventEmitter {
devices.map(async (dev: any) => { devices.map(async (dev: any) => {
//get commands //get commands
let commands: { [name: string]: ICommand } = {}; let commands: { [name: string]: ICommand } = {};
let deviceCommands: any = await this._harmony.getDeviceCommands(dev.id); let deviceCommands: any = await this._harmony.getDeviceCommands(
dev.id
);
deviceCommands.forEach((command: any) => { deviceCommands.forEach((command: any) => {
commands[command.label] = command.action; commands[command.label] = command.action;
}); });
@ -59,17 +59,17 @@ export class HarmonyHub extends EventEmitter {
name: dev.label, name: dev.label,
commands: commands, commands: commands,
log: this._log, log: this._log,
harmony: this._harmony harmony: this._harmony,
}); });
})); })
);
this.emit("Ready"); this.emit("Ready");
} catch (err) { } catch (err) {
this._log(`ERROR - error connecting to harmony: ${err}`); this._log(`ERROR - error connecting to harmony: ${err}`);
} }
} };
private connect = async (): Promise<void> => { private connect = async (): Promise<void> => {
await this._harmony.Connect(this._ip); await this._harmony.Connect(this._ip);
} };
} }

View File

@ -0,0 +1,5 @@
import { IActivity } from "./Config";
export interface IActivityState {
currentActivity: IActivity;
}

View File

@ -1 +1 @@
export * from './IDevice'; export * from "./device";

View File

@ -0,0 +1,209 @@
import { Logging } from "homebridge";
import { inject, injectable } from "tsyringe";
import { HarmonyDataProvider2 } from "../DataProviders/harmonyDataProvider2";
import { StateDataProvider } from "../DataProviders/stateDataProvider";
import { IActivityState } from "../Models/activityState";
import {
IActivity,
IConfig,
IDeviceSetupItem,
IInput,
IOutput,
} from "../Models/Config";
import { HarmonyDevice } from "../Models/harmonyDevice";
@injectable()
export class ActivityService {
constructor(
@inject(HarmonyDataProvider2)
private _harmonyDataProvider: HarmonyDataProvider2,
@inject(StateDataProvider) private _stateDataProvider: StateDataProvider,
@inject("IConfig") private _config: IConfig,
@inject("log") private _log: Logging
) {}
public startActivity = async (
controlUnitName: string,
activity: IActivity
) => {
this._log.info(
`Starting activity ${activity.DisplayName} for controlUnit: ${controlUnitName}`
);
let devicesToTurnOn: Array<HarmonyDevice> = this.getDevicesToTurnOn(
activity,
controlUnitName
);
await this._harmonyDataProvider.turnOnDevices(devicesToTurnOn);
await this.assignDeviceInput(activity);
await this.routeInputsToOutputs(activity);
let lastActivity = this.getCurrentActivity(controlUnitName);
let devicesToTurnOff: Array<HarmonyDevice> = this.getDevicesToTurnOff(
controlUnitName,
lastActivity,
activity
);
await this._harmonyDataProvider.turnOffDevices(devicesToTurnOff);
this._stateDataProvider.updateState(activity, controlUnitName);
};
public stopCurrentActivity = async (
controlUnitName: string
): Promise<void> => {
if (!this.getCurrentActivity(controlUnitName)) {
return;
}
let lastActivity: IActivity | undefined =
this.getCurrentActivity(controlUnitName);
let devicesToTurnOff: Array<HarmonyDevice> = this.getDevicesToTurnOff(
controlUnitName,
lastActivity
);
await this._harmonyDataProvider.turnOffDevices(devicesToTurnOff);
this._stateDataProvider.removeState(controlUnitName);
};
public getCurrentActivity(controlUnitName: string): IActivity | undefined {
return this._stateDataProvider.getState(controlUnitName);
}
/**
* Return if a control unit is active
* @param controlUnitName
*/
public getIsActive(controlUnitName: string): IActivity | undefined {
return this._stateDataProvider.getState(controlUnitName);
}
/**
* 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._stateDataProvider.states) {
//Skip self
if (controlUnitKey === controlUnitName) {
continue;
}
let currentOtherState: IActivityState =
this._stateDataProvider.states[controlUnitKey]!;
if (currentOtherState) {
currentOtherState.currentActivity.DeviceSetupList.forEach(
(value: IDeviceSetupItem) => {
//there are devices to remove
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)];
}
}
);
}
}
return devicesToTurnOn;
}
private getDevicesToTurnOn(
activity: IActivity,
controlUnitName: string
): Array<HarmonyDevice> {
let potentialDevices = this.buildPotentialDeviceList(activity);
return this.sanitizeDeviceList(potentialDevices, controlUnitName);
}
private getDevicesToTurnOff(
controlUnitName: string,
lastActivity?: IActivity,
nextActivity?: IActivity
) {
let potentialDevices = lastActivity
? this.buildPotentialDeviceList(lastActivity)
: [];
//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
if (nextActivity) {
potentialDevices.forEach((device: HarmonyDevice, index: number) => {
if (
device &&
device.name &&
nextActivity.DeviceSetupList.some((e) => {
return e && e.DeviceName === device.name;
})
) {
delete potentialDevices[index];
}
});
}
return this.sanitizeDeviceList(potentialDevices, controlUnitName);
}
private buildPotentialDeviceList(activity: IActivity): HarmonyDevice[] {
return activity.DeviceSetupList.map(
(value: IDeviceSetupItem): HarmonyDevice => {
return this._harmonyDataProvider.getDeviceFromName(value.DeviceName);
}
);
}
private async assignDeviceInput(activity: IActivity): Promise<void> {
await Promise.all(
activity.DeviceSetupList.map(async (value: IDeviceSetupItem) => {
let device: HarmonyDevice = this._harmonyDataProvider.getDeviceFromName(
value.DeviceName
);
if (device && device.supportsCommand(`Input${value.Input}`)) {
await device.sendCommand(`Input${value.Input}`);
}
})
);
}
private async routeInputsToOutputs(activity: IActivity) {
if (activity.UseMatrix) {
//get input and output
let input: IInput = this._config.Matrix.Inputs.filter(
(e) => e.InputDevice === activity.ControlDevice
)[0];
let output: IOutput = this._config.Matrix.Outputs.filter(
(e) => e.OutputDevice === activity.OutputDevice
)[0];
let inputCommandName: string = `In ${input.InputNumber}`;
let outputCommandName: string = `Out ${output.OutputLetter}`;
let matrixDevice: HarmonyDevice =
this._harmonyDataProvider.getDeviceFromName(
this._config.Matrix.DeviceName
);
//Route hdmi
if (
matrixDevice.supportsCommand(inputCommandName) &&
matrixDevice.supportsCommand(outputCommandName)
) {
await matrixDevice.sendCommand(outputCommandName);
await matrixDevice.sendCommand(inputCommandName);
await matrixDevice.sendCommand(outputCommandName);
await matrixDevice.sendCommand(inputCommandName);
}
}
}
}

View File

@ -0,0 +1,71 @@
import { inject } from "tsyringe";
import { RemoteKey } from "../Accessories/controlUnit";
import { HarmonyDataProvider2 } from "../DataProviders/harmonyDataProvider2";
import { StateDataProvider } from "../DataProviders/stateDataProvider";
import { IConfig } from "../Models/Config";
import { HarmonyDevice } from "../Models/harmonyDevice";
export class CommandService {
constructor(
@inject(StateDataProvider) private _stateDataProvider: StateDataProvider,
@inject(HarmonyDataProvider2)
private _harmonyDataProvider: HarmonyDataProvider2
) {}
/**
* 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) => {
let currentActivity = this._stateDataProvider.getState(controlUnitName);
if (currentActivity) {
let commandName: string = "";
let device: HarmonyDevice = this._harmonyDataProvider.getDeviceFromName(
currentActivity.ControlDevice
);
switch (key) {
case RemoteKey.ARROW_UP: {
commandName = "Direction Up";
break;
}
case RemoteKey.ARROW_DOWN: {
commandName = "Direction Down";
break;
}
case RemoteKey.ARROW_LEFT: {
commandName = "Direction Left";
break;
}
case RemoteKey.ARROW_RIGHT: {
commandName = "Direction Right";
break;
}
case RemoteKey.SELECT: {
commandName = "Select";
break;
}
case RemoteKey.PLAY_PAUSE: {
commandName = "Pause";
break;
}
case RemoteKey.INFORMATION: {
commandName = "Menu";
break;
}
case RemoteKey.BACK: {
commandName = "Back";
break;
}
case RemoteKey.EXIT: {
commandName = "Back";
break;
}
}
await device.sendCommand(commandName);
}
};
}

View File

@ -0,0 +1,44 @@
import { Logging } from "homebridge";
import { inject } from "tsyringe";
import { HarmonyDataProvider2 } from "../DataProviders/harmonyDataProvider2";
import { StateDataProvider } from "../DataProviders/stateDataProvider";
import { IConfig } from "../Models/Config";
import { HarmonyDevice } from "../Models/harmonyDevice";
export class VolumeService {
constructor(
@inject(StateDataProvider) private _stateDataProvider: StateDataProvider,
@inject(HarmonyDataProvider2)
private _harmonyDataProvider: HarmonyDataProvider2
) {}
/**
* Turn the volume up for the current running activity.
*/
public volumeUp = async (controlUnitName: string) => {
let volumeUpCommand: string = "Volume Up";
let currentActivity = this._stateDataProvider.getState(controlUnitName);
if (currentActivity) {
let volumeDevice: HarmonyDevice =
this._harmonyDataProvider.getDeviceFromName(
currentActivity.VolumeDevice
);
await volumeDevice.sendCommand(volumeUpCommand);
}
};
/**
* Volume down for current running activity.
*/
public volumeDown = async (controlUnitName: string) => {
let volumeDownCommand: string = "Volume Down";
let currentActivity = this._stateDataProvider.getState(controlUnitName);
if (currentActivity) {
let volumeDevice: HarmonyDevice =
this._harmonyDataProvider.getDeviceFromName(
currentActivity.VolumeDevice
);
await volumeDevice.sendCommand(volumeDownCommand);
}
};
}

View File

@ -1,2 +1,2 @@
export * from "./Callbackify"; export * from "./callbackify";
export * from "./Sleep"; export * from "./sleep";

View File

@ -9,12 +9,17 @@ import {
Service, Service,
} from "homebridge"; } from "homebridge";
import { ControlUnit, DeviceButton } from "./Accessories"; import { ControlUnit, DeviceButton } from "./Accessories";
import { Sequence } from "./Accessories/Sequence"; import { Sequence } from "./Accessories/sequence";
import HarmonyDataProvider from "./DataProviders/HarmonyDataProvider"; import HarmonyDataProvider from "./DataProviders/harmonyDataProvider";
import { IConfig, IControlUnit, IDeviceButton } from "./Models/Config"; import { IConfig, IControlUnit, IDeviceButton } from "./Models/Config";
import { ISequence } from "./Models/Config/ISequence"; import { ISequence } from "./Models/Config/sequence";
import { PLATFORM_NAME, PLUGIN_NAME } from "./settings"; import { PLATFORM_NAME, PLUGIN_NAME } from "./settings";
import { container } from "tsyringe"; import { container } from "tsyringe";
import { HarmonyDataProvider2 } from "./DataProviders/harmonyDataProvider2";
import { StateDataProvider } from "./DataProviders/stateDataProvider";
import { CommandService } from "./Services/commandService";
import { ActivityService } from "./Services/activityService";
import { VolumeService } from "./Services/volumeService";
export class Platform implements DynamicPlatformPlugin { export class Platform implements DynamicPlatformPlugin {
constructor( constructor(
@ -26,14 +31,6 @@ export class Platform implements DynamicPlatformPlugin {
this.config = config as unknown as IConfig; this.config = config as unknown as IConfig;
this.register(); this.register();
//construct data provider
// const dataProvider = new HarmonyDataProvider({
// hubs: this.config.Hubs,
// deviceConfigs: this.config.Devices,
// matrix: this.config.Matrix,
// log: this.log,
// });
let didFinishLaunching = false; let didFinishLaunching = false;
this.api.on("didFinishLaunching", async () => { this.api.on("didFinishLaunching", async () => {
log.debug("Executed didFinishLaunching callback"); log.debug("Executed didFinishLaunching callback");
@ -43,39 +40,6 @@ export class Platform implements DynamicPlatformPlugin {
this.pruneAccessories(); this.pruneAccessories();
didFinishLaunching = true; didFinishLaunching = true;
}); });
// 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.info("All hubs connected");
// this.discoverControlUnitAccessories(dataProvider);
// this.discoverDeviceButtonAccessories(dataProvider);
// this.discoverSequenceAccessories(dataProvider);
// this.pruneAccessories();
// if (this.config.EmitDevicesOnStartup) {
// const hubs = this.dataProvider!.hubs;
// Object.values(hubs).forEach((hub: HarmonyHub) => {
// const deviceDictionary = hub.devices;
// this.log.info(`${hub.hubName}`);
// Object.values(deviceDictionary).forEach((device: HarmonyDevice) => {
// this.log.info(` ${device.name} : ${device.id}`);
// Object.keys(device.commands).forEach((command: string) => {
// this.log.info(` ${command}`);
// });
// });
// });
// }
// });
}
} }
public readonly Service: typeof Service = this.api.hap.Service; public readonly Service: typeof Service = this.api.hap.Service;
@ -104,7 +68,9 @@ export class Platform implements DynamicPlatformPlugin {
new ControlUnit( new ControlUnit(
this, this,
existingAccessory, existingAccessory,
container.resolve(HarmonyDataProvider), container.resolve(ActivityService),
container.resolve(CommandService),
container.resolve(VolumeService),
unit.Activities unit.Activities
); );
@ -122,7 +88,9 @@ export class Platform implements DynamicPlatformPlugin {
new ControlUnit( new ControlUnit(
this, this,
accessory, accessory,
container.resolve(HarmonyDataProvider), container.resolve(ActivityService),
container.resolve(CommandService),
container.resolve(VolumeService),
unit.Activities unit.Activities
); );
@ -253,6 +221,9 @@ export class Platform implements DynamicPlatformPlugin {
container.register<Platform>(Platform, { useValue: this }); container.register<Platform>(Platform, { useValue: this });
container.register<IConfig>("IConfig", { useValue: this.config }); container.register<IConfig>("IConfig", { useValue: this.config });
container.register<Logger>("log", { useValue: this.log }); container.register<Logger>("log", { useValue: this.log });
container.registerSingleton<StateDataProvider>(StateDataProvider);
container.registerSingleton<CommandService>(CommandService);
container.registerSingleton<HarmonyDataProvider>(HarmonyDataProvider); container.registerSingleton<HarmonyDataProvider>(HarmonyDataProvider);
container.registerSingleton<HarmonyDataProvider2>(HarmonyDataProvider2);
} }
} }