Skip to content

Commit

Permalink
Handle onSystemDidWakeUp as well
Browse files Browse the repository at this point in the history
  • Loading branch information
neilenns committed Jan 9, 2025
1 parent 67f9870 commit c1f89d4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/events/streamDeck/applicationDidLaunch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
7 changes: 5 additions & 2 deletions src/events/streamDeck/applicationDidTerminate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
14 changes: 14 additions & 0 deletions src/events/streamDeck/systemDidWakeUp.ts
Original file line number Diff line number Diff line change
@@ -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();
}
};
17 changes: 17 additions & 0 deletions src/managers/trackAudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c1f89d4

Please sign in to comment.