Skip to content

Commit

Permalink
Add long press to hotline, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
neilenns committed Aug 21, 2024
1 parent acc5b13 commit 47a1a12
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 48 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ After installation the plugin actions are available under the TrackAudio categor
The station status action displays the current status of a single station's button in TrackAudio, including
whether communication is currently active. Pressing the action will toggle the equivalent button in TrackAudio,
convenient for listening to other frequencies while controlling with the ability to quickly turn off listening
to those frequencies when things get busy.
to those frequencies when things get busy. A long press of the action will refresh the action's state.

For example, if you are controlling `LMT_TWR` and have TrackAudio set up like this:

Expand Down Expand Up @@ -79,6 +79,7 @@ be added with `XCA` enabled and the hotline station should be added with `RX` en
hotline action with the primary and hotline station callsigns.

Once configured, pressing the action will toggle `TX` active between your primary and hotline frequencies.
A long press of the action will refresh the action's state.

### Hotline action settings <!-- omit from toc -->

Expand All @@ -99,7 +100,8 @@ Once configured, pressing the action will toggle `TX` active between your primar
## Configuring a TrackAudio status action

The TrackAudio status action shows the status of the connection between StreamDeck and TrackAudio, and whether
the voice connection in TrackAudio is up. Pressing the action will force a state refresh.
the voice connection in TrackAudio is up. A long press of the action will force a refresh of all the StreamDeck
TrackAudio actions.

### TrackAudio status action settings <!-- omit from toc -->

Expand All @@ -118,7 +120,7 @@ the voice connection in TrackAudio is up. Pressing the action will force a state
The ATIS letter action shows the current AITS letter for a station, refreshed automatically every minute.
When the ATIS letter updates the action will show an orange background until the action is pressed to reset the
state. Pressing the action when it is not in the updated state will force a refresh of the ATIS information.
state. A long press of the action will force a refresh of the ATIS information.

See the [SVG template documentation](https://github.com/neilenns/streamdeck-trackaudio/wiki/SVG-templates) for an example
template that shows the title small and station letter big.
Expand Down
4 changes: 2 additions & 2 deletions src/actions/atisLetter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ export class AtisLetter extends SingletonAction<AtisLetterSettings> {
const pressLength = Date.now() - this._keyDownStart;

if (pressLength > LONG_PRESS_THRESHOLD) {
actionManager.atisLetterLongPress(ev.action.id);
actionManager.atisLetterLongPress(ev.action);
} else {
actionManager.atisLetterShortPress(ev.action.id);
actionManager.atisLetterShortPress(ev.action);
}
}
}
Expand Down
20 changes: 16 additions & 4 deletions src/actions/hotline.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import {
action,
DidReceiveSettingsEvent,
KeyDownEvent,
KeyUpEvent,
SingletonAction,
WillAppearEvent,
WillDisappearEvent,
} from "@elgato/streamdeck";
import actionManager from "@managers/action";
import { LONG_PRESS_THRESHOLD } from "@utils/constants";

@action({ UUID: "com.neil-enns.trackaudio.hotline" })
/**
* Represents the status of a TrackAudio station
*/
export class Hotline extends SingletonAction<HotlineSettings> {
private _keyDownStart = 0;

// When the action is added to a profile it gets saved in the ActionManager
// instance for use elsewhere in the code. The default title is also set
// to something useful.
Expand All @@ -35,9 +38,18 @@ export class Hotline extends SingletonAction<HotlineSettings> {
actionManager.updateHotline(ev.action, ev.payload.settings);
}

// When the key is pressed send the request to toggle the hotline.
onKeyDown(ev: KeyDownEvent<HotlineSettings>): void | Promise<void> {
actionManager.toggleHotline(ev.action.id);
onKeyDown(): void | Promise<void> {
this._keyDownStart = Date.now();
}

onKeyUp(ev: KeyUpEvent<HotlineSettings>): Promise<void> | void {
const pressLength = Date.now() - this._keyDownStart;

if (pressLength > LONG_PRESS_THRESHOLD) {
actionManager.hotlineLongPress(ev.action);
} else {
actionManager.hotlineShortPress(ev.action);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/actions/stationStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ export class StationStatus extends SingletonAction<StationSettings> {
const pressLength = Date.now() - this._keyDownStart;

if (pressLength > LONG_PRESS_THRESHOLD) {
actionManager.stationStatusLongPress(ev.action.id);
actionManager.stationStatusLongPress(ev.action);
} else {
actionManager.stationStatusShortPress(ev.action.id);
actionManager.stationStatusShortPress(ev.action);
}
}
}
Expand Down
97 changes: 60 additions & 37 deletions src/managers/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,31 +133,6 @@ class ActionManager extends EventEmitter {
this.emit("actionAdded", controller);
}

/**
* Called when a station status action has a long press. Resets the
* station status and refreshses its state.
* @param actionId The ID of the action that had the long press
*/
public stationStatusLongPress(actionId: string) {
const savedAction = this.getStationStatusControllers().find(
(entry) => entry.action.id === actionId
);

if (!savedAction) {
return;
}

savedAction.reset();
trackAudioManager.refreshStationState(savedAction.callsign);

savedAction.action.showOk().catch((error: unknown) => {
handleAsyncException(
"Unable to show OK on station status button:",
error
);
});
}

/**
* Called when a TrackAudio status action keydown event is triggered.
* Forces a refresh of the TrackAudio status.
Expand All @@ -179,9 +154,9 @@ class ActionManager extends EventEmitter {
* Called when an ATIS letter action has a short press. Clears the state.
* @param actionId The ID of the action that had the short press
*/
public atisLetterShortPress(actionId: string) {
public atisLetterShortPress(action: Action) {
const savedAction = this.getAtisLetterControllers().find(
(entry) => entry.action.id === actionId
(entry) => entry.action.id === action.id
);

if (!savedAction) {
Expand All @@ -195,9 +170,9 @@ class ActionManager extends EventEmitter {
* Called when an ATIS letter action has a long press. Refreshses the ATIS.
* @param actionId The ID of the action that had the long press
*/
public atisLetterLongPress(actionId: string) {
public atisLetterLongPress(action: Action) {
const savedAction = this.getAtisLetterControllers().find(
(entry) => entry.action.id === actionId
(entry) => entry.action.id === action.id
);

if (!savedAction) {
Expand All @@ -207,7 +182,7 @@ class ActionManager extends EventEmitter {
savedAction.reset();
vatsimManager.refresh();

savedAction.action.showOk().catch((error: unknown) => {
action.showOk().catch((error: unknown) => {
handleAsyncException("Unable to show OK on ATIS button:", error);
});
}
Expand Down Expand Up @@ -578,13 +553,15 @@ class ActionManager extends EventEmitter {
}

/**
* Toggles the tx on both the primary and hotline frequency.
* @param id The action id to toggle the state of
* Handles the short press of a hotline action. Toggles the tx on both the primary and hotline frequency.
* @param actionId The action id to toggle the state of
*/
public toggleHotline(id: string): void {
const foundAction = this.actions.find((entry) => entry.action.id === id);
public hotlineShortPress(action: Action): void {
const foundAction = this.getHotlineControllers().find(
(entry) => entry.action.id === action.id
);

if (!foundAction || !isHotlineController(foundAction)) {
if (!foundAction) {
return;
}

Expand Down Expand Up @@ -623,14 +600,35 @@ class ActionManager extends EventEmitter {
});
}

public hotlineLongPress(action: Action) {
const foundAction = this.getHotlineControllers().find(
(entry) => entry.action.id === action.id
);

if (!foundAction) {
return;
}

foundAction.reset();
trackAudioManager.refreshStationState(foundAction.primaryCallsign);
trackAudioManager.refreshStationState(foundAction.hotlineCallsign);

action.showOk().catch((error: unknown) => {
handleAsyncException(
"Unable to show OK status on TrackAudio action: ",
error
);
});
}

/**
* Handles a short press of a station status action. Toggles the
* the tx, rx, xc, or spkr state of a frequency bound to a StreamDeck action.
* @param actionId The action id to toggle the state of
*/
public stationStatusShortPress(actionId: string): void {
public stationStatusShortPress(action: Action): void {
const foundAction = this.actions.find(
(entry) => entry.action.id === actionId
(entry) => entry.action.id === action.id
);

if (!foundAction || !isStationStatusController(foundAction)) {
Expand All @@ -655,6 +653,31 @@ class ActionManager extends EventEmitter {
});
}

/**
* Called when a station status action has a long press. Resets the
* station status and refreshses its state.
* @param actionId The ID of the action that had the long press
*/
public stationStatusLongPress(action: Action) {
const savedAction = this.getStationStatusControllers().find(
(entry) => entry.action.id === action.id
);

if (!savedAction) {
return;
}

savedAction.reset();
trackAudioManager.refreshStationState(savedAction.callsign);

action.showOk().catch((error: unknown) => {
handleAsyncException(
"Unable to show OK on station status button:",
error
);
});
}

/**
* Updates the connection state on all TrackAudio status buttons to the current connected states
* and updates the background image to the appropriate state image.
Expand Down

0 comments on commit 47a1a12

Please sign in to comment.