diff --git a/services/nodecg-io-streamdeck/extension/index.ts b/services/nodecg-io-streamdeck/extension/index.ts index 7d70f624d..a7bf1fdfe 100644 --- a/services/nodecg-io-streamdeck/extension/index.ts +++ b/services/nodecg-io-streamdeck/extension/index.ts @@ -14,10 +14,15 @@ module.exports = (nodecg: NodeCG.ServerAPI) => { }; class StreamdeckServiceBundle extends ServiceBundle { - presets = Object.fromEntries(this.buildPresets()); + constructor(nodecg: NodeCG.ServerAPI, serviceType: string, serviceConfigName: string, schemaPath: string) { + super(nodecg, serviceType, serviceConfigName, schemaPath); + this.buildPresets() + .then((presets) => (this.presets = Object.fromEntries(presets))) + .catch((err) => nodecg.log.error("Failed to build presets for the streamdeck service:", err)); + } - private buildPresets(): Array<[string, StreamdeckServiceConfig]> { - const decks = streamdeck.listStreamDecks(); + private async buildPresets() { + const decks = await streamdeck.listStreamDecks(); return decks.map((deck) => { const presetName = `${deck.model}@${deck.path}`; const presetConfig = { device: deck.path }; @@ -25,13 +30,29 @@ class StreamdeckServiceBundle extends ServiceBundle> { + let device: string | undefined = config.device; + if (device === "default") { + const decks = await streamdeck.listStreamDecks(); + if (!decks[0]) { + return error("No connected streamdeck found"); + } + + device = decks[0]?.path; + } + + return success(device); + } + async validateConfig(config: StreamdeckServiceConfig): Promise> { try { - let device: string | undefined = config.device; - if (device === "default") { - device = undefined; + const device = await this.getDeviceOrDefault(config); + if (device.failed) { + return device; } - streamdeck.openStreamDeck(device).close(); // Throws an error if the streamdeck is not found + + const deck = await streamdeck.openStreamDeck(device.result); // Throws an error if the streamdeck is not found + deck.close(); return emptySuccess(); } catch (err) { return error(String(err)); @@ -40,13 +61,13 @@ class StreamdeckServiceBundle extends ServiceBundle> { try { - let device: string | undefined = config.device; - if (device === "default") { - device = undefined; + const device = await this.getDeviceOrDefault(config); + if (device.failed) { + return device; } logger.info(`Connecting to the streamdeck ${config.device}.`); - const deck = streamdeck.openStreamDeck(device); + const deck = await streamdeck.openStreamDeck(device.result); logger.info(`Successfully connected to the streamdeck ${config.device}.`); return success(deck);