This commit is contained in:
5
src/models/activityState.ts
Normal file
5
src/models/activityState.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { IActivity } from "./config";
|
||||
|
||||
export interface IActivityState {
|
||||
currentActivity: IActivity;
|
||||
}
|
10
src/models/config/activity.ts
Normal file
10
src/models/config/activity.ts
Normal 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;
|
||||
}
|
22
src/models/config/config.ts
Normal file
22
src/models/config/config.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { IMatrix } from "./matrix";
|
||||
import { IActivity } from "./activity";
|
||||
import { IDeviceButton } from "./deviceButton";
|
||||
import { IDeviceConfig } from "./deviceConfig";
|
||||
import { IHub } from "./hub";
|
||||
import { ISequence } from "./sequence";
|
||||
|
||||
export interface IControlUnit {
|
||||
DisplayName: string;
|
||||
Activities: Array<IActivity>;
|
||||
}
|
||||
|
||||
export interface IConfig {
|
||||
hubIp: string;
|
||||
EmitDevicesOnStartup: boolean;
|
||||
Matrix: IMatrix;
|
||||
ControlUnits: Array<IControlUnit>;
|
||||
DeviceButtons: Array<IDeviceButton>;
|
||||
Sequences: Array<ISequence>;
|
||||
Devices: Array<IDeviceConfig>;
|
||||
Hubs: Array<IHub>;
|
||||
}
|
7
src/models/config/deviceButton.ts
Normal file
7
src/models/config/deviceButton.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export interface IDeviceButton {
|
||||
DeviceName: string;
|
||||
ButtonName: string;
|
||||
DisplayName: string;
|
||||
NumberOfKeyPresses: number;
|
||||
IsStateful: boolean;
|
||||
}
|
4
src/models/config/deviceConfig.ts
Normal file
4
src/models/config/deviceConfig.ts
Normal file
@ -0,0 +1,4 @@
|
||||
export interface IDeviceConfig {
|
||||
Name: string;
|
||||
Hub: string;
|
||||
}
|
5
src/models/config/deviceSetupItem.ts
Normal file
5
src/models/config/deviceSetupItem.ts
Normal file
@ -0,0 +1,5 @@
|
||||
|
||||
export interface IDeviceSetupItem {
|
||||
DeviceName: string;
|
||||
Input: string;
|
||||
}
|
5
src/models/config/hub.ts
Normal file
5
src/models/config/hub.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export interface IHub {
|
||||
Name: string;
|
||||
Ip: string;
|
||||
Harmony: any;
|
||||
}
|
5
src/models/config/index.ts
Normal file
5
src/models/config/index.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export * from "./activity";
|
||||
export * from "./config";
|
||||
export * from "./deviceButton";
|
||||
export * from "./deviceSetupItem";
|
||||
export * from "./matrix";
|
15
src/models/config/matrix.ts
Normal file
15
src/models/config/matrix.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;
|
||||
}
|
10
src/models/config/sequence.ts
Normal file
10
src/models/config/sequence.ts
Normal file
@ -0,0 +1,10 @@
|
||||
export interface ISequence {
|
||||
DisplayName: string;
|
||||
Steps: Array<IStep>;
|
||||
}
|
||||
|
||||
export interface IStep {
|
||||
DeviceName?: string;
|
||||
DeviceCommand?: string;
|
||||
Delay: number;
|
||||
}
|
19
src/models/device.ts
Normal file
19
src/models/device.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;
|
||||
}
|
93
src/models/harmonyDevice.ts
Normal file
93
src/models/harmonyDevice.ts
Normal file
@ -0,0 +1,93 @@
|
||||
import { ICommand } from "./device";
|
||||
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}`);
|
||||
}
|
||||
}
|
||||
}
|
75
src/models/harmonyHub.ts
Normal file
75
src/models/harmonyHub.ts
Normal file
@ -0,0 +1,75 @@
|
||||
import { HarmonyDevice } from "./harmonyDevice";
|
||||
const Harmony = require("harmony-websocket");
|
||||
import { ICommand } from "./device";
|
||||
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);
|
||||
this._harmony.on("stateDigest", (data: any) => {
|
||||
console.log(data);
|
||||
});
|
||||
|
||||
this._harmony.on("automationState", (data: any) => {
|
||||
console.log(data);
|
||||
});
|
||||
|
||||
//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);
|
||||
};
|
||||
}
|
1
src/models/index.ts
Normal file
1
src/models/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from "./device";
|
Reference in New Issue
Block a user