Skip to content

Commit

Permalink
Capitalize type names
Browse files Browse the repository at this point in the history
  • Loading branch information
DallasHoff committed Sep 5, 2022
1 parent 5c219fb commit e6ddfba
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 45 deletions.
30 changes: 15 additions & 15 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { API, DynamicPlatformPlugin, Logger, PlatformAccessory, PlatformConfig,
import { PLATFORM_NAME, PLUGIN_NAME, DEFAULT_DISCOVERY_INTERVAL, SERVER_CONNECTION_TIMEOUT } from './settings';
import { OpenRgbPlatformAccessory } from './platformAccessory';

import { rgbServer, rgbDevice, rgbDeviceContext } from './rgb';
import { RgbServer, RgbDevice, RgbDeviceContext } from './rgb';
import { Client as OpenRGB } from 'openrgb-sdk';
import { getDeviceLedRgbColor, findDeviceModeId, isLedOff } from './utils';

Expand All @@ -17,7 +17,7 @@ export class OpenRgbPlatform implements DynamicPlatformPlugin {
public readonly Characteristic: typeof Characteristic = this.api.hap.Characteristic;

// this is used to track restored cached accessories
public accessories: PlatformAccessory<rgbDeviceContext>[] = [];
public accessories: PlatformAccessory<RgbDeviceContext>[] = [];

// track which accessories have registered handlers
public handlerUuids: string[] = [];
Expand Down Expand Up @@ -77,7 +77,7 @@ export class OpenRgbPlatform implements DynamicPlatformPlugin {
* This function is invoked when Homebridge restores cached accessories from disk at startup.
* It should be used to setup event handlers for characteristics and update respective values.
*/
configureAccessory(accessory: PlatformAccessory<rgbDeviceContext>) {
configureAccessory(accessory: PlatformAccessory<RgbDeviceContext>) {
this.log.info('Loading accessory from cache:', accessory.displayName);

// add the restored accessory to the accessories cache so we can track if it has already been registered
Expand All @@ -91,11 +91,11 @@ export class OpenRgbPlatform implements DynamicPlatformPlugin {
*/
async discoverDevices() {
// OpenRGB SDK servers listed in config
const servers: rgbServer[] = this.config.servers;
const servers: RgbServer[] = this.config.servers;
// servers that connected successfully; use index to match to a found device
const foundServers: rgbServer[] = [];
const foundServers: RgbServer[] = [];
// RGB devices reported by found servers
const foundDevices: rgbDevice[] = [];
const foundDevices: RgbDevice[] = [];
// UUID's of found devices
const foundUuids: string[] = [];

Expand All @@ -115,7 +115,7 @@ export class OpenRgbPlatform implements DynamicPlatformPlugin {
this.log.debug('Registering devices');
foundDevices.forEach((device, deviceIndex) => {
// server this device belongs to
const deviceServer: rgbServer = foundServers[deviceIndex];
const deviceServer: RgbServer = foundServers[deviceIndex];

// unique ID for the device
const uuid = this.genUuid(device);
Expand Down Expand Up @@ -152,7 +152,7 @@ export class OpenRgbPlatform implements DynamicPlatformPlugin {
this.log.info('Adding new accessory:', device.name);

// create a new accessory
const accessory = new this.api.platformAccessory<rgbDeviceContext>(device.name, uuid);
const accessory = new this.api.platformAccessory<RgbDeviceContext>(device.name, uuid);
this.accessories.push(accessory);

// the `context` property can be used to store any data about the accessory
Expand Down Expand Up @@ -182,10 +182,10 @@ export class OpenRgbPlatform implements DynamicPlatformPlugin {
// (unless preserveDisconnected option is set)
// or if the devices belong to a server that is no longer in the config
this.accessories = this.accessories.filter(accessory => {
const accServer: rgbServer = accessory.context.server;
const accServer: RgbServer = accessory.context.server;
const accUuid: string = accessory.UUID;

const serverMatch = (server: rgbServer) => (
const serverMatch = (server: RgbServer) => (
server.name === accServer.name &&
server.host === accServer.host &&
server.port === accServer.port
Expand All @@ -210,7 +210,7 @@ export class OpenRgbPlatform implements DynamicPlatformPlugin {
}

/** For generating a UUID for an RGB device from a globally unique but constant set of inputs */
genUuid(device: rgbDevice): string {
genUuid(device: RgbDevice): string {
return this.api.hap.uuid.generate(`${device.name}-${device.serial}-${device.location}`);
}

Expand All @@ -220,8 +220,8 @@ export class OpenRgbPlatform implements DynamicPlatformPlugin {
* client (the connection object) and devices (array of RGB device info)
*/
async rgbConnection(
server: rgbServer,
action: (client: any, devices: rgbDevice[]) => void | Promise<void>,
server: RgbServer,
action: (client: any, devices: RgbDevice[]) => void | Promise<void>,
): Promise<number> {
const { name: serverName, host: serverHost, port: serverPort } = server;
const client = new OpenRGB(serverName, serverPort, serverHost);
Expand All @@ -238,7 +238,7 @@ export class OpenRgbPlatform implements DynamicPlatformPlugin {
}

// Build array of device information
const devices: rgbDevice[] = [];
const devices: RgbDevice[] = [];
let controllerCount = 0;

try {
Expand All @@ -249,7 +249,7 @@ export class OpenRgbPlatform implements DynamicPlatformPlugin {

for (let deviceId = 0; deviceId < controllerCount; deviceId++) {
try {
const device: rgbDevice = await client.getControllerData(deviceId);
const device: RgbDevice = await client.getControllerData(deviceId);
devices.push(device);
} catch (err) {
this.log.warn(`Unable to get status of RGB device ${deviceId} on OpenRGB SDK server at ${serverHost}:${serverPort}`);
Expand Down
18 changes: 9 additions & 9 deletions src/platformAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';

import { OpenRgbPlatform } from './platform';

import { color, openRgbColor, rgbDeviceContext, rgbDeviceStates } from './rgb';
import { Color, OpenRgbColor, RgbDeviceContext, RgbDeviceStates } from './rgb';
import * as ColorConvert from 'color-convert';
import { getDeviceLedRgbColor, findDeviceModeId, isLedOff, createDeviceLedConfig, getStateHsvColor } from './utils';
import { CHARACTERISTIC_UPDATE_DELAY } from './settings';
Expand All @@ -15,7 +15,7 @@ import { CHARACTERISTIC_UPDATE_DELAY } from './settings';
export class OpenRgbPlatformAccessory {
private service: Service;

private states: rgbDeviceStates = {
private states: RgbDeviceStates = {
On: false,
Hue: 0,
Saturation: 0,
Expand All @@ -24,7 +24,7 @@ export class OpenRgbPlatformAccessory {

constructor(
private readonly platform: OpenRgbPlatform,
private readonly accessory: PlatformAccessory<rgbDeviceContext>,
private readonly accessory: PlatformAccessory<RgbDeviceContext>,
) {

// set accessory information
Expand Down Expand Up @@ -118,8 +118,8 @@ export class OpenRgbPlatformAccessory {
* color and make the assumption that the others match it.
* If the computer/SDK server is off, the light will appear to be off, not unresponsive.
*/
async getLedsHsv(): Promise<color> {
let colorHsv: color = [0, 0, 0];
async getLedsHsv(): Promise<Color> {
let colorHsv: Color = [0, 0, 0];

await this.platform.rgbConnection(this.accessory.context.server, (client, devices) => {
const device = devices.find(d => this.platform.genUuid(d) === this.accessory.UUID);
Expand Down Expand Up @@ -158,7 +158,7 @@ export class OpenRgbPlatformAccessory {
if (!device) {
return;
}
const ledColor: openRgbColor = device.colors[0];
const ledColor: OpenRgbColor = device.colors[0];
const ledIsBlack = (ledColor.red + ledColor.green + ledColor.blue) === 0;
const deviceModeIsOff = findDeviceModeId(device, 'Off') === device.activeMode;
if (ledIsBlack || deviceModeIsOff) {
Expand Down Expand Up @@ -210,8 +210,8 @@ export class OpenRgbPlatformAccessory {

// New state info
const isOn: boolean = this.states.On;
const newColorHsv: color = getStateHsvColor(this.states);
let newColorRgb: color = ColorConvert.hsv.rgb(newColorHsv);
const newColorHsv: Color = getStateHsvColor(this.states);
let newColorRgb: Color = ColorConvert.hsv.rgb(newColorHsv);
let newMode: number | undefined = undefined;

await this.platform.rgbConnection(this.accessory.context.server, async (client, devices) => {
Expand Down Expand Up @@ -261,7 +261,7 @@ export class OpenRgbPlatformAccessory {
}
}
// Set light colors
const newLedColors: openRgbColor[] = createDeviceLedConfig(newColorRgb, device);
const newLedColors: OpenRgbColor[] = createDeviceLedConfig(newColorRgb, device);
await client.updateLeds(device.deviceId, newLedColors);
if (!isLedOff(newColorRgb)) {
this.accessory.context.lastPoweredRgbColor = newColorRgb;
Expand Down
22 changes: 11 additions & 11 deletions src/rgb.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/** A color (e.g. an RGB or HSV color made up of its 3 channel values) */
export type color = [number, number, number];
export type Color = [number, number, number];

/** A color object as used by the OpenRGB SDK */
export interface openRgbColor {
export interface OpenRgbColor {
red: number;
green: number;
blue: number;
}

/** Describes a device as returned by the OpenRGB SDK */
export interface rgbDevice {
export interface RgbDevice {
deviceId: number;
type: number;
name: string;
Expand All @@ -21,34 +21,34 @@ export interface rgbDevice {
leds: [
{
name: string;
value: openRgbColor;
value: OpenRgbColor;
}
];
colors: openRgbColor[];
colors: OpenRgbColor[];
modes?: any[];
zones?: any[];
[key: string]: any;
}

/** Describes a device running the OpenRGB SDK server */
export interface rgbServer {
export interface RgbServer {
name: string;
host: string;
port: number;
}

/** State information that HomeKit keeps for accessories */
export interface rgbDeviceStates {
export interface RgbDeviceStates {
On: boolean;
Hue: number;
Saturation: number;
Brightness: number;
}

/** Context information stored for accessories */
export interface rgbDeviceContext {
device: rgbDevice;
server: rgbServer;
lastPoweredRgbColor?: color;
export interface RgbDeviceContext {
device: RgbDevice;
server: RgbServer;
lastPoweredRgbColor?: Color;
lastPoweredModeId?: number;
}
20 changes: 10 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import { color, openRgbColor, rgbDevice, rgbDeviceStates } from './rgb';
import { Color, OpenRgbColor, RgbDevice, RgbDeviceStates } from './rgb';

/** Gets the RGB color that is set on the provided device */
export function getDeviceLedRgbColor(device: rgbDevice): color {
const ledColor: openRgbColor = device.colors[0];
const ledRgb: color = [ledColor.red, ledColor.green, ledColor.blue];
export function getDeviceLedRgbColor(device: RgbDevice): Color {
const ledColor: OpenRgbColor = device.colors[0];
const ledRgb: Color = [ledColor.red, ledColor.green, ledColor.blue];
return ledRgb;
}

/** Gets the HSV color that is currently represented by an accessory's state */
export function getStateHsvColor(states: rgbDeviceStates): color {
export function getStateHsvColor(states: RgbDeviceStates): Color {
return [states.Hue, states.Saturation, states.Brightness];
}

/** Determines whether the provided color is black */
export function isLedOff(color: color): boolean {
export function isLedOff(color: Color): boolean {
return color[0] === 0 && color[1] === 0 && color[2] === 0;
}

/** Finds the ID of a device mode by name or returns undefined if the device has no matching mode */
export function findDeviceModeId(device: rgbDevice, modeName: string): number | undefined {
export function findDeviceModeId(device: RgbDevice, modeName: string): number | undefined {
return device.modes?.find(mode => mode.name?.trim().toLowerCase() === modeName.trim().toLowerCase())?.id;
}

/** Takes an RGB color and builds an array that can be used to set all of the given device's LED's */
export function createDeviceLedConfig(rgbColor: color, device: rgbDevice): openRgbColor[] {
const ledColor: openRgbColor = {
export function createDeviceLedConfig(rgbColor: Color, device: RgbDevice): OpenRgbColor[] {
const ledColor: OpenRgbColor = {
red: rgbColor[0],
green: rgbColor[1],
blue: rgbColor[2],
};
const ledColors: openRgbColor[] = Array(device.colors.length).fill(ledColor);
const ledColors: OpenRgbColor[] = Array(device.colors.length).fill(ledColor);
return ledColors;
}

0 comments on commit e6ddfba

Please sign in to comment.