Skip to content

Commit

Permalink
refactor: flypad config
Browse files Browse the repository at this point in the history
  • Loading branch information
2hwk committed Apr 4, 2024
1 parent dae6215 commit 94d4993
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, { FC } from 'react';
import { useSimVar, Units, AirframeType } from '@flybywiresim/fbw-sdk';
import { IconPlane } from '@tabler/icons';
import { Box, LightningFill, PeopleFill, Rulers, Speedometer2 } from 'react-bootstrap-icons';
import { t, A320NoseOutline, A380NoseOutline, useAppSelector } from '@flybywiresim/flypad';
import { t, A320NoseOutline, A380NoseOutline, useAppSelector, getMaxPax } from '@flybywiresim/flypad';

interface InformationEntryProps {
title: string;
Expand Down Expand Up @@ -42,10 +42,6 @@ export const OverviewPage = () => {
};

const airframeInfo = useAppSelector((state) => state.config.airframeInfo);
const cabinInfo = useAppSelector((state) => state.config.cabinInfo);

const seatMap = cabinInfo.seatMap;
const totalPax = seatMap.reduce((count, station) => count + station.rows.reduce((stationCount, row) => stationCount + row.seats.length, 0), 0);

return (
<div className="h-content-section-reduced border-theme-accent mr-3 w-min overflow-hidden rounded-lg border-2 p-6">
Expand Down Expand Up @@ -79,7 +75,7 @@ export const OverviewPage = () => {
<Box size={23} />
</InformationEntry>

<InformationEntry title={t('Dispatch.Overview.MaximumPassengers')} info={`${totalPax} passengers`}>
<InformationEntry title={t('Dispatch.Overview.MaximumPassengers')} info={`${getMaxPax()} passengers`}>
<PeopleFill size={23} />
</InformationEntry>
</div>
Expand Down
6 changes: 3 additions & 3 deletions fbw-common/src/systems/instruments/src/EFB/Efb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { distanceTo } from 'msfs-geo';
import { ErrorBoundary } from 'react-error-boundary';
import { MemoryRouter as Router } from 'react-router';
import { Provider } from 'react-redux';
import { setAirframeInfo, setCabinInfo, setFlypadPayloadInfo } from '@flybywiresim/flypad';
import { setAirframeInfo, setCabinInfo, setFlypadInfo } from '@flybywiresim/flypad';
import { Error as ErrorIcon } from './Assets/Error';
import { FailuresOrchestratorProvider } from './failures-orchestrator-provider';
import { AlertModal, ModalContainer, ModalProvider, useModals } from './UtilComponents/Modals/Modals';
Expand Down Expand Up @@ -105,10 +105,10 @@ const Efb = () => {
process.env.AIRCRAFT_VARIANT,
).then((info) => dispatch(setAirframeInfo(info)));

UniversalConfigProvider.fetchFlypadPayloadInfo(
UniversalConfigProvider.fetchFlypadInfo(
process.env.AIRCRAFT_PROJECT_PREFIX,
process.env.AIRCRAFT_VARIANT,
).then((info) => dispatch(setFlypadPayloadInfo(info)));
).then((info) => dispatch(setFlypadInfo(info)));

UniversalConfigProvider.fetchCabinInfo(
process.env.AIRCRAFT_PROJECT_PREFIX,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@

/* eslint-disable max-len */
import React, { useState } from 'react';
import { AircraftType, Units, useSimVar } from '@flybywiresim/fbw-sdk';
import { AirframeType, Units, useSimVar } from '@flybywiresim/fbw-sdk';
import { isSimbriefDataLoaded } from '@flybywiresim/flypad';
import { A320Fuel } from './A320_251N/A320Fuel';
import { A380Fuel } from './A380_842/A380Fuel';
import { useAppSelector } from '../../../Store/store';

export const Fuel = () => {
const [airframe] = useSimVar('L:A32NX_AIRCRAFT_TYPE', 'Enum');
const airframeInfo = useAppSelector((state) => state.config.airframeInfo);
const [isOnGround] = useSimVar('SIM ON GROUND', 'Bool', 8_059);
const simbriefUnits = useAppSelector((state) => state.simbrief.data.units);
const simbriefPlanRamp = useAppSelector((state) => state.simbrief.data.fuels.planRamp);

const simbriefDataLoaded = isSimbriefDataLoaded();

const [massUnitForDisplay] = useState(Units.usingMetric ? 'KGS' : 'LBS');
const [convertUnit] = useState(Units.usingMetric ? 1 : (1 / 0.4535934));

switch (airframe) {
case AircraftType.A380_842:
switch (airframeInfo.variant) {
case AirframeType.A380_842:
return (
<A380Fuel
simbriefDataLoaded={simbriefDataLoaded}
Expand All @@ -31,7 +30,7 @@ export const Fuel = () => {
isOnGround={isOnGround}
/>
);
case AircraftType.A320_251N:
case AirframeType.A320_251N:
default:
return (
<A320Fuel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import { PayloadProps } from '../PayloadPage';

export const A320Payload: React.FC<PayloadProps> = ({
airframeInfo,
payloadInfo,
flypadInfo,
cabinInfo,
maxPax,
maxCargo,
simbriefUnits,
simbriefBagWeight,
simbriefPaxWeight,
Expand Down Expand Up @@ -72,9 +74,6 @@ export const A320Payload: React.FC<PayloadProps> = ({
const [seatMap] = useState<PaxStationInfo[]>(cabinInfo.seatMap);
const [cargoMap] = useState<CargoStationInfo[]>(cabinInfo.cargoMap);

const maxPax = useMemo(() => seatMap.reduce((a, b) => a + b.capacity, 0), [seatMap]);
const maxCargo = useMemo(() => cargoMap.reduce((a, b) => a + b.weight, 0), [cargoMap]);

// Calculate Total Pax from Pax Flags
const totalPax = useMemo(() => {
let p = 0;
Expand Down Expand Up @@ -593,7 +592,7 @@ export const A320Payload: React.FC<PayloadProps> = ({
width={525}
height={511}
envelope={airframeInfo.designLimits.performanceEnvelope}
limits={payloadInfo.chartLimits}
limits={flypadInfo.chartLimits}
cg={boardingStarted ? Math.round(gwCgMac * 100) / 100 : Math.round(desiredGwCgMac * 100) / 100}
gw={boardingStarted ? Math.round(gw) : Math.round(gwDesired)}
mldwCg={boardingStarted ? Math.round(gwCgMac * 100) / 100 : Math.round(desiredGwCgMac * 100) / 100}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@

/* eslint-disable max-len */
import React, { useState } from 'react';
import { AirframeInfo, CabinInfo, FlypadPayloadInfo, PayloadType, Units, usePersistentProperty, useSimVar } from '@flybywiresim/fbw-sdk';
import { useAppSelector, isSimbriefDataLoaded } from '@flybywiresim/flypad';
import { AirframeInfo, CabinInfo, FlypadInfo, PayloadType, Units, usePersistentProperty, useSimVar } from '@flybywiresim/fbw-sdk';
import { useAppSelector, isSimbriefDataLoaded, getMaxPax, getMaxCargo } from '@flybywiresim/flypad';
import { A380Payload } from './WideBody/A380Payload';
import { A320Payload } from './NarrowBody/A320Payload';

export interface PayloadProps {
airframeInfo: AirframeInfo,
payloadInfo: FlypadPayloadInfo,
flypadInfo: FlypadInfo,
cabinInfo: CabinInfo,
maxPax: number,
maxCargo: number,
simbriefUnits: string,
simbriefBagWeight: number,
simbriefPaxWeight: number,
Expand All @@ -22,7 +24,6 @@ export interface PayloadProps {
payloadImported: boolean,
massUnitForDisplay: string,
isOnGround: boolean,

boardingStarted: boolean,
boardingRate: string,
setBoardingStarted: (boardingStarted: any) => void,
Expand All @@ -46,17 +47,19 @@ export const PayloadPage = () => {

const [massUnitForDisplay] = useState(Units.usingMetric ? 'KGS' : 'LBS');

const payloadInfo = useAppSelector((state) => state.config.flypadPayloadInfo);
const flypadInfo = useAppSelector((state) => state.config.flypadInfo);
const airframeInfo = useAppSelector((state) => state.config.airframeInfo);
const cabinInfo = useAppSelector((state) => state.config.cabinInfo);

switch (payloadInfo.type) {
switch (flypadInfo.type) {
case PayloadType.DoubleDeckPassengerOps:
return (
<A380Payload
airframeInfo={airframeInfo}
payloadInfo={payloadInfo}
flypadInfo={flypadInfo}
cabinInfo={cabinInfo}
maxPax={getMaxPax()}
maxCargo={getMaxCargo()}
simbriefUnits={simbriefUnits}
simbriefBagWeight={simbriefBagWeight}
simbriefPaxWeight={simbriefPaxWeight}
Expand All @@ -79,8 +82,10 @@ export const PayloadPage = () => {
return (
<A320Payload
airframeInfo={airframeInfo}
payloadInfo={payloadInfo}
flypadInfo={flypadInfo}
cabinInfo={cabinInfo}
maxPax={getMaxPax()}
maxCargo={getMaxCargo()}
simbriefUnits={simbriefUnits}
simbriefBagWeight={simbriefBagWeight}
simbriefPaxWeight={simbriefPaxWeight}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import { PayloadProps } from '../PayloadPage';

export const A380Payload: React.FC<PayloadProps> = ({
airframeInfo,
payloadInfo,
flypadInfo,
cabinInfo,
maxPax,
maxCargo,
simbriefUnits,
simbriefBagWeight,
simbriefPaxWeight,
Expand Down Expand Up @@ -98,9 +100,6 @@ export const A380Payload: React.FC<PayloadProps> = ({
const [seatMap] = useState<PaxStationInfo[]>(cabinInfo.seatMap);
const [cargoMap] = useState<CargoStationInfo[]>(cabinInfo.cargoMap);

const maxPax = useMemo(() => seatMap.reduce((a, b) => a + b.capacity, 0), [seatMap]);
const maxCargo = useMemo(() => cargoMap.reduce((a, b) => a + b.weight, 0), [cargoMap]);

// Calculate Total Pax from Pax Flags
const totalPax = useMemo(() => {
let p = 0;
Expand Down Expand Up @@ -668,7 +667,7 @@ export const A380Payload: React.FC<PayloadProps> = ({
width={525}
height={511}
envelope={airframeInfo.designLimits.performanceEnvelope}
limits={payloadInfo.chartLimits}
limits={flypadInfo.chartLimits}
cg={boardingStarted ? Math.round(gwCgMac * 100) / 100 : Math.round(desiredGwCgMac * 100) / 100}
gw={boardingStarted ? Math.round(gw) : Math.round(gwDesired)}
mldwCg={boardingStarted ? Math.round(gwCgMac * 100) / 100 : Math.round(desiredGwCgMac * 100) / 100}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
/* eslint-disable max-len */
// Copyright (c) 2023-2024 FlyByWire Simulations
// SPDX-License-Identifier: GPL-3.0

import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { AirframeInfo, CabinInfo, FlypadPayloadInfo } from '@flybywiresim/fbw-sdk';
import { AirframeInfo, CabinInfo, FlypadInfo } from '@flybywiresim/fbw-sdk';
import { store, RootState } from '../store';

interface AircraftState {
airframeInfo: AirframeInfo;
flypadPayloadInfo: FlypadPayloadInfo;
flypadInfo: FlypadInfo;
cabinInfo: CabinInfo;
}

const initialState: AircraftState = {
airframeInfo: null,
flypadPayloadInfo: null,
flypadInfo: null,
cabinInfo: null,
};

Expand All @@ -23,14 +25,19 @@ export const configSlice = createSlice({
setAirframeInfo: (state, action: PayloadAction<AirframeInfo>) => {
state.airframeInfo = action.payload;
},
setFlypadPayloadInfo: (state, action: PayloadAction<FlypadPayloadInfo>) => {
state.flypadPayloadInfo = action.payload;
setFlypadInfo: (state, action: PayloadAction<FlypadInfo>) => {
state.flypadInfo = action.payload;
},
setCabinInfo: (state, action: PayloadAction<CabinInfo>) => {
state.cabinInfo = action.payload;
},
},
});

export const { setAirframeInfo, setFlypadPayloadInfo, setCabinInfo } = configSlice.actions;
export const { setAirframeInfo, setFlypadInfo, setCabinInfo } = configSlice.actions;

export const getMaxPax = (): number => (store.getState() as RootState).config.cabinInfo.seatMap.reduce((count, station) => count + station.rows.reduce((stationCount, row) => stationCount + row.seats.length, 0), 0);

export const getMaxCargo = (): number => (store.getState() as RootState).config.cabinInfo.cargoMap.reduce((a, b) => a + b.weight, 0);

export default configSlice.reducer;
14 changes: 7 additions & 7 deletions fbw-common/src/systems/shared/src/UniversalConfigProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
/* eslint-disable no-underscore-dangle */

import JSON5 from 'json5';
import { AirframeInfo, CabinInfo, FlypadPayloadInfo } from './unifiedConfig';
import { AirframeInfo, CabinInfo, FlypadInfo } from './unifiedConfig';

export class UniversalConfigProvider {
private static airframeInfo: AirframeInfo;

private static flypadPayloadInfo: FlypadPayloadInfo;
private static flypadInfo: FlypadInfo;

private static cabinInfo: CabinInfo;

Expand Down Expand Up @@ -47,20 +47,20 @@ export class UniversalConfigProvider {
return this.airframeInfo;
}

public static async fetchFlypadPayloadInfo(aircraft: string, variant: string): Promise<FlypadPayloadInfo> {
if (this.flypadPayloadInfo) {
return this.flypadPayloadInfo;
public static async fetchFlypadInfo(aircraft: string, variant: string): Promise<FlypadInfo> {
if (this.flypadInfo) {
return this.flypadInfo;
}

const json = await this.fetchVFSJson5(aircraft, variant, '<atc_id>', 'flypad-payload');
this.flypadPayloadInfo = (
this.flypadInfo = (
{
type: json._type,
chartLimits: json.chartLimits,
}
);

return this.flypadPayloadInfo;
return this.flypadInfo;
}

public static async fetchCabinInfo(aircraft: string, variant: string): Promise<CabinInfo> {
Expand Down
2 changes: 1 addition & 1 deletion fbw-common/src/systems/shared/src/unifiedConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export enum PayloadType {
/**
* Contains the flypad-payload.json file's information in a structured way.
*/
export interface FlypadPayloadInfo {
export interface FlypadInfo {
type: PayloadType;
chartLimits: PayloadChartLimits;
}
Expand Down

0 comments on commit 94d4993

Please sign in to comment.