Removing business logic from models. First pass at overrides feature
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,65 +1,83 @@
|
||||
import { Logging } from "homebridge";
|
||||
import { inject, injectable } from "tsyringe";
|
||||
import { ICommand } from "../models";
|
||||
import { IConfig } 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";
|
||||
import { sleep } from "../util";
|
||||
|
||||
@injectable()
|
||||
export class HarmonyDataProvider {
|
||||
private _hubs: { [hubName: string]: HarmonyHub } = {};
|
||||
private _hubsByDevice: { [deviceName: string]: string } = {};
|
||||
private _deviceConfigByName: { [deviceName: string]: IDeviceConfig } = {};
|
||||
|
||||
constructor(
|
||||
@inject("IConfig") private _config: IConfig,
|
||||
@inject("log") private _log: Logging
|
||||
) {
|
||||
_config.Devices.forEach((deviceConfig: IDeviceConfig) => {
|
||||
this._hubsByDevice[deviceConfig.Name] = deviceConfig.Hub;
|
||||
this._deviceConfigByName[deviceConfig.Name] = deviceConfig;
|
||||
});
|
||||
// this._deviceConfigs = props.deviceConfigs;
|
||||
|
||||
this.connect(_config.Hubs);
|
||||
this.emitInfo();
|
||||
}
|
||||
|
||||
public async turnOnDevices(devices: Array<HarmonyDevice>): Promise<void> {
|
||||
public async powerOnDevices(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();
|
||||
await this.powerOnDevice(device);
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
public async turnOffDevices(devices: Array<HarmonyDevice>) {
|
||||
public async powerOffDevices(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();
|
||||
await this.powerOffDevice(device);
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
public async sendCommand(
|
||||
commandName: string,
|
||||
harmonyDevice: HarmonyDevice
|
||||
): Promise<void> {
|
||||
let command!: ICommand;
|
||||
|
||||
commandName = this.getOverrideCommand(commandName, harmonyDevice);
|
||||
|
||||
if (harmonyDevice.supportsCommand(commandName)) {
|
||||
command = harmonyDevice.getCommand(commandName);
|
||||
}
|
||||
const hub = this.getHubByDevice(harmonyDevice);
|
||||
await hub.sendCommand(command);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the IDevice by name.
|
||||
* @param deviceName The device to retrieve.
|
||||
*/
|
||||
public getDeviceFromName(deviceName: string): HarmonyDevice {
|
||||
public getDeviceByName(deviceName: string): HarmonyDevice {
|
||||
let device: HarmonyDevice | undefined;
|
||||
try {
|
||||
device =
|
||||
this._hubs[this._hubsByDevice[deviceName]].getDeviceByName(deviceName);
|
||||
this._hubs[this._deviceConfigByName[deviceName].Hub].getDeviceByName(
|
||||
deviceName
|
||||
);
|
||||
} catch (err) {
|
||||
this._log.info(`Error retrieving device from hub: ${err}`);
|
||||
}
|
||||
@@ -101,4 +119,49 @@ export class HarmonyDataProvider {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private getHubByDevice(device: HarmonyDevice) {
|
||||
return this._hubs[this._deviceConfigByName[device.name].Hub];
|
||||
}
|
||||
|
||||
private async powerOnDevice(harmonyDevice: HarmonyDevice): Promise<void> {
|
||||
let powerOnCommand: string = "Power On";
|
||||
let powerToggleCommand: string = "Power Toggle";
|
||||
if (harmonyDevice.supportsCommand(powerOnCommand)) {
|
||||
await this.sendCommand(powerOnCommand, harmonyDevice);
|
||||
harmonyDevice.on = true;
|
||||
} else if (harmonyDevice.supportsCommand(powerToggleCommand)) {
|
||||
await this.sendCommand(powerToggleCommand, harmonyDevice);
|
||||
harmonyDevice.on = true;
|
||||
} else {
|
||||
await this.sendCommand(powerOnCommand, harmonyDevice);
|
||||
}
|
||||
}
|
||||
|
||||
private async powerOffDevice(harmonyDevice: HarmonyDevice): Promise<void> {
|
||||
let powerOffCommand: string = "Power Off";
|
||||
let powerToggleCommand: string = "Power Toggle";
|
||||
if (harmonyDevice.supportsCommand(powerOffCommand)) {
|
||||
await this.sendCommand(powerOffCommand, harmonyDevice);
|
||||
harmonyDevice.on = false;
|
||||
} else if (harmonyDevice.supportsCommand(powerToggleCommand)) {
|
||||
await this.sendCommand(powerToggleCommand, harmonyDevice);
|
||||
harmonyDevice.on = false;
|
||||
}
|
||||
}
|
||||
|
||||
private getOverrideCommand(
|
||||
commandName: string,
|
||||
harmonyDevice: HarmonyDevice
|
||||
) {
|
||||
const deviceConfig: IDeviceConfig =
|
||||
this._deviceConfigByName[harmonyDevice.name];
|
||||
if (!deviceConfig.Overrides) {
|
||||
return commandName;
|
||||
}
|
||||
const overrideCommand = deviceConfig.Overrides.find(
|
||||
(e) => e.Command == commandName
|
||||
);
|
||||
return overrideCommand ? overrideCommand.Override : commandName;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user