Skip to content

Commit

Permalink
Refactor action.ts (#61)
Browse files Browse the repository at this point in the history
* Refactor action.ts
Fixes #55

* Fix build breaks
  • Loading branch information
neilenns authored Jan 6, 2025
1 parent 2ed7c22 commit db0e6a8
Show file tree
Hide file tree
Showing 24 changed files with 255 additions and 199 deletions.
51 changes: 32 additions & 19 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
{
/* Prefer tabs over spaces for accessibility */
"editor.insertSpaces": false,
"editor.detectIndentation": false,
/* Explorer */
"explorer.fileNesting.enabled": true,
"explorer.fileNesting.patterns": {
"*.js": "${capture}.js.map, ${capture}.min.js, ${capture}.d.ts",
"package.json": "package-lock.json, yarn.lock, pnpm-lock.yaml, rollup.config.mjs, tsconfig.json",
"eslint.config.js": ".markdownlint.jsonc",
"README.md": "DEVELOPMENT.md"
},
"files.exclude": {
"node_modules": true,
"com.neil-enns.vatis.streamDeckPlugin": true,
},
"eslint.useFlatConfig": true,
"git.enableSmartCommit": true,
"[svg]": {
/* Prefer tabs over spaces for accessibility */
"editor.insertSpaces": false,
"editor.detectIndentation": false,
/* Explorer */
"explorer.fileNesting.enabled": true,
"explorer.fileNesting.patterns": {
"*.js": "${capture}.js.map, ${capture}.min.js, ${capture}.d.ts",
"package.json": "package-lock.json, yarn.lock, pnpm-lock.yaml, rollup.config.mjs, tsconfig.json",
"eslint.config.js": ".markdownlint.jsonc",
"README.md": "DEVELOPMENT.md"
},
"files.exclude": {
"node_modules": true,
"com.neil-enns.vatis.streamDeckPlugin": true
},
"eslint.useFlatConfig": true,
"git.enableSmartCommit": true,
"[svg]": {
"editor.formatOnSave": false
},
"cSpell.words": [
"Addv",
"Atis",
"atisletter",
"elgato",
"getv",
"handlev",
"streamdeck",
"Typeguard",
"Updatev",
"vatis",
"vatisstatus"
]
}
}
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ After installation the plugin actions are available under the vATIS category:
The ATIS letter action displays the station name, current AITS letter, and current altimeter
for a single station in vATIS.

Pressing the action clears the new ATIS indicator in vATIS and on the action. A long
press of the action clears the new ATIS indicator on all stations in vATIS.
Pressing the action clears the new ATIS indicator in vATIS and on the action. A long press of the action clears the new ATIS indicator on all stations in vATIS.

By default the pressure displays in white. For pressure reported in inches of mercury,
if the value falls below 29.92 the value will show in red as a reminder that FL180 (or FL190,
Expand All @@ -46,7 +45,7 @@ like this to display the information on your Stream Deck:
| Station | The name of the station you want to display status for. Required. | |
| Type | The type of the station. | Combined |
| Current | The image to display when the ATIS letter shown is current. Optional. | ![Black background, white text](docs/images/atis-connected.png) ![Black background, white text, red text for pressure](docs/images/atis-warning.png) |
| Unavailable | The iamge to display when there is no connection to vATIS. Optional. | ![Black background, "ATIS" for letter, grey text](docs/images/atis-notconnected.png) |
| Unavailable | The image to display when there is no connection to vATIS. Optional. | ![Black background, "ATIS" for letter, grey text](docs/images/atis-notconnected.png) |
| Updated | The image to display when the ATIS letter updated to a new one. Optional. | ![Orange background, white text](docs/images/atis-updated.png) |

The default display automatically includes the station name, ATIS letter, and altimeter. When specifying
Expand Down
16 changes: 10 additions & 6 deletions src/actions/atisLetter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ import {
WillAppearEvent,
WillDisappearEvent,
} from "@elgato/streamdeck";
import { handleAddAtisLetter } from "@events/streamdeck/atisLetter/addAtisLetter";
import { handleAtisLetterLongPress } from "@events/streamdeck/atisLetter/atisLetterLongPress";
import { handleAtisLetterShortPress } from "@events/streamdeck/atisLetter/atisLetterShortPress";
import { handleUpdateAtisLetterSettings } from "@events/streamdeck/atisLetter/updateAtisLetterSettings";
import { handleRemove } from "@events/streamdeck/remove";
import { AtisType } from "@interfaces/messages";
import actionManager from "@managers/action";
import { LONG_PRESS_THRESHOLD } from "@utils/constants";

@action({ UUID: "com.neil-enns.vatis.atisletter" })
Expand All @@ -30,14 +34,14 @@ export class AtisLetter extends SingletonAction<AtisLetterSettings> {
return;
}

actionManager.addAtisLetter(ev.action, ev.payload.settings);
handleAddAtisLetter(ev.action, ev.payload.settings);
}

// When the action is removed from a profile it also gets removed from the ActionManager.
override onWillDisappear(
ev: WillDisappearEvent<AtisLetterSettings>
): void | Promise<void> {
actionManager.remove(ev.action);
handleRemove(ev.action);
}

// When settings are received the ActionManager is called to update the existing
Expand All @@ -51,7 +55,7 @@ export class AtisLetter extends SingletonAction<AtisLetterSettings> {
return;
}

actionManager.updateAtisLetterSettings(ev.action, ev.payload.settings);
handleUpdateAtisLetterSettings(ev.action, ev.payload.settings);
}

override onKeyDown(): Promise<void> | void {
Expand All @@ -62,9 +66,9 @@ export class AtisLetter extends SingletonAction<AtisLetterSettings> {
const pressLength = Date.now() - this._keyDownStart;

if (pressLength > LONG_PRESS_THRESHOLD) {
actionManager.atisLetterLongPress(ev.action);
handleAtisLetterLongPress();
} else {
actionManager.atisLetterShortPress(ev.action);
handleAtisLetterShortPress(ev.action);
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/actions/vAtisStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {
WillAppearEvent,
WillDisappearEvent,
} from "@elgato/streamdeck";
import actionManager from "@managers/action";
import { handleRemove } from "@events/streamdeck/remove";
import { handleAddvAtisStatus } from "@events/streamdeck/vAtisStatus/addvAtisStatus";
import { handleUpdatevAtisStatusSettings } from "@events/streamdeck/vAtisStatus/updatevAtisStatusSettings";
import { handlevAtisStatusLongPress } from "@events/streamdeck/vAtisStatus/vatisStatusLongPress";
import { LONG_PRESS_THRESHOLD } from "@utils/constants";

@action({ UUID: "com.neil-enns.vatis.vatisstatus" })
Expand All @@ -28,14 +31,14 @@ export class vAtisAudioStatus extends SingletonAction<vAtisStatusSettings> {
return;
}

actionManager.addvAtis(ev.action, ev.payload.settings);
handleAddvAtisStatus(ev.action, ev.payload.settings);
}

// When the action is removed from a profile it also gets removed from the ActionManager.
override onWillDisappear(
ev: WillDisappearEvent<vAtisStatusSettings>
): void | Promise<void> {
actionManager.remove(ev.action);
handleRemove(ev.action);
}

override onDidReceiveSettings(
Expand All @@ -47,7 +50,7 @@ export class vAtisAudioStatus extends SingletonAction<vAtisStatusSettings> {
return;
}

actionManager.updatevAtisStatus(ev.action, ev.payload.settings);
handleUpdatevAtisStatusSettings(ev.action, ev.payload.settings);
}

override onKeyDown(): Promise<void> | void {
Expand All @@ -58,7 +61,7 @@ export class vAtisAudioStatus extends SingletonAction<vAtisStatusSettings> {
const pressLength = Date.now() - this._keyDownStart;

if (pressLength > LONG_PRESS_THRESHOLD) {
actionManager.vAtisStatusLongPress(ev.action);
handlevAtisStatusLongPress(ev.action);
}
}
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions src/events/streamdeck/atisLetter/addAtisLetter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { AtisLetterSettings } from "@actions/atisLetter";
import { AtisLetterController } from "@controllers/atisLetter";
import { KeyAction } from "@elgato/streamdeck";
import actionManager from "@managers/action";

/**
* Adds an atis letter action to the action list. Emits an atisLetterAdded
* event after the action is added.
* @param action The action
* @param settings The settings for the action
*/
export const handleAddAtisLetter = (
action: KeyAction,
settings: AtisLetterSettings
) => {
const controller = new AtisLetterController(action, settings);

actionManager.add(controller);
actionManager.emit("atisLetterAdded", controller);
actionManager.emit("actionAdded", controller);
};
10 changes: 10 additions & 0 deletions src/events/streamdeck/atisLetter/atisLetterLongPress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import vAtisManager from "@managers/vatis";

/**
* Called when an ATIS letter action has a long press. Clears the new
* ATIS state on all stations.
* @param actionId The ID of the action that had the long press
*/
export const handleAtisLetterLongPress = () => {
vAtisManager.sendMessage({ type: "acknowledgeAtisUpdate" });
};
26 changes: 26 additions & 0 deletions src/events/streamdeck/atisLetter/atisLetterShortPress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { KeyAction } from "@elgato/streamdeck";
import actionManager from "@managers/action";
import vAtisManager from "@managers/vatis";

/**
* 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
*/
export const handleAtisLetterShortPress = (action: KeyAction) => {
const savedAction = actionManager
.getAtisLetterControllers()
.find((entry) => entry.action.id === action.id);

if (!savedAction?.station) {
return;
}

// Send a clear request to vATIS
vAtisManager.sendMessage({
type: "acknowledgeAtisUpdate",
value: {
station: savedAction.station,
atisType: savedAction.atisType,
},
});
};
33 changes: 33 additions & 0 deletions src/events/streamdeck/atisLetter/updateAtisLetterSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { AtisLetterSettings } from "@actions/atisLetter";
import { KeyAction } from "@elgato/streamdeck";
import actionManager from "@managers/action";

/**
* Updates the settings associated with an ATIS letter status action.
* Emits a atisLetterUpdated event if the settings require
* the action to refresh.
* @param action The action to update
* @param settings The new settings to use
*/
export const handleUpdateAtisLetterSettings = (
action: KeyAction,
settings: AtisLetterSettings
) => {
const savedAction = actionManager
.getAtisLetterControllers()
.find((entry) => entry.action.id === action.id);

if (!savedAction) {
return;
}

const requiresRefresh =
savedAction.settings.station !== settings.station ||
savedAction.settings.atisType !== settings.atisType;

savedAction.settings = settings;

if (requiresRefresh) {
actionManager.emit("atisLetterUpdated", savedAction);
}
};
10 changes: 10 additions & 0 deletions src/events/streamdeck/remove.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ActionContext } from "@elgato/streamdeck";
import actionManager from "@managers/action";

/**
* Removes an action from the list. Emits a removed event after the action is removed.
* @param action The action to remove
*/
export const handleRemove = (action: ActionContext) => {
actionManager.remove(action);
};
20 changes: 20 additions & 0 deletions src/events/streamdeck/vAtisStatus/addvAtisStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { vAtisStatusSettings } from "@actions/vAtisStatus";
import { vAtisStatusController } from "@controllers/vAtisStatus";
import { KeyAction } from "@elgato/streamdeck";
import actionManager from "@managers/action";

/**
* Adds a vATIS status action to the action list. Emits a vAtisStatusAdded event
* after the action is added.
* @param action The action to add
*/
export const handleAddvAtisStatus = (
action: KeyAction,
settings: vAtisStatusSettings
) => {
const controller = new vAtisStatusController(action, settings);

actionManager.add(controller);
actionManager.emit("vAtisStatusAdded", controller);
actionManager.emit("actionAdded", controller);
};
23 changes: 23 additions & 0 deletions src/events/streamdeck/vAtisStatus/updatevAtisStatusSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { vAtisStatusSettings } from "@actions/vAtisStatus";
import { KeyAction } from "@elgato/streamdeck";
import actionManager from "@managers/action";

/**
* Updates the settings associated with a vATIS status action.
* @param action The action to update
* @param settings The new settings to use
*/
export const handleUpdatevAtisStatusSettings = (
action: KeyAction,
settings: vAtisStatusSettings
) => {
const savedAction = actionManager
.getvAtisStatusControllers()
.find((entry) => entry.action.id === action.id);

if (!savedAction) {
return;
}

savedAction.settings = settings;
};
22 changes: 22 additions & 0 deletions src/events/streamdeck/vAtisStatus/vatisStatusLongPress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { KeyAction } from "@elgato/streamdeck";
import actionManager from "@managers/action";
import vAtisManager from "@managers/vatis";
import { handleAsyncException } from "@utils/handleAsyncException";

/**
* Called when a vATIS status action has a long press. Refreshes all
* ATIS actions.
* @param actionId The ID of the action that had the long press
*/
export const handlevAtisStatusLongPress = (action: KeyAction) => {
actionManager.getAtisLetterControllers().forEach((entry) => {
entry.reset();
});

// Refresh all the stations.
vAtisManager.refreshAtis();

action.showOk().catch((error: unknown) => {
handleAsyncException("Unable to show OK on ATIS button:", error);
});
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/interfaces/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export interface GetAtis {
type: "getAtis";
value?: {
station: string;
atisType: AtisType;
atisType?: AtisType;
};
}

Expand Down
Loading

0 comments on commit db0e6a8

Please sign in to comment.