53 lines
1.6 KiB
TypeScript
53 lines
1.6 KiB
TypeScript
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<void> {
|
|
await this.updateTitle(ev.action, ev.payload.settings as unknown as OctoPrintSettings);
|
|
}
|
|
|
|
override async onKeyDown(ev: KeyDownEvent): Promise<void> {
|
|
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<void> {
|
|
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");
|
|
}
|
|
}
|
|
}
|
|
|