Skip to content

Commit

Permalink
Use app monitoring instead of polling to establish websocket connection
Browse files Browse the repository at this point in the history
Fixes #366
  • Loading branch information
neilenns committed Jan 9, 2025
1 parent 98a75b2 commit 67f9870
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 41 deletions.
4 changes: 4 additions & 0 deletions com.neil-enns.trackaudio.sdPlugin/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@
]
}
],
"ApplicationsToMonitor": {
"mac": ["com.vatsim.trackaudio"],
"windows": ["trackaudio.exe"]
},
"Category": "TrackAudio",
"CategoryIcon": "images/plugin/categoryIcon",
"CodePath": "bin/plugin.js",
Expand Down
13 changes: 0 additions & 13 deletions src/events/action/actionAdded.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/events/action/removed.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import actionManager from "@managers/action";
import vatsimManager from "@managers/vatsim";
import svgManager from "@managers/svg";
import trackAudioManager from "@managers/trackAudio";

export const handleRemoved = (count: number) => {
if (count === 0) {
trackAudioManager.disconnect();
svgManager.reset();
}

// If there are no more ATIS letter actions then stop polling VATSIM.
if (actionManager.getAtisLetterControllers().length === 0) {
vatsimManager.stop();
}

// If there are no more actions at all stop trying to connect to TrackAudio
if (actionManager.getActions().length === 0) {
trackAudioManager.disconnect();
}
};
10 changes: 10 additions & 0 deletions src/events/streamDeck/applicationDidLaunch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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) => {
logger.info("Received applicationDidLaunch event", ev.application);
trackAudioManager.connect();
};
14 changes: 14 additions & 0 deletions src/events/streamDeck/applicationDidTerminate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ApplicationDidTerminateEvent } from "@elgato/streamdeck";
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
) => {
logger.info("Received applicationDidTerminate event", ev.application);
trackAudioManager.disconnect();
vatsimManager.stop();
};
1 change: 0 additions & 1 deletion src/events/streamDeck/atisLetter/addAtisLetter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ export const handleAddAtisLetter = (

actionManager.add(controller);
actionManager.emit("atisLetterAdded", controller);
actionManager.emit("actionAdded", controller);
};
1 change: 0 additions & 1 deletion src/events/streamDeck/hotline/addHotline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ export const handleAddHotline = (
// Force buttons to refresh so the newly added button shows the correct state.
actionManager.add(controller);
actionManager.emit("hotlineAdded", controller);
actionManager.emit("actionAdded", controller);
};
1 change: 0 additions & 1 deletion src/events/streamDeck/mainVolume/addMainVolume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ export const handleAddMainVolume = (

actionManager.add(controller);
actionManager.emit("mainVolumeAdded", controller);
actionManager.emit("actionAdded", controller);
};
1 change: 0 additions & 1 deletion src/events/streamDeck/pushToTalk/addPushToTalk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ import actionManager from "@managers/action";
actionManager.add(controller);

actionManager.emit("pushToTalkAdded", controller);
actionManager.emit("actionAdded", controller);
};
1 change: 0 additions & 1 deletion src/events/streamDeck/stationStatus/addStation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ export const handleAddStation = (

actionManager.add(controller);
actionManager.emit("stationStatusAdded", controller);
actionManager.emit("actionAdded", controller);
};
1 change: 0 additions & 1 deletion src/events/streamDeck/stationVolume/addStationVolume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ export const handleAddStationVolume = (

actionManager.add(controller);
actionManager.emit("stationVolumeAdded", controller);
actionManager.emit("actionAdded", controller);
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ export const handleAddTrackAudioStatus = (

actionManager.add(controller);
actionManager.emit("trackAudioStatusAdded", controller);
actionManager.emit("actionAdded", controller);
};
1 change: 0 additions & 1 deletion src/managers/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ class ActionManager extends EventEmitter {
*/
public add(controller: Controller): void {
this.actions.push(controller);
this.emit("actionAdded", controller);
}

/**
Expand Down
30 changes: 17 additions & 13 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,32 @@ import { StationVolume } from "@actions/stationVolume";
import { TrackAudioStatus } from "@actions/trackAudioStatus";

// Event handlers
import { MainVolume } from "@actions/mainVolume";
import { handleActionAdded } from "@events/action/actionAdded";
import { handleAtisLetterAdded } from "@events/action/atisLetterAdded";
import { handleAtisLetterUpdated } from "@events/action/atisLetterUpdated";
import { handleHotlineSettingsUpdated } from "@events/action/hotlineSettingsUpdated";
import { handleMainVolumeAdded } from "@events/action/mainVolumeAdded";
import { handleRemoved } from "@events/action/removed";
import { handleStationStatusAdded } from "@events/action/stationStatusAdded";
import { handleStationStatusSettingsUpdated } from "@events/action/stationStatusSettingsUpdated";
import { handleStationVolumeAdded } from "@events/action/stationVolumeAdded";
import { handleTrackAudioStatusAdded } from "@events/action/trackAudioStatusAdded";
import { handleImageChanged } from "@events/svg/imageChanged";
import { handleConnected } from "@events/trackAudio/connected";
import { handleDisconnected } from "@events/trackAudio/disconnected";
import { handleFrequencyRemoved } from "@events/trackAudio/frequencyRemoved";
import { handleHotlineSettingsUpdated } from "@events/action/hotlineSettingsUpdated";
import { handleImageChanged } from "@events/svg/imageChanged";
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 { handleRemoved } from "@events/action/removed";
import { handleRxBegin } from "@events/trackAudio/rxBegin";
import { handleRxEnd } from "@events/trackAudio/rxEnd";
import { handleStationAdded } from "@events/trackAudio/stationAdded";
import { handleStationStates } from "@events/trackAudio/stationStates";
import { handleStationStateUpdate } from "@events/trackAudio/stationStateUpdate";
import { handleStationStatusAdded } from "@events/action/stationStatusAdded";
import { handleStationStatusSettingsUpdated } from "@events/action/stationStatusSettingsUpdated";
import { handleStationVolumeAdded } from "@events/action/stationVolumeAdded";
import { handleTrackAudioStatusAdded } from "@events/action/trackAudioStatusAdded";
import { handleTxBegin } from "@events/trackAudio/txBegin";
import { handleTxEnd } from "@events/trackAudio/txEnd";
import { handleVoiceConnectedState } from "@events/trackAudio/voiceConnectedState";
import { handleVatsimDataReceived } from "@events/vatsim/vatsimDataReceived";
import { handleVoiceConnectedState } from "@events/trackAudio/voiceConnectedState";
import { MainVolume } from "@actions/mainVolume";

const logger = mainLogger.child({ service: "plugin" });

Expand All @@ -54,7 +55,7 @@ process.on("uncaughtException", (error) => {
logger.error("Uncaught Exception:", error);
});

// Register all the event handlers
// Register all the actions
streamDeck.actions.registerAction(new AtisLetter());
streamDeck.actions.registerAction(new Hotline());
streamDeck.actions.registerAction(new PushToTalk());
Expand All @@ -63,6 +64,10 @@ streamDeck.actions.registerAction(new TrackAudioStatus());
streamDeck.actions.registerAction(new StationVolume());
streamDeck.actions.registerAction(new MainVolume());

// Register the event handlers
streamDeck.system.onApplicationDidLaunch(handleOnApplicationDidLaunch);
streamDeck.system.onApplicationDidTerminate(handleOnApplicationDidTerminate);

trackAudioManager.on("connected", () => {
disconnectHandled = false;
handleConnected();
Expand Down Expand Up @@ -99,7 +104,6 @@ actionManager.on("trackAudioStatusAdded", handleTrackAudioStatusAdded);
actionManager.on("trackAudioStatusUpdated", handleTrackAudioStatusAdded);
actionManager.on("atisLetterAdded", handleAtisLetterAdded);
actionManager.on("atisLetterUpdated", handleAtisLetterUpdated);
actionManager.on("actionAdded", handleActionAdded);
actionManager.on("stationVolumeAdded", handleStationVolumeAdded);
actionManager.on("mainVolumeAdded", handleMainVolumeAdded);

Expand Down

0 comments on commit 67f9870

Please sign in to comment.