diff --git a/README.md b/README.md index 5f8d40c..b7a1f42 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,12 @@ **A Stream Deck plugin to control your DIY [BusyLight](https://code.igox.org/iGoX/busylight) (ESP32 + MicroPython + Microdot).** +You can download the Stream Deck plugin to control your BusyLight: + +[![marketplace button](resources/images/streamdeck-plugin-get-it-on-marketplace.svg)](https://marketplace.elgato.com/product/igox-busylight-7448a0be-6dd6-4711-ba0d-86c52b9075b9) + +Or directly from the [releases](https://code.igox.org/busylight/busylight-streamdeck-plugin/releases) page. + --- ## Screenshot diff --git a/busylight/org.igox.busylight.sdPlugin/manifest.json b/busylight/org.igox.busylight.sdPlugin/manifest.json index 673f234..416d5c2 100644 --- a/busylight/org.igox.busylight.sdPlugin/manifest.json +++ b/busylight/org.igox.busylight.sdPlugin/manifest.json @@ -1,9 +1,26 @@ { "Name": "iGoX BusyLight", - "Version": "0.3.1.0", + "Version": "0.3.3.0", "Author": "iGoX", "$schema": "https://schemas.elgato.com/streamdeck/plugins/manifest.json", "Actions": [ + { + "Name": "Live BusyLight status", + "UUID": "org.igox.busylight.status.live", + "Icon": "imgs/actions/icons/status/status", + "Tooltip": "Display the current BusyLight status", + "PropertyInspectorPath": "ui/live-status-config.html", + "SupportedInMultiActions": false, + "Controllers": [ + "Keypad" + ], + "States": [ + { + "Image": "imgs/actions/icons/status/status", + "TitleAlignment": "bottom" + } + ] + }, { "Name": "Set BusyLight status", "UUID": "org.igox.busylight.status.set", @@ -56,11 +73,11 @@ "Category": "iGoX BusyLight", "CategoryIcon": "imgs/plugin/category-icon", "CodePath": "bin/plugin.js", - "Description": "Control your DIY BusyLight (https://github.com/igox/busylight) from your Stream Deck", + "Description": "Control your DIY BusyLight (https://github.com/busylight/busylight) from your Stream Deck", "Icon": "imgs/plugin/icon", - "SDKVersion": 2, + "SDKVersion": 3, "Software": { - "MinimumVersion": "6.4" + "MinimumVersion": "7.5" }, "OS": [ { diff --git a/busylight/org.igox.busylight.sdPlugin/ui/live-status-config.html b/busylight/org.igox.busylight.sdPlugin/ui/live-status-config.html new file mode 100644 index 0000000..778f365 --- /dev/null +++ b/busylight/org.igox.busylight.sdPlugin/ui/live-status-config.html @@ -0,0 +1,25 @@ + + + + Live BusyLight status + + + + + + + + + + + \ No newline at end of file diff --git a/busylight/org.igox.busylight.streamDeckPlugin b/busylight/org.igox.busylight.streamDeckPlugin new file mode 100644 index 0000000..0111fa5 Binary files /dev/null and b/busylight/org.igox.busylight.streamDeckPlugin differ diff --git a/busylight/package-lock.json b/busylight/package-lock.json index 131ce62..e674ae5 100644 --- a/busylight/package-lock.json +++ b/busylight/package-lock.json @@ -5,7 +5,8 @@ "packages": { "": { "dependencies": { - "@elgato/streamdeck": "^1.0.0" + "@elgato/streamdeck": "^1.0.0", + "ws": "^8.22.0" }, "devDependencies": { "@elgato/cli": "^1.1.0", @@ -15,6 +16,7 @@ "@rollup/plugin-typescript": "^12.1.0", "@tsconfig/node20": "^20.1.2", "@types/node": "~20.15.0", + "@types/ws": "^8.18.2", "rollup": "^4.0.2", "tslib": "^2.6.2", "typescript": "^5.2.2" @@ -704,6 +706,16 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/ws": { + "version": "8.18.2", + "resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.18.2.tgz", + "integrity": "sha512-67MQl+fpWKVTT1NYdnmo3U4sc/xPo/zQBncVnI74qmQa0z/b+1g6iYqNmGCPbxO+zz2aklb08a0oHfegiVd0/w==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@zip.js/zip.js": { "version": "2.7.54", "resolved": "https://registry.npmjs.org/@zip.js/zip.js/-/zip.js-2.7.54.tgz", @@ -2348,9 +2360,9 @@ } }, "node_modules/ws": { - "version": "8.18.0", - "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz", - "integrity": "sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==", + "version": "8.22.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.22.0.tgz", + "integrity": "sha512-Ydggc987+RO0AnWtZ/7Wq9FtNvcrL1b/RO0ud9mWjUPgDrsAAwQSF51sm2hm1XofbU/4jkpGEsLFsZZxU+1DOg==", "license": "MIT", "engines": { "node": ">=10.0.0" diff --git a/busylight/package.json b/busylight/package.json index 4cfa101..dd4f1b4 100644 --- a/busylight/package.json +++ b/busylight/package.json @@ -12,11 +12,13 @@ "@rollup/plugin-typescript": "^12.1.0", "@tsconfig/node20": "^20.1.2", "@types/node": "~20.15.0", + "@types/ws": "^8.18.2", "rollup": "^4.0.2", "tslib": "^2.6.2", "typescript": "^5.2.2" }, "dependencies": { - "@elgato/streamdeck": "^1.0.0" + "@elgato/streamdeck": "^1.0.0", + "ws": "^8.22.0" } -} \ No newline at end of file +} diff --git a/busylight/src/actions/live-status.ts b/busylight/src/actions/live-status.ts new file mode 100644 index 0000000..e5053d1 --- /dev/null +++ b/busylight/src/actions/live-status.ts @@ -0,0 +1,127 @@ +import streamDeck, { action, SingletonAction, type KeyAction, type WillAppearEvent, type WillDisappearEvent } from "@elgato/streamdeck"; +import WebSocket from "ws"; + +const statuses = new Set(["available", "away", "busy", "on", "off", "colored"]); + +@action({ UUID: "org.igox.busylight.status.live" }) +export class LiveStatus extends SingletonAction { + private readonly keys = new Map(); + private socket?: WebSocket; + private reconnectTimer?: ReturnType; + private url?: string; + private status?: string; + private title = "Connecting"; + + constructor() { + super(); + streamDeck.settings.onDidReceiveGlobalSettings<{ url?: string }>(ev => { + this.configure(ev.settings.url); + }); + } + + override async onWillAppear(ev: WillAppearEvent): Promise { + if (!ev.action.isKey()) return; + this.keys.set(ev.action.id, ev.action); + this.render(ev.action); + const settings = await streamDeck.settings.getGlobalSettings<{ url?: string }>(); + this.configure(settings.url); + } + + override onWillDisappear(ev: WillDisappearEvent): void { + this.keys.delete(ev.action.id); + if (this.keys.size === 0) { + this.disconnect(); + this.status = undefined; + this.title = "Connecting"; + } + } + + private configure(serverUrl?: string): void { + let url: URL; + try { + url = new URL(serverUrl?.trim() || "http://busylight-esp32.local"); + if (!["http:", "https:", "ws:", "wss:"].includes(url.protocol)) { + throw new Error("Unsupported BusyLight URL protocol"); + } + url.protocol = ["https:", "wss:"].includes(url.protocol) ? "wss:" : "ws:"; + url.pathname = "/ws/status"; + url.search = ""; + url.hash = ""; + } catch { + this.disconnect(); + this.url = undefined; + this.update(undefined, "Invalid URL"); + return; + } + + const nextUrl = url.toString(); + if (this.url !== nextUrl) { + this.disconnect(); + this.url = nextUrl; + this.update(undefined, "Connecting"); + } + if (this.keys.size > 0 && !this.socket && !this.reconnectTimer) { + this.connect(); + } + } + + private connect(): void { + if (!this.url || this.keys.size === 0) return; + const socket = new WebSocket(this.url, { handshakeTimeout: 5000 }); + this.socket = socket; + + socket.on("message", data => { + if (this.socket !== socket) return; + try { + const message: unknown = JSON.parse(data.toString()); + if (typeof message !== "object" || message === null || !("status" in message) + || typeof message.status !== "string" || !statuses.has(message.status)) { + streamDeck.logger.warn("Ignoring invalid BusyLight status message"); + return; + } + this.update(message.status, message.status.charAt(0).toUpperCase() + message.status.slice(1)); + } catch { + streamDeck.logger.warn("Ignoring malformed BusyLight status message"); + } + }); + socket.on("error", error => { + if (this.socket !== socket) return; + streamDeck.logger.warn(`BusyLight WebSocket error: ${error.message}`); + socket.terminate(); + }); + socket.on("close", () => { + if (this.socket !== socket) return; + this.socket = undefined; + this.update(undefined, "Offline"); + if (this.keys.size > 0) { + this.reconnectTimer = setTimeout(() => { + this.reconnectTimer = undefined; + this.connect(); + }, 2000); + } + }); + } + + private disconnect(): void { + clearTimeout(this.reconnectTimer); + this.reconnectTimer = undefined; + const socket = this.socket; + this.socket = undefined; + socket?.terminate(); + } + + private update(status: string | undefined, title: string): void { + this.status = status; + this.title = title; + for (const key of this.keys.values()) this.render(key); + } + + private render(key: KeyAction): void { + const image = this.status + ? `imgs/actions/buttons/${this.status}/${this.status}.png` + : "imgs/actions/icons/status/status.png"; + void Promise.all([key.setImage(image), key.setTitle(this.title)]).catch(error => { + streamDeck.logger.error("Failed to update BusyLight status button", error); + }); + } +} \ No newline at end of file diff --git a/busylight/src/plugin.ts b/busylight/src/plugin.ts index 98e70d5..1ae94d8 100644 --- a/busylight/src/plugin.ts +++ b/busylight/src/plugin.ts @@ -3,6 +3,7 @@ import streamDeck, { LogLevel, SingletonAction, action, type DidReceiveSettingsE import { SetStatus} from "./actions/set-status"; import { SetBrightness } from "./actions/set-brightness"; import { SetColor } from "./actions/set-color"; +import { LiveStatus } from "./actions/live-status"; // We can enable "trace" logging so that all messages between the Stream Deck, and the plugin are recorded. When storing sensitive information streamDeck.logger.setLevel(LogLevel.INFO); @@ -11,6 +12,7 @@ streamDeck.logger.setLevel(LogLevel.INFO); streamDeck.actions.registerAction(new SetStatus()); streamDeck.actions.registerAction(new SetBrightness()); streamDeck.actions.registerAction(new SetColor()); +streamDeck.actions.registerAction(new LiveStatus()); // Finally, connect to the Stream Deck. streamDeck.connect();