|
| 1 | +import { triggerNewAsset } from "./polling"; |
| 2 | + |
| 3 | +class ImmichFrame { |
| 4 | + private static instance: ImmichFrame | null = null; |
| 5 | + |
| 6 | + private readonly SCREENSAVER_DELAY_MS = 4 * 1000; |
| 7 | + |
| 8 | + private readonly PORT = 53287 as const; |
| 9 | + private readonly BASE_URL = `http://localhost:${this.PORT}`; |
| 10 | + |
| 11 | + private readonly endpoints = { |
| 12 | + DIM: "dim", |
| 13 | + UNDIM: "undim", |
| 14 | + NEXT: "next", |
| 15 | + PREVIOUS: "previous", |
| 16 | + PAUSE: "pause", |
| 17 | + SETTINGS: "settings", |
| 18 | + }; |
| 19 | + |
| 20 | + inSleepMode: boolean = false; |
| 21 | + |
| 22 | + public static getInstance(): ImmichFrame { |
| 23 | + if (!ImmichFrame.instance) { |
| 24 | + ImmichFrame.instance = new ImmichFrame(); |
| 25 | + } |
| 26 | + return ImmichFrame.instance; |
| 27 | + } |
| 28 | + |
| 29 | + public dimScreen(): void { |
| 30 | + fetch(`${this.BASE_URL}/${this.endpoints.DIM}`).catch((error) => |
| 31 | + console.error("Error dimming ImmichFrame screen:", error), |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + public undimScreen(): void { |
| 36 | + fetch(`${this.BASE_URL}/${this.endpoints.UNDIM}`).catch((error) => |
| 37 | + console.error("Error undimming ImmichFrame screen:", error), |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + public setScreensaverState(enable: boolean): void { |
| 42 | + try { |
| 43 | + if (enable) { |
| 44 | + if (this.inSleepMode) return; |
| 45 | + |
| 46 | + this.inSleepMode = true; |
| 47 | + |
| 48 | + setTimeout(() => this.dimScreen(), this.SCREENSAVER_DELAY_MS); |
| 49 | + } else { |
| 50 | + if (!this.inSleepMode) return; |
| 51 | + |
| 52 | + this.undimScreen(); |
| 53 | + |
| 54 | + this.inSleepMode = false; |
| 55 | + |
| 56 | + triggerNewAsset(); |
| 57 | + } |
| 58 | + } catch (error) { |
| 59 | + console.error("Error in ImmichFrame screen operations:", error); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +const immichFrame = ImmichFrame.getInstance(); |
| 65 | + |
| 66 | +export default immichFrame; |
0 commit comments