import streamDeck, { action, KeyDownEvent, SingletonAction, WillAppearEvent } from "@elgato/streamdeck"; import { OctoPrintClient, OctoPrintSettings } from "../octoprint-client"; @action({ UUID: "com.octoprint.monitor.psu-on" }) export class PsuOnAction extends SingletonAction { override async onWillAppear(ev: WillAppearEvent): Promise { await this.updateTitle(ev.action, ev.payload.settings as unknown as OctoPrintSettings); } override async onKeyDown(ev: KeyDownEvent): Promise { const settings = ev.payload.settings as unknown as OctoPrintSettings; if (!settings?.host || !settings?.apiKey) { await ev.action.setTitle("Configure"); return; } const client = new OctoPrintClient(settings); try { await ev.action.setTitle("Turning\nOn…"); await client.turnPSUOn(); setTimeout(() => this.updateTitle(ev.action, settings), 1500); } catch (err) { streamDeck.logger.error("PsuOn: failed", err); await ev.action.setTitle("⚠️ Error"); setTimeout(() => this.updateTitle(ev.action, settings), 2000); } } private async updateTitle(action: any, settings: OctoPrintSettings): Promise { if (!settings?.host || !settings?.apiKey) { await action.setTitle("Configure"); return; } try { const client = new OctoPrintClient(settings); const psu = await client.getPSUState(); if (psu.isPSUOn) { await action.setTitle("PSU\nOn"); } else { await action.setTitle("Turn\nPSU On"); } } catch { await action.setTitle("Turn\nPSU On"); } } }