From c1f89d4a97089b56b0bffeffa43b00fa197f12a3 Mon Sep 17 00:00:00 2001
From: Neil Enns <neile@live.com>
Date: Thu, 9 Jan 2025 14:57:08 -0800
Subject: [PATCH] Handle onSystemDidWakeUp as well

---
 src/events/streamDeck/applicationDidLaunch.ts   |  5 +++--
 .../streamDeck/applicationDidTerminate.ts       |  7 +++++--
 src/events/streamDeck/systemDidWakeUp.ts        | 14 ++++++++++++++
 src/managers/trackAudio.ts                      | 17 +++++++++++++++++
 src/plugin.ts                                   |  2 ++
 5 files changed, 41 insertions(+), 4 deletions(-)
 create mode 100644 src/events/streamDeck/systemDidWakeUp.ts

diff --git a/src/events/streamDeck/applicationDidLaunch.ts b/src/events/streamDeck/applicationDidLaunch.ts
index 1d27739..248927f 100644
--- a/src/events/streamDeck/applicationDidLaunch.ts
+++ b/src/events/streamDeck/applicationDidLaunch.ts
@@ -2,9 +2,10 @@ import { ApplicationDidLaunchEvent } from "@elgato/streamdeck";
 import trackAudioManager from "@managers/trackAudio";
 import mainLogger from "@utils/logger";
 
-const logger = mainLogger.child({ service: "applicationDidLaunch" });
-
 export const handleOnApplicationDidLaunch = (ev: ApplicationDidLaunchEvent) => {
+  const logger = mainLogger.child({ service: "applicationDidLaunch" });
+
   logger.info("Received applicationDidLaunch event", ev.application);
+  trackAudioManager.isAppRunning = true;
   trackAudioManager.connect();
 };
diff --git a/src/events/streamDeck/applicationDidTerminate.ts b/src/events/streamDeck/applicationDidTerminate.ts
index 54c52cf..3f23cdd 100644
--- a/src/events/streamDeck/applicationDidTerminate.ts
+++ b/src/events/streamDeck/applicationDidTerminate.ts
@@ -3,12 +3,15 @@ import mainLogger from "@utils/logger";
 import trackAudioManager from "@managers/trackAudio";
 import vatsimManager from "@managers/vatsim";
 
-const logger = mainLogger.child({ service: "handleOnApplicationDidTerminate" });
-
 export const handleOnApplicationDidTerminate = (
   ev: ApplicationDidTerminateEvent
 ) => {
+  const logger = mainLogger.child({
+    service: "handleOnApplicationDidTerminate",
+  });
+
   logger.info("Received applicationDidTerminate event", ev.application);
+  trackAudioManager.isAppRunning = false;
   trackAudioManager.disconnect();
   vatsimManager.stop();
 };
diff --git a/src/events/streamDeck/systemDidWakeUp.ts b/src/events/streamDeck/systemDidWakeUp.ts
new file mode 100644
index 0000000..0f841a5
--- /dev/null
+++ b/src/events/streamDeck/systemDidWakeUp.ts
@@ -0,0 +1,14 @@
+import trackAudioManager from "@managers/trackAudio";
+import mainLogger from "@utils/logger";
+
+export const handleOnSystemDidWakeUp = () => {
+  const logger = mainLogger.child({ service: "systemDidWakeUp" });
+
+  logger.info("Received systemDidWakeUp event");
+
+  // This ensures reconnection to the TrackAudio manager if somehow the websocket
+  // connection doesn't automatically start back up after a system wake.
+  if (trackAudioManager.isAppRunning) {
+    trackAudioManager.connect();
+  }
+};
diff --git a/src/managers/trackAudio.ts b/src/managers/trackAudio.ts
index 537397f..e163dac 100644
--- a/src/managers/trackAudio.ts
+++ b/src/managers/trackAudio.ts
@@ -29,6 +29,7 @@ class TrackAudioManager extends EventEmitter {
   private reconnectTimer: NodeJS.Timeout | null = null;
 
   private _isVoiceConnected = false;
+  private _isAppRunning = false;
 
   //#region Constructor and instance management
   private constructor() {
@@ -47,6 +48,22 @@ class TrackAudioManager extends EventEmitter {
   }
   //#endregion
 
+  /**
+   * Gets whether the TrackAudio application was detected as running by Stream Deck.
+   * @returns {boolean} True if running.
+   */
+  public get isAppRunning(): boolean {
+    return this._isAppRunning;
+  }
+
+  /**
+   * Sets whether the TrackAudio application is running.
+   * @params newValue True if running.
+   */
+  public set isAppRunning(newValue: boolean) {
+    this._isAppRunning = newValue;
+  }
+
   /**
    * Sets the connection URL for TrackAudio.
    * @param url The URL for the TrackAudio instance
diff --git a/src/plugin.ts b/src/plugin.ts
index be25e83..1a25534 100644
--- a/src/plugin.ts
+++ b/src/plugin.ts
@@ -29,6 +29,7 @@ import { handleMainVolumeAdded } from "@events/action/mainVolumeAdded";
 import { handleMainVolumeChange } from "@events/trackAudio/mainVolumeChange";
 import { handleOnApplicationDidLaunch } from "@events/streamDeck/applicationDidLaunch";
 import { handleOnApplicationDidTerminate } from "@events/streamDeck/applicationDidTerminate";
+import { handleOnSystemDidWakeUp } from "@events/streamDeck/systemDidWakeUp";
 import { handleRemoved } from "@events/action/removed";
 import { handleRxBegin } from "@events/trackAudio/rxBegin";
 import { handleRxEnd } from "@events/trackAudio/rxEnd";
@@ -67,6 +68,7 @@ streamDeck.actions.registerAction(new MainVolume());
 // Register the event handlers
 streamDeck.system.onApplicationDidLaunch(handleOnApplicationDidLaunch);
 streamDeck.system.onApplicationDidTerminate(handleOnApplicationDidTerminate);
+streamDeck.system.onSystemDidWakeUp(handleOnSystemDidWakeUp);
 
 trackAudioManager.on("connected", () => {
   disconnectHandled = false;