From aba9b9664164a671524c9fdaa231c067e2f537b3 Mon Sep 17 00:00:00 2001 From: Saschl <19493808+Saschl@users.noreply.github.com> Date: Wed, 23 Oct 2024 00:43:20 +0200 Subject: [PATCH] fix: make datastorage aircraft specific (#9116) * fix: make datastorage aircraft specific * fix: remove usages of legacy persistence.ts * use common name for datastore update event (cherry picked from commit 8c58f8bf8948acd094128749163975909725b23d) --- .../html_ui/Pages/A32NX_Utils/NXDataStore.js | 4 +- .../src/Common/LegacyCdsDisplayUnit.tsx | 2 +- .../instruments/src/Common/persistence.tsx | 2 +- .../systems/instruments/src/SD/StatusArea.tsx | 2 +- fbw-a380x/src/systems/shared/src/NXUnits.ts | 33 ----- .../src/systems/shared/src/persistence.ts | 51 ------- fbw-a380x/src/systems/shared/src/units.ts | 136 ------------------ .../src/systems/shared/src/persistence.ts | 9 +- 8 files changed, 10 insertions(+), 229 deletions(-) delete mode 100644 fbw-a380x/src/systems/shared/src/NXUnits.ts delete mode 100644 fbw-a380x/src/systems/shared/src/persistence.ts delete mode 100644 fbw-a380x/src/systems/shared/src/units.ts diff --git a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXDataStore.js b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXDataStore.js index 0043a27937e..3dc7b61f531 100644 --- a/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXDataStore.js +++ b/fbw-a32nx/src/base/flybywire-aircraft-a320-neo/html_ui/Pages/A32NX_Utils/NXDataStore.js @@ -19,11 +19,11 @@ class NXDataStore { static set(key, val) { SetStoredData(`A32NX_${key}`, val); - this.listener.triggerToAllSubscribers('A32NX_NXDATASTORE_UPDATE', key, val); + this.listener.triggerToAllSubscribers('FBW_NXDATASTORE_UPDATE', key, val); } static subscribe(key, callback) { - return Coherent.on('A32NX_NXDATASTORE_UPDATE', (updatedKey, value) => { + return Coherent.on('FBW_NXDATASTORE_UPDATE', (updatedKey, value) => { if (key === '*' || key === updatedKey) { callback(updatedKey, value); } diff --git a/fbw-a380x/src/systems/instruments/src/Common/LegacyCdsDisplayUnit.tsx b/fbw-a380x/src/systems/instruments/src/Common/LegacyCdsDisplayUnit.tsx index a923db2128b..015199ccdfd 100644 --- a/fbw-a380x/src/systems/instruments/src/Common/LegacyCdsDisplayUnit.tsx +++ b/fbw-a380x/src/systems/instruments/src/Common/LegacyCdsDisplayUnit.tsx @@ -1,5 +1,5 @@ import React, { forwardRef, PropsWithChildren, useCallback, useEffect, useRef, useState } from 'react'; -import { NXDataStore } from '@shared/persistence'; +import { NXDataStore } from '@flybywiresim/fbw-sdk'; import { DcElectricalBus } from '@shared/electrical'; import { useSimVar } from './simVars'; import { useUpdate } from './hooks'; diff --git a/fbw-a380x/src/systems/instruments/src/Common/persistence.tsx b/fbw-a380x/src/systems/instruments/src/Common/persistence.tsx index ca0b461803e..967575bd808 100644 --- a/fbw-a380x/src/systems/instruments/src/Common/persistence.tsx +++ b/fbw-a380x/src/systems/instruments/src/Common/persistence.tsx @@ -1,5 +1,5 @@ import { useEffect, useState } from 'react'; -import { NXDataStore } from '@shared/persistence'; +import { NXDataStore } from '@flybywiresim/fbw-sdk'; /** * This hook allows to read and set a persistent storage property. diff --git a/fbw-a380x/src/systems/instruments/src/SD/StatusArea.tsx b/fbw-a380x/src/systems/instruments/src/SD/StatusArea.tsx index f32eb4cd4de..67d8e4c358b 100644 --- a/fbw-a380x/src/systems/instruments/src/SD/StatusArea.tsx +++ b/fbw-a380x/src/systems/instruments/src/SD/StatusArea.tsx @@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react'; import { useSimVar } from '@instruments/common/simVars'; import { useArinc429Var } from '@instruments/common/arinc429'; -import { NXUnits } from '@shared/NXUnits'; +import { NXUnits } from '@flybywiresim/fbw-sdk'; import { Layer } from '@instruments/common/utils'; export const StatusArea = () => { diff --git a/fbw-a380x/src/systems/shared/src/NXUnits.ts b/fbw-a380x/src/systems/shared/src/NXUnits.ts deleted file mode 100644 index 96989d9a6a3..00000000000 --- a/fbw-a380x/src/systems/shared/src/NXUnits.ts +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Unit conversion utilities - */ -import { NXDataStore } from '@shared/persistence'; - -export class NXUnits { - private static _metricWeight: boolean; - - static get metricWeight() { - if (NXUnits._metricWeight === undefined) { - NXDataStore.getAndSubscribe( - 'CONFIG_USING_METRIC_UNIT', - (_, value: string) => { - NXUnits._metricWeight = value === '1'; - }, - '1', - ); - } - return NXUnits._metricWeight; - } - - static userToKg(value: number) { - return NXUnits.metricWeight ? value : value / 2.204625; - } - - static kgToUser(value: number) { - return NXUnits.metricWeight ? value : value * 2.204625; - } - - static userWeightUnit() { - return NXUnits.metricWeight ? 'KG' : 'LBS'; // EIS uses S suffix on LB - } -} diff --git a/fbw-a380x/src/systems/shared/src/persistence.ts b/fbw-a380x/src/systems/shared/src/persistence.ts deleted file mode 100644 index 7c04907d520..00000000000 --- a/fbw-a380x/src/systems/shared/src/persistence.ts +++ /dev/null @@ -1,51 +0,0 @@ -type StorageValue = string | number; - -type StorageContents = T extends undefined ? StorageValue : T; - -declare function GetStoredData(property: string, defaultValue?: StorageContents); -declare function SetStoredData(property: string, newValue: StorageContents); - -type SubscribeCallback = (key: string, value: string) => void; -type SubscribeCancellation = () => void; - -/** - * Allows interacting with the persistent storage - */ -export class NXDataStore { - /** - * Reads a value from persistent storage - * - * @param key The property key - * @param defaultVal The default value if the property is not set - */ - static get(key: string, defaultVal?: StorageContents): StorageContents { - const val = GetStoredData(`A32NX_${key}`); - if (!val) { - return defaultVal; - } - return val; - } - - /** - * Sets a value in persistent storage - * - * @param key The property key - * @param val The value to assign to the property - */ - static set(key: string, val: StorageContents): void { - SetStoredData(`A32NX_${key}`, val); - } - - static subscribe(key: string, callback: SubscribeCallback): SubscribeCancellation { - return Coherent.on('A32NX_NXDATASTORE_UPDATE', (updatedKey: string, value: string) => { - if (key === '*' || key === updatedKey) { - callback(updatedKey, value); - } - }).clear; - } - - static getAndSubscribe(key: string, callback: SubscribeCallback, defaultVal?: string): SubscribeCancellation { - callback(key, NXDataStore.get(key, defaultVal)); - return NXDataStore.subscribe(key, callback); - } -} diff --git a/fbw-a380x/src/systems/shared/src/units.ts b/fbw-a380x/src/systems/shared/src/units.ts deleted file mode 100644 index 34562181e55..00000000000 --- a/fbw-a380x/src/systems/shared/src/units.ts +++ /dev/null @@ -1,136 +0,0 @@ -import { NXDataStore } from './persistence'; - -// SI base units -export type Celsius = number; // derived unit -export type HectoPascal = number; // derived unit -export type KiloGram = number; -export type Metre = number; -export type Litre = number; - -// USCS base units -export type Fahrenheit = number; -export type Foot = number; -export type Pound = number; -export type Gallon = number; - -export type InchOfMercury = number; - -export class Units { - private static mMetricUnits: boolean; - - static get usingMetric(): boolean { - if (Units.mMetricUnits === undefined) { - NXDataStore.getAndSubscribe('CONFIG_USING_METRIC_UNIT', (_: string, value: string) => { - Units.mMetricUnits = value === '1'; - }); - } - return Units.mMetricUnits; - } - - static poundToKilogram(value: Pound): KiloGram { - return value * 0.4535934; - } - - static kilogramToPound(value: KiloGram): Pound { - return value / 0.4535934; - } - - static userToKilogram(value: Pound): KiloGram { - return Units.usingMetric ? value : Units.poundToKilogram(value); - } - - static kilogramToUser(value: KiloGram): Pound | KiloGram { - return Units.usingMetric ? value : Units.kilogramToPound(value); - } - - static get userWeightSuffixEis2(): 'kg' | 'lbs' { - // EIS uses S suffix on LB - return Units.usingMetric ? 'kg' : 'lbs'; - } - - static footToMetre(value: Foot): Metre { - return value / 3.28084; - } - - static metreToFoot(value: Metre): Foot { - return value * 3.28084; - } - - static userToMetre(value: Foot): Metre { - return Units.usingMetric ? value : Units.footToMetre(value); - } - - static metreToUser(value: Metre): Foot | Metre { - return Units.usingMetric ? value : Units.metreToFoot(value); - } - - static get userLengthSuffixEis2(): 'm' | 'ft' { - return Units.usingMetric ? 'm' : 'ft'; - } - - static fahrenheitToCelsius(value: Fahrenheit): Celsius { - return ((value - 32) * 5) / 9; - } - - static celsiusToFahrenheit(value: Celsius): Fahrenheit { - return (value * 9) / 5 + 32; - } - - static userToCelsius(value: Fahrenheit): Celsius { - return Units.usingMetric ? value : Units.fahrenheitToCelsius(value); - } - - static celsiusToUser(value: Celsius): Fahrenheit | Celsius { - return Units.usingMetric ? value : Units.celsiusToFahrenheit(value); - } - - static get userTemperatureSuffixEis2(): '°C' | '°F' { - return Units.usingMetric ? '°C' : '°F'; - } - - static inchOfMercuryToHectopascal(value: InchOfMercury): HectoPascal { - return value * 33.863886666667; - } - - static hectopascalToInchOfMercury(value: HectoPascal): InchOfMercury { - return value / 33.863886666667; - } - - static userToHectopascal(value: InchOfMercury): HectoPascal { - return Units.usingMetric ? value : Units.inchOfMercuryToHectopascal(value); - } - - static hectopascalToUser(value: Celsius): InchOfMercury { - return Units.usingMetric ? value : Units.hectopascalToInchOfMercury(value); - } - - static hectopascalToUserString(value: Celsius): string { - return Units.usingMetric - ? `${Math.round(value)}` - : (Math.round(Units.hectopascalToInchOfMercury(value)) / 100).toFixed(2); - } - - static get userPressureSuffixEis2(): 'hPa' | 'in.Hg' { - return Units.usingMetric ? 'hPa' : 'in.Hg'; - } - - static gallonToLitre(value: number): Litre { - return value * 3.78541; - } - - static litreToGallon(value: number): Gallon { - return value / 3.78541; - } - - static litreToUser(value: number): Litre | Gallon { - return Units.usingMetric ? value : value * 0.264172052358148; - } - - static userToLitre(value: number): Litre { - return Units.usingMetric ? value : value / 0.264172052358148; - } - - static get userVolumeSuffixEis2(): 'l' | 'gal' { - return Units.usingMetric ? 'l' : 'gal'; - } -} diff --git a/fbw-common/src/systems/shared/src/persistence.ts b/fbw-common/src/systems/shared/src/persistence.ts index b9c9d2b0915..b9a40fd105e 100644 --- a/fbw-common/src/systems/shared/src/persistence.ts +++ b/fbw-common/src/systems/shared/src/persistence.ts @@ -5,6 +5,7 @@ type SubscribeCancellation = () => void; * Allows interacting with the persistent storage */ export class NXDataStore { + private static aircraftProjectPrefix: string = process.env.AIRCRAFT_PROJECT_PREFIX.toUpperCase(); private static mListener: ViewListener.ViewListener; private static get listener() { @@ -22,7 +23,7 @@ export class NXDataStore { static get(key: string, defaultVal: string): string; static get(key: string, defaultVal?: string): string | undefined; static get(key: string, defaultVal?: string): any { - const val = GetStoredData(`A32NX_${key}`); + const val = GetStoredData(`${this.aircraftProjectPrefix}_${key}`); // GetStoredData returns null on error, or empty string for keys that don't exist (why isn't that an error??) // We could use SearchStoredData, but that spams the console with every key (somebody left their debug print in) if (val === null || val.length === 0) { @@ -38,12 +39,12 @@ export class NXDataStore { * @param val The value to assign to the property */ static set(key: string, val: string): void { - SetStoredData(`A32NX_${key}`, val); - this.listener.triggerToAllSubscribers('A32NX_NXDATASTORE_UPDATE', key, val); + SetStoredData(`${this.aircraftProjectPrefix}_${key}`, val); + this.listener.triggerToAllSubscribers('FBW_NXDATASTORE_UPDATE', key, val); } static subscribe(key: string, callback: SubscribeCallback): SubscribeCancellation { - return Coherent.on('A32NX_NXDATASTORE_UPDATE', (updatedKey: string, value: string) => { + return Coherent.on('FBW_NXDATASTORE_UPDATE', (updatedKey: string, value: string) => { if (key === '*' || key === updatedKey) { callback(updatedKey, value); }