Progress commit

This commit is contained in:
watsonb8
2020-01-23 08:44:11 -05:00
parent e9693435ce
commit 5ddd1e1af0
9 changed files with 195 additions and 95 deletions

View File

@ -1,6 +1,8 @@
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;
@ -9,8 +11,10 @@ export interface IControlUnit {
export interface IConfig {
hubIp: string;
EmitDevicesOnStartup: boolean,
Matrix: IMatrix
ControlUnits: Array<IControlUnit>
DeviceButtons: Array<IDeviceButton>
EmitDevicesOnStartup: boolean;
Matrix: IMatrix;
ControlUnits: Array<IControlUnit>;
DeviceButtons: Array<IDeviceButton>;
Devices: Array<IDeviceConfig>;
Hubs: Array<IHub>;
}

View File

@ -2,5 +2,6 @@ export interface IDeviceButton {
DeviceName: string;
ButtonName: string;
DisplayName: string;
NumberOfKeyPresses: number;
IsStateful: boolean;
}

View File

@ -0,0 +1,4 @@
export interface IDeviceConfig {
Name: string;
Hub: string;
}

View File

@ -0,0 +1,5 @@
export interface IHub {
Name: string;
Ip: string;
Harmony: any;
}

View File

@ -0,0 +1,79 @@
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;
//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
await this._harmony.sendCommand(JSON.stringify(command));
//Sleep
await sleep(800);
} catch (err) {
this._log(`ERROR - error sending command to harmony: ${err}`);
}
}
}

View File

@ -5,10 +5,15 @@ export interface ICommand {
}
export interface IDevice {
id: string,
name: string,
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;
}