Skip to content

Commit

Permalink
feet: connect when simbridge status changes, not automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjozork committed Mar 31, 2024
1 parent e4635e1 commit 021b20c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
18 changes: 16 additions & 2 deletions fbw-common/src/systems/instruments/src/remote-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Wait } from '@microsoft/msfs-sdk';
import { NXDataStore } from '@flybywiresim/fbw-sdk';
import { ClientState, NXDataStore, SimBridgeClientState } from '@flybywiresim/fbw-sdk';
import { protocolV0 } from '@flybywiresim/remote-bridge-types';
import { Base64 } from 'js-base64';

Expand Down Expand Up @@ -80,7 +80,14 @@ export class RemoteClient {
this.clientName = options.clientName;
this.fileDownloadBasePath = options.fileDownloadBasePath;

this.attemptConnect();
ClientState.getInstance().simBridgeConnectionState.sub((state) => {
if (state === SimBridgeClientState.CONNECTED) {
this.attemptConnect();
} else if (this.ws) {
this.ws.close();
}
});

this.fetchInstrumentsMetadata(options.instrumentsMetadataFile).then((metadata) => this.instruments = metadata);
}

Expand Down Expand Up @@ -109,6 +116,13 @@ export class RemoteClient {
}

private attemptConnect(): void {
const simBridgeClientState = ClientState.getInstance().getSimBridgeClientState();

if (simBridgeClientState !== SimBridgeClientState.CONNECTED) {
console.log(`[RemoteClient](attemptConnect) Not attempting to connect, as SimBridge client is in state "${simBridgeClientState}"`);
return;
}

this.connectionAttemptCount++;

console.log(`[RemoteClient](attemptConnect) Attempting to connect (${this.url}). attempt #${this.connectionAttemptCount}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

/* eslint-disable no-console */
import { NXDataStore } from '@flybywiresim/fbw-sdk';
import { Subject, Subscribable } from '@microsoft/msfs-sdk';
import { Health } from './Health';

/**
Expand Down Expand Up @@ -41,7 +42,9 @@ export class ClientState {
private maxSimBridgeConnectionAttempts: number = 60;

// Indicates the state of the client connection to the SimBridge server
private simBridgeState: SimBridgeClientState = SimBridgeClientState.OFF;
private readonly simBridgeState = Subject.create(SimBridgeClientState.OFF);

public readonly simBridgeConnectionState: Subscribable<SimBridgeClientState> = this.simBridgeState;

/**
* Private constructor for the singleton. Start checking the server availability regularly
Expand Down Expand Up @@ -102,14 +105,14 @@ export class ClientState {
* @returns {SimBridgeClientState}
*/
public getSimBridgeClientState(): SimBridgeClientState {
return this.simBridgeState;
return this.simBridgeState.get();
}

/**
* Returns true if the SimBridgeClientState is CONNECTED
*/
public isConnected(): boolean {
return this.simBridgeState === SimBridgeClientState.CONNECTED;
return this.simBridgeState.get() === SimBridgeClientState.CONNECTED;
}

/**
Expand All @@ -119,18 +122,18 @@ export class ClientState {
*/
private setSimBridgeState() {
if (this.available) {
this.simBridgeState = SimBridgeClientState.CONNECTED;
this.simBridgeState.set(SimBridgeClientState.CONNECTED);
return;
}
switch (this.simBridgeEnabledSetting) {
case 'AUTO ON':
this.simBridgeState = SimBridgeClientState.CONNECTING;
this.simBridgeState.set(SimBridgeClientState.CONNECTING);
break;
case 'AUTO OFF':
this.simBridgeState = SimBridgeClientState.OFFLINE;
this.simBridgeState.set(SimBridgeClientState.OFFLINE);
break;
default:
this.simBridgeState = SimBridgeClientState.OFF;
this.simBridgeState.set(SimBridgeClientState.OFF);
}
}

Expand Down

0 comments on commit 021b20c

Please sign in to comment.