From 09989bb8458670784c7bf68854f60567f2214dd2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 24 May 2023 15:25:23 +0200 Subject: [PATCH] refactor(simulator): strong type worker data for evses MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/types/ChargingStationWorker.ts | 4 +-- src/types/index.ts | 1 + .../ChargingStationConfigurationUtils.ts | 31 ++++++++++--------- src/utils/MessageChannelUtils.ts | 2 +- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/types/ChargingStationWorker.ts b/src/types/ChargingStationWorker.ts index 41ca7aac..6fe529f9 100644 --- a/src/types/ChargingStationWorker.ts +++ b/src/types/ChargingStationWorker.ts @@ -20,7 +20,7 @@ export interface ChargingStationWorkerData extends WorkerData { chargingStationWorkerOptions?: ChargingStationWorkerOptions; } -type EvseStatusType = Omit & { +export type EvseStatusWorkerType = Omit & { connectors?: ConnectorStatus[]; }; @@ -28,7 +28,7 @@ export interface ChargingStationData extends WorkerData { started: boolean; stationInfo: ChargingStationInfo; connectors: ConnectorStatus[]; - evses: EvseStatusType[]; + evses: EvseStatusWorkerType[]; ocppConfiguration: ChargingStationOcppConfiguration; wsState?: | typeof WebSocket.CONNECTING diff --git a/src/types/index.ts b/src/types/index.ts index 27ffb6d7..fee7eab6 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -146,6 +146,7 @@ export { type ChargingStationWorkerMessage, type ChargingStationWorkerMessageData, ChargingStationWorkerMessageEvents, + type EvseStatusWorkerType, } from './ChargingStationWorker'; export type { ChargingStationInfo } from './ChargingStationInfo'; export type { diff --git a/src/utils/ChargingStationConfigurationUtils.ts b/src/utils/ChargingStationConfigurationUtils.ts index 3549fa7b..e61730c7 100644 --- a/src/utils/ChargingStationConfigurationUtils.ts +++ b/src/utils/ChargingStationConfigurationUtils.ts @@ -4,6 +4,7 @@ import type { ChargingStationAutomaticTransactionGeneratorConfiguration, ConnectorStatus, EvseStatusConfiguration, + EvseStatusWorkerType, } from '../types'; export const buildChargingStationAutomaticTransactionGeneratorConfiguration = ( @@ -30,30 +31,32 @@ export const buildConnectorsStatus = (chargingStation: ChargingStation): Connect export const enum OutputFormat { configuration = 'configuration', - ipc = 'ipc', + worker = 'worker', } export const buildEvsesStatus = ( chargingStation: ChargingStation, outputFormat: OutputFormat = OutputFormat.configuration -): EvseStatusConfiguration[] => { +): (EvseStatusWorkerType | EvseStatusConfiguration)[] => { return [...chargingStation.evses.values()].map((evseStatus) => { const connectorsStatus = [...evseStatus.connectors.values()].map( // eslint-disable-next-line @typescript-eslint/no-unused-vars ({ transactionSetInterval, ...connectorStatusRest }) => connectorStatusRest ); - if (outputFormat === OutputFormat.ipc) { - return { - ...evseStatus, - connectors: connectorsStatus, - }; - } else if (outputFormat === OutputFormat.configuration) { - const status = { - ...evseStatus, - connectorsStatus, - }; - delete status.connectors; - return status; + let status: EvseStatusConfiguration; + switch (outputFormat) { + case OutputFormat.worker: + return { + ...evseStatus, + connectors: connectorsStatus, + }; + case OutputFormat.configuration: + status = { + ...evseStatus, + connectorsStatus, + }; + delete (status as EvseStatusWorkerType).connectors; + return status; } }); }; diff --git a/src/utils/MessageChannelUtils.ts b/src/utils/MessageChannelUtils.ts index 06054d5e..3fcdbb20 100644 --- a/src/utils/MessageChannelUtils.ts +++ b/src/utils/MessageChannelUtils.ts @@ -60,7 +60,7 @@ export class MessageChannelUtils { started: chargingStation.started, stationInfo: chargingStation.stationInfo, connectors: buildConnectorsStatus(chargingStation), - evses: buildEvsesStatus(chargingStation, OutputFormat.ipc), + evses: buildEvsesStatus(chargingStation, OutputFormat.worker), ocppConfiguration: chargingStation.ocppConfiguration, wsState: chargingStation?.wsConnection?.readyState, bootNotificationResponse: chargingStation.bootNotificationResponse, -- 2.34.1