Add PSU
This commit is contained in:
52
src/actions/psu-on.ts
Normal file
52
src/actions/psu-on.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user