Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: EFB Setting to enable MCDU Server connection to avoid too many connection attempts #6947

Merged
merged 12 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class A320_Neo_CDU_MainDisplay extends FMCMainDisplay {
this.updateRequest = false;
this.initB = false;
this.lastPowerState = 0;
this.socket = undefined;
this.socketConnectionRetries = 0;
this.PageTimeout = {
Fast: 500,
Medium: 1000,
Expand Down Expand Up @@ -292,11 +294,25 @@ class A320_Neo_CDU_MainDisplay extends FMCMainDisplay {
}

// The MCDU is a client to the MCDU Server and tries to connect in regular intervals.
// every 2500ms
if (this.mcduServerConnectUpdateThrottler.canUpdate(_deltaTime) !== -1
&& (!this.socket || this.socket.readyState !== 1)) {

this.connectWebsocket(NXDataStore.get("CONFIG_EXTERNAL_MCDU_PORT", "8380"));
const connectionEnabled = NXDataStore.get("CONFIG_EXTERNAL_MCDU_SERVER_ENABLED", '1');
const maxConnectionAttempts = 60;
if (this.mcduServerConnectUpdateThrottler.canUpdate(_deltaTime) !== -1) {
// try to connect websocket if enabled and none existing
if (connectionEnabled === '1' && (!this.socket || this.socket.readyState !== 1)) {
// we try to connect for 5min then we deactivate the connection setting
if (this.socketConnectionRetries++ >= maxConnectionAttempts) {
console.log("Maximum number of connection attempts to MCDU Server exceeded. No more attempts.");
NXDataStore.set("CONFIG_EXTERNAL_MCDU_SERVER_ENABLED", '0');
this.socketConnectionRetries = 0;
} else {
console.log(`Attempting MCDU Server connection ${this.socketConnectionRetries} of ${maxConnectionAttempts} attempts.`);
this.connectWebsocket(NXDataStore.get("CONFIG_EXTERNAL_MCDU_PORT", "8380"));
}
} else if (connectionEnabled !== '1' && this.socket && this.socket.readyState === 1) {
// if there is a socket existing but enabled setting has been changed then close connection
this.socket.close();
delete this.socket;
}
}

// There is no (known) event when power is turned on or off (e.g. Ext Pwr) and remote clients
Expand Down Expand Up @@ -1427,9 +1443,10 @@ class A320_Neo_CDU_MainDisplay extends FMCMainDisplay {
* Attempts to connect to a local websocket server
*/
connectWebsocket(port) {

if (this.socket && this.socket.readyState) {
Saschl marked this conversation as resolved.
Show resolved Hide resolved
this.socket.close();
this.socket = undefined;
delete this.socket;
}

const url = `ws://127.0.0.1:${port}`;
Expand All @@ -1449,6 +1466,7 @@ class A320_Neo_CDU_MainDisplay extends FMCMainDisplay {
(new NXNotif).showNotification({title: "MCDU CONNECTED", message: "Successfully connected to MCDU server.", timeout: 5000});
this.sendToSocket("mcduConnected");
this.sendUpdate();
this.socketConnectionRetries = 0;
};

this.socket.onmessage = (event) => {
Expand Down
13 changes: 13 additions & 0 deletions src/instruments/src/EFB/Settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ const SimOptionsPage = () => {
const [dynamicRegistration, setDynamicRegistration] = usePersistentProperty('DYNAMIC_REGISTRATION_DECAL', '0');
const [defaultBaro, setDefaultBaro] = usePersistentProperty('CONFIG_INIT_BARO_UNIT', 'AUTO');
const [mcduServerPort, setMcduServerPort] = usePersistentProperty('CONFIG_EXTERNAL_MCDU_PORT', '8380');
const [mcduServerEnabled, setMcduServerEnabled] = usePersistentProperty('CONFIG_EXTERNAL_MCDU_SERVER_ENABLED', '1');
const [radioReceiverUsage, setRadioReceiverUsage] = usePersistentProperty('RADIO_RECEIVER_USAGE_ENABLED', '0');
const [, setRadioReceiverUsageSimVar] = useSimVar('L:A32NX_RADIO_RECEIVER_USAGE_ENABLED', 'number', 0);

Expand Down Expand Up @@ -335,6 +336,18 @@ const SimOptionsPage = () => {
/>
</div>

<div className="py-4 flex flex-row justify-between items-center">
<span>
<span className="text-lg text-gray-300">Enable MCDU Server Connection (deactivates after 5min)</span>
frankkopp marked this conversation as resolved.
Show resolved Hide resolved
</span>
<Toggle
value={mcduServerEnabled === '1'}
onToggle={(value) => {
setMcduServerEnabled(value ? '1' : '0');
}}
/>
</div>

<div className="py-4 flex flex-row justify-between items-center">
<span>
<span className="text-lg text-gray-300">Use calculated ILS signals</span>
Expand Down