Good start. Debugging needed for both harmony and neeo

This commit is contained in:
watsonb8
2019-07-29 22:31:14 -04:00
parent 31c5c5b9e2
commit 2475aea1c8
9 changed files with 153 additions and 37 deletions

View File

@ -5,6 +5,7 @@ import { IDevice } from './IDevice';
import { RemoteKey } from '../Accessories/ControlUnit';
import { sleep } from '../Util/Sleep';
import { hub } from "..";
import fetch from 'node-fetch';
let Characteristic: HAPNodeJS.Characteristic;
@ -21,6 +22,12 @@ interface IHubDataProviderProps {
matrix: Matrix
}
interface INeeoMacro {
roomKey: string,
macroKey: string,
deviceKey: string,
}
class HubDataProvider {
//Harmony Fields
private harmonyHubAddress: string;
@ -45,15 +52,18 @@ class HubDataProvider {
if (this.harmonyHubAddress) {
try {
this.connectHarmony(this.harmonyHubAddress);
Promise.resolve(this.connectHarmony(this.harmonyHubAddress));
} catch (err) {
this.log(`Error connecting to harmony hub at ${this.harmonyHubAddress}: ${err}`);
}
}
if (this.neeoHubAddress) {
try {
Promise.resolve(this.connectNeeo(this.neeoHubAddress));
} catch (err) {
this.log(`Error connecting to neeo hub at ${this.neeoHubAddress}: ${err}`)
}
}
}
/**
@ -77,7 +87,7 @@ class HubDataProvider {
//Build potential list of devices to turn off
let devicesToTurnOff: Array<IDevice> = this.states[controlUnitName]!.currentActivity.deviceSetupItems
.map((value: DeviceSetupItem): IDevice => {
return this.getDeviceFromName(value.deviceName);
return this.getDeviceFromName(value.deviceName, value.hubType);
});
//Resolve device conflicts with other controlUnits
@ -106,7 +116,7 @@ class HubDataProvider {
//Build potential list of devices to to turn on
let devicesToTurnOn: Array<IDevice> = activity.deviceSetupItems.map((value: DeviceSetupItem): IDevice => {
return this.getDeviceFromName(value.deviceName);
return this.getDeviceFromName(value.deviceName, value.hubType);
});
//Resolve device conflicts with other controlUnits
@ -125,7 +135,7 @@ class HubDataProvider {
//Assign correct input
await Promise.all(
activity.deviceSetupItems.map(async (value: DeviceSetupItem) => {
let device: IDevice = this.getDeviceFromName(value.deviceName);
let device: IDevice = this.getDeviceFromName(value.deviceName, value.hubType);
if (device && device.supportsCommand(`Input ${value.input}`)) {
let command: string = device.getCommand(`Input ${value.input}`);
@ -142,7 +152,7 @@ class HubDataProvider {
let inputCommandName: string = `In ${input.inputNumber}`;
let outputCommandName: string = `Out ${output.outputLetter}`;
let matrixDevice: IDevice = this.getDeviceFromName(this.matrix.deviceName);
let matrixDevice: IDevice = this.getDeviceFromName(this.matrix.deviceName, this.matrix.hubType);
//Route hdmi
if (matrixDevice.supportsCommand(inputCommandName) && matrixDevice.supportsCommand(outputCommandName)) {
@ -154,7 +164,7 @@ class HubDataProvider {
//Build potential list of devices to turn off
if (lastActivity) {
let devicesToTurnOff: Array<IDevice> = lastActivity.deviceSetupItems.map((value: DeviceSetupItem): IDevice => {
return this.getDeviceFromName(value.deviceName);
return this.getDeviceFromName(value.deviceName, value.hubType);
});
//remove devices that will be used for next activity from list
@ -195,8 +205,11 @@ class HubDataProvider {
*/
public volumeUp = async (controlUnitName: string) => {
let volumeUpCommand: string = "Volume Up"
let currentActivity: Activity = this.states[controlUnitName]!.currentActivity;
let deviceSetupItem: DeviceSetupItem = currentActivity.deviceSetupItems.filter(e => e.deviceName == currentActivity.volumeDeviceId)[0];
if (this.states[controlUnitName]) {
let volumeDevice: IDevice = this.getDeviceFromName(this.states[controlUnitName]!.currentActivity.volumeDeviceId);
let volumeDevice: IDevice = this.getDeviceFromName(currentActivity.volumeDeviceId, deviceSetupItem.hubType);
if (volumeDevice.supportsCommand(volumeUpCommand)) {
volumeDevice.sendCommand(volumeDevice.getCommand(volumeUpCommand));
}
@ -208,8 +221,11 @@ class HubDataProvider {
*/
public volumeDown = async (controlUnitName: string) => {
let volumeDownCommand: string = "Volume Down"
let currentActivity: Activity = this.states[controlUnitName]!.currentActivity;
let deviceSetupItem: DeviceSetupItem = currentActivity.deviceSetupItems.filter(e => e.deviceName == currentActivity.volumeDeviceId)[0];
if (this.states[controlUnitName]) {
let volumeDevice: IDevice = this.getDeviceFromName(this.states[controlUnitName]!.currentActivity.volumeDeviceId);
let volumeDevice: IDevice = this.getDeviceFromName(currentActivity.volumeDeviceId, deviceSetupItem.hubType);
if (volumeDevice.supportsCommand(volumeDownCommand)) {
volumeDevice.sendCommand(volumeDevice.getCommand(volumeDownCommand));
}
@ -261,7 +277,10 @@ class HubDataProvider {
if (this.states[controlUnitName]) {
let commandName: string = "";
let device: IDevice = this.getDeviceFromName(this.states[controlUnitName]!.currentActivity.controlDeviceId);
let currentActivity: Activity = this.states[controlUnitName]!.currentActivity;
let deviceSetupItem: DeviceSetupItem = currentActivity.deviceSetupItems.filter(e => e.deviceName == currentActivity.volumeDeviceId)[0];
let device: IDevice = this.getDeviceFromName(this.states[controlUnitName]!.currentActivity.controlDeviceId, deviceSetupItem.hubType);
switch (key) {
case RemoteKey.ARROW_UP: {
commandName = "Direction Up";
@ -311,8 +330,8 @@ class HubDataProvider {
* Get the IDevice by name.
* @param deviceName The device to retrieve.
*/
private getDeviceFromName(deviceName: string): IDevice {
return this.devices[deviceName];
private getDeviceFromName(deviceName: string, hubType: hub): IDevice {
return this.devices[`${hubType}-${deviceName}`];
}
/**
@ -343,7 +362,7 @@ class HubDataProvider {
deviceCommands.forEach((command: any) => {
commands[command.label] = command.action;
});
self.devices[dev.label] = {
self.devices[`Harmony-${dev.label}`] = {
id: dev.id,
name: dev.label,
commands: commands,
@ -405,6 +424,84 @@ class HubDataProvider {
private connectNeeo = async (address: string): Promise<void> => {
let baseUrl = `http://${address}:3000/v1/projects/home`;
let response: any = await fetch(`${baseUrl}/rooms`);
let rooms: { [key: string]: any } = await response.json();
function regularizeString(str: string) {
str = str.toLowerCase();
return str.replace(/(?:^|\s)\S/g, function (a) { return a.toUpperCase(); });
}
for (let key in rooms) {
let devices: { [key: string]: any } = rooms[key].devices;
for (let deviceKey in devices) {
let device = devices[deviceKey];
//shape commands from macros
let commands: { [item: string]: any } = {};
for (let macroKey in device.macros) {
let macro: any = device.macros[macroKey];
commands[regularizeString(macro.name)] = {
deviceKey: macro.deviceKey,
macroKey: macro.key,
roomKey: macro.roomKey,
};
}
this.devices[`Neeo-${deviceKey}`] = {
id: device.key,
name: device.name,
log: this.log,
hubType: "Neeo",
commands: commands,
on: false,
supportsCommand(commandName: string): boolean {
let command = this.commands[commandName];
return (command) ? true : false;
},
getCommand(commandName: string): string {
return this.commands[commandName];
},
async sendCommand(command: string): Promise<void> {
if (this.supportsCommand(command)) {
let macro: INeeoMacro = this.commands[command];
let url: string = `${baseUrl}/rooms/${macro.roomKey}/devices/${macro.deviceKey}/macros/${macro.macroKey}/trigger`;
this.log(`Sending command ${url}`);
fetch(`${baseUrl}/rooms/${macro.roomKey}/devices/${macro.deviceKey}/macros/${macro.macroKey}/trigger`);
}
},
async powerOn(): Promise<void> {
let powerOnCommand: string = "Power On";
let powerToggleCommand: string = "Power Toggle";
if (this.supportsCommand(powerOnCommand)) {
await this.sendCommand(this.getCommand(powerOnCommand));
this.on = true;
} else if (this.supportsCommand(powerToggleCommand)) {
await this.sendCommand(this.getCommand(powerToggleCommand));
this.on = true;
}
},
async powerOff(): Promise<void> {
let powerOffCommand: string = "Power Off";
let powerToggleCommand: string = "Power Toggle";
if (this.supportsCommand(powerOffCommand)) {
await this.sendCommand(this.getCommand(powerOffCommand));
this.on = false;
} else if (this.supportsCommand(powerToggleCommand)) {
await this.sendCommand(this.getCommand(powerToggleCommand));
this.on = false;
}
}
}
}
}
this.log(`Device list: ${JSON.stringify(this.devices)}`);
}
}

View File

@ -3,7 +3,7 @@ import { hub } from "..";
export interface IDevice {
id: string,
name: string,
harmony: any | undefined,
harmony?: any,
log: any,
hubType: hub
supportsCommand(commandName: string): boolean,
@ -11,6 +11,6 @@ export interface IDevice {
sendCommand(command: string): void,
powerOn(): Promise<void>,
powerOff(): Promise<void>,
commands: { [name: string]: string };
commands: { [name: string]: any };
on: boolean;
}