Removing business logic from models. First pass at overrides feature
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Brandon Watson
2021-12-28 20:11:13 -05:00
parent c22a8a0325
commit e6e3d45b5b
9 changed files with 148 additions and 88 deletions

View File

@ -1,4 +1,5 @@
export interface IDeviceConfig {
Name: string;
Hub: string;
}
Name: string;
Hub: string;
Overrides: Array<{ Command: string; Override: string }>;
}

View File

@ -10,15 +10,12 @@ export interface IHarmonyDeviceProps {
}
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;
}
@ -30,6 +27,10 @@ export class HarmonyDevice {
return this._on;
}
public set on(value: boolean) {
this._on = value;
}
public get commands(): { [name: string]: ICommand } {
return this._commands;
}
@ -43,51 +44,4 @@ export class HarmonyDevice {
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}`);
}
}
}

View File

@ -2,6 +2,7 @@ import { HarmonyDevice } from "./harmonyDevice";
const Harmony = require("harmony-websocket");
import { ICommand } from "./device";
import { EventEmitter } from "events";
import { sleep } from "../util";
export class HarmonyHub extends EventEmitter {
private _devices: { [deviceName: string]: HarmonyDevice } = {};
@ -72,4 +73,22 @@ export class HarmonyHub extends EventEmitter {
private connect = async (): Promise<void> => {
await this._harmony.Connect(this._ip);
};
public async sendCommand(command: ICommand): Promise<void> {
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}`);
}
}
}