|
| 1 | +import * as path from 'path'; |
| 2 | +import * as fs from 'node:fs'; |
| 3 | +import sharp from 'sharp'; |
| 4 | +import type {StreamDeck} from '@elgato-stream-deck/node'; |
| 5 | +import {fileURLToPath} from 'url'; |
| 6 | +import type {HID, HIDActions} from './HID'; |
| 7 | + |
| 8 | +const __filename = fileURLToPath(import.meta.url); |
| 9 | +const __dirname = path.dirname(__filename); |
| 10 | + |
| 11 | +export default abstract class BaseStreamDeckHID implements HID { |
| 12 | + protected streamDeck: StreamDeck; |
| 13 | + protected layouts: number = 0; |
| 14 | + protected activeLayout: number = 0; |
| 15 | + |
| 16 | + protected hasVerificationPending = false; |
| 17 | + |
| 18 | + protected acceptCallback!: () => void; |
| 19 | + protected rejectCallback!: () => void; |
| 20 | + |
| 21 | + protected actions: HIDActions; |
| 22 | + |
| 23 | + protected isConnected!: boolean; |
| 24 | + |
| 25 | + constructor(streamDeck: StreamDeck, layouts: number) { |
| 26 | + this.streamDeck = streamDeck; |
| 27 | + this.layouts = layouts; |
| 28 | + |
| 29 | + this.streamDeck.clearPanel(); |
| 30 | + this.streamDeck.setBrightness(100); |
| 31 | + |
| 32 | + this.streamDeck.on('error', error => { |
| 33 | + console.error(error); |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + async getButtonImageBuffer(width, height, image: string): Promise<Buffer> { |
| 38 | + return sharp(path.resolve(__dirname, '../assets/' + image)) |
| 39 | + .flatten() |
| 40 | + .resize(width, height) |
| 41 | + .raw() |
| 42 | + .toBuffer(); |
| 43 | + } |
| 44 | + |
| 45 | + async getLayoutButtonImageBuffer(width, height, label: string, backgroundColor: string, textColor: string): Promise<Buffer> { |
| 46 | + // load svg file as string |
| 47 | + let svg = fs.readFileSync(path.resolve(__dirname, '../assets/Layout.svg'), 'utf8'); |
| 48 | + // replace "L1" with label |
| 49 | + svg = svg.replace('L1', label); |
| 50 | + // replace style="fill: #FFF" with background color |
| 51 | + svg = svg.replace('style="fill: #FFF"', 'style="fill: ' + backgroundColor + '"'); |
| 52 | + // replace fill="#000" with text color |
| 53 | + svg = svg.replace('fill="#000"', 'fill="' + textColor + '"'); |
| 54 | + |
| 55 | + return sharp(Buffer.from(svg)) |
| 56 | + .flatten() |
| 57 | + .resize(width, height) |
| 58 | + .raw() |
| 59 | + .toBuffer(); |
| 60 | + } |
| 61 | + |
| 62 | + requireVerification(accept: () => void, reject: () => void): void { |
| 63 | + this.hasVerificationPending = true; |
| 64 | + |
| 65 | + this.acceptCallback = accept; |
| 66 | + this.rejectCallback = reject; |
| 67 | + |
| 68 | + this.showVerificationButtons(); |
| 69 | + } |
| 70 | + |
| 71 | + verificationAccepted(): void { |
| 72 | + this.hasVerificationPending = false; |
| 73 | + this.hideVerificationButtons(); |
| 74 | + } |
| 75 | + |
| 76 | + verificationRejected(): void { |
| 77 | + this.hasVerificationPending = false; |
| 78 | + this.hideVerificationButtons(); |
| 79 | + } |
| 80 | + |
| 81 | + showVerificationButtons(): void { |
| 82 | + this.streamDeck.clearPanel(); |
| 83 | + } |
| 84 | + |
| 85 | + showBBBScreen(): void { |
| 86 | + this.streamDeck.clearPanel(); |
| 87 | + } |
| 88 | + |
| 89 | + hideVerificationButtons(): void { |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + async close(): Promise<void> { |
| 94 | + this.streamDeck.removeAllListeners(); |
| 95 | + await this.streamDeck.close(); |
| 96 | + } |
| 97 | + |
| 98 | + connected(actions: HIDActions): void { |
| 99 | + this.isConnected = true; |
| 100 | + this.muteCallback = actions.mute; |
| 101 | + this.unmuteCallback = actions.unmute; |
| 102 | + this.leaveCallback = actions.leave; |
| 103 | + |
| 104 | + this.layout1Callback = actions.layout1; |
| 105 | + this.layout2Callback = actions.layout2; |
| 106 | + this.layout3Callback = actions.layout3; |
| 107 | + } |
| 108 | + |
| 109 | + disconnected(): void { |
| 110 | + this.isConnected = false; |
| 111 | + this.showBBBScreen(); |
| 112 | + } |
| 113 | +} |
0 commit comments