Skip to content

Commit

Permalink
chore: use interface for client in device implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Oct 30, 2023
1 parent 3a11b94 commit f7ce68e
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventEmitter } from 'eventemitter3'
import { Socket } from 'net'
import { ClientCapabilities, DeviceDrawProps, DeviceRegisterProps } from './device-types/api'
import { ClientCapabilities, CompanionClient, DeviceDrawProps, DeviceRegisterProps } from './device-types/api'
import { DEFAULT_PORT } from './lib'
import * as semver from 'semver'

Expand Down Expand Up @@ -87,7 +87,7 @@ export type CompanionSatelliteClientEvents = {
deviceErrored: [{ deviceId: string; message: string }]
}

export class CompanionSatelliteClient extends EventEmitter<CompanionSatelliteClientEvents> {
export class CompanionSatelliteClient extends EventEmitter<CompanionSatelliteClientEvents> implements CompanionClient {
private readonly debug: boolean
private socket: Socket | undefined

Expand Down
14 changes: 9 additions & 5 deletions src/device-types/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { CompanionSatelliteClient } from '../client'

export type DeviceId = string

export interface DeviceDrawProps {
Expand All @@ -25,7 +23,7 @@ export interface WrappedDevice {

close(): Promise<void>

initDevice(client: CompanionSatelliteClient, status: string): Promise<void>
initDevice(client: CompanionClient, status: string): Promise<void>

updateCapabilities(capabilities: ClientCapabilities): void

Expand Down Expand Up @@ -53,6 +51,12 @@ export interface ClientCapabilities {
readonly useCustomBitmapResolution: boolean
}

// export interface CompanionClient {
export interface CompanionClient {
get host(): string

keyDown(deviceId: string, keyIndex: number): void
keyUp(deviceId: string, keyIndex: number): void

// }
rotateLeft(deviceId: string, keyIndex: number): void
rotateRight(deviceId: string, keyIndex: number): void
}
5 changes: 2 additions & 3 deletions src/device-types/infinitton.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { CompanionSatelliteClient } from '../client'
import { CardGenerator } from '../cards'
import { ClientCapabilities, DeviceDrawProps, DeviceRegisterProps, WrappedDevice } from './api'
import { ClientCapabilities, CompanionClient, DeviceDrawProps, DeviceRegisterProps, WrappedDevice } from './api'
import Infinitton = require('infinitton-idisplay')
import * as imageRs from '@julusian/image-rs'

Expand Down Expand Up @@ -37,7 +36,7 @@ export class InfinittonWrapper implements WrappedDevice {
async close(): Promise<void> {
this.#panel.close()
}
async initDevice(client: CompanionSatelliteClient, status: string): Promise<void> {
async initDevice(client: CompanionClient, status: string): Promise<void> {
console.log('Registering key events for ' + this.deviceId)
this.#panel.on('down', (key) => client.keyDown(this.deviceId, key))
this.#panel.on('up', (key) => client.keyUp(this.deviceId, key))
Expand Down
5 changes: 2 additions & 3 deletions src/device-types/loupedeck-live-s.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { LoupedeckDevice, LoupedeckDisplayId, LoupedeckBufferFormat, LoupedeckModelId } from '@loupedeck/node'
import * as imageRs from '@julusian/image-rs'
import { CompanionSatelliteClient } from '../client'
import { CardGenerator } from '../cards'
import { ImageWriteQueue } from '../writeQueue'
import { ClientCapabilities, DeviceDrawProps, DeviceRegisterProps, WrappedDevice } from './api'
import { ClientCapabilities, CompanionClient, DeviceDrawProps, DeviceRegisterProps, WrappedDevice } from './api'

export class LoupedeckLiveSWrapper implements WrappedDevice {
readonly #cardGenerator: CardGenerator
Expand Down Expand Up @@ -87,7 +86,7 @@ export class LoupedeckLiveSWrapper implements WrappedDevice {
this.#queue?.abort()
await this.#deck.close()
}
async initDevice(client: CompanionSatelliteClient, status: string): Promise<void> {
async initDevice(client: CompanionClient, status: string): Promise<void> {
const convertButtonId = (type: 'button' | 'rotary', id: number): number => {
if (type === 'button') {
// return 24 + id
Expand Down
5 changes: 2 additions & 3 deletions src/device-types/loupedeck-live.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { LoupedeckDevice, LoupedeckDisplayId, LoupedeckBufferFormat, LoupedeckModelId } from '@loupedeck/node'
import * as imageRs from '@julusian/image-rs'
import { CompanionSatelliteClient } from '../client'
import { CardGenerator } from '../cards'
import { ImageWriteQueue } from '../writeQueue'
import { ClientCapabilities, DeviceDrawProps, DeviceRegisterProps, WrappedDevice } from './api'
import { ClientCapabilities, CompanionClient, DeviceDrawProps, DeviceRegisterProps, WrappedDevice } from './api'

export class LoupedeckLiveWrapper implements WrappedDevice {
readonly #cardGenerator: CardGenerator
Expand Down Expand Up @@ -91,7 +90,7 @@ export class LoupedeckLiveWrapper implements WrappedDevice {
this.#queue?.abort()
await this.#deck.close()
}
async initDevice(client: CompanionSatelliteClient, status: string): Promise<void> {
async initDevice(client: CompanionClient, status: string): Promise<void> {
const convertButtonId = (type: 'button' | 'rotary', id: number, rotarySecondary: boolean): number => {
if (type === 'button' && id >= 0 && id < 8) {
return 24 + id
Expand Down
5 changes: 2 additions & 3 deletions src/device-types/razer-stream-controller-x.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { LoupedeckDevice, LoupedeckDisplayId, LoupedeckBufferFormat, LoupedeckModelId } from '@loupedeck/node'
import * as imageRs from '@julusian/image-rs'
import { CompanionSatelliteClient } from '../client'
import { CardGenerator } from '../cards'
import { ImageWriteQueue } from '../writeQueue'
import { ClientCapabilities, DeviceDrawProps, DeviceRegisterProps, WrappedDevice } from './api'
import { ClientCapabilities, CompanionClient, DeviceDrawProps, DeviceRegisterProps, WrappedDevice } from './api'

export class RazerStreamControllerXWrapper implements WrappedDevice {
readonly #cardGenerator: CardGenerator
Expand Down Expand Up @@ -87,7 +86,7 @@ export class RazerStreamControllerXWrapper implements WrappedDevice {
this.#queue?.abort()
await this.#deck.close()
}
async initDevice(client: CompanionSatelliteClient, status: string): Promise<void> {
async initDevice(client: CompanionClient, status: string): Promise<void> {
const convertButtonId = (type: 'button' | 'rotary', id: number): number => {
if (type === 'button') {
return id
Expand Down
5 changes: 2 additions & 3 deletions src/device-types/streamdeck.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { DeviceModelId, StreamDeck } from '@elgato-stream-deck/node'
import * as imageRs from '@julusian/image-rs'
import { CompanionSatelliteClient } from '../client'
import { CardGenerator } from '../cards'
import { ImageWriteQueue } from '../writeQueue'
import { ClientCapabilities, DeviceDrawProps, DeviceRegisterProps, WrappedDevice } from './api'
import { ClientCapabilities, CompanionClient, DeviceDrawProps, DeviceRegisterProps, WrappedDevice } from './api'

export class StreamDeckWrapper implements WrappedDevice {
readonly #cardGenerator: CardGenerator
Expand Down Expand Up @@ -120,7 +119,7 @@ export class StreamDeckWrapper implements WrappedDevice {
this.#queue?.abort()
await this.#deck.close()
}
async initDevice(client: CompanionSatelliteClient, status: string): Promise<void> {
async initDevice(client: CompanionClient, status: string): Promise<void> {
console.log('Registering key events for ' + this.deviceId)
this.#deck.on('down', (key) => client.keyDown(this.deviceId, key))
this.#deck.on('up', (key) => client.keyUp(this.deviceId, key))
Expand Down
5 changes: 2 additions & 3 deletions src/device-types/xencelabs-quick-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import {
XencelabsQuickKeysDisplayOrientation,
WheelEvent,
} from '@xencelabs-quick-keys/node'
import { CompanionSatelliteClient } from '../client'
import { WrappedDevice, DeviceRegisterProps, DeviceDrawProps, ClientCapabilities } from './api'
import { WrappedDevice, DeviceRegisterProps, DeviceDrawProps, ClientCapabilities, CompanionClient } from './api'

function keyToCompanion(k: number): number | null {
if (k >= 0 && k < 4) return k + 1
Expand Down Expand Up @@ -52,7 +51,7 @@ export class QuickKeysWrapper implements WrappedDevice {

await this.#surface.stopData()
}
async initDevice(client: CompanionSatelliteClient, status: string): Promise<void> {
async initDevice(client: CompanionClient, status: string): Promise<void> {
console.log('Registering key events for ' + this.deviceId)

const handleDown = (key: number) => {
Expand Down

0 comments on commit f7ce68e

Please sign in to comment.