refactor(simulator): strong type worker data for evses
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 24 May 2023 13:25:23 +0000 (15:25 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 24 May 2023 13:25:23 +0000 (15:25 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/types/ChargingStationWorker.ts
src/types/index.ts
src/utils/ChargingStationConfigurationUtils.ts
src/utils/MessageChannelUtils.ts

index 41ca7aacc870a38e11f6552deed1c446d82b223d..6fe529f91ac45183dd4ffaf147136f1745cddc54 100644 (file)
@@ -20,7 +20,7 @@ export interface ChargingStationWorkerData extends WorkerData {
   chargingStationWorkerOptions?: ChargingStationWorkerOptions;
 }
 
-type EvseStatusType = Omit<EvseStatus, 'connectors'> & {
+export type EvseStatusWorkerType = Omit<EvseStatus, 'connectors'> & {
   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
index 27ffb6d75014fea6a182fc509e713cc4b0c5da54..fee7eab631dc797b16b1156d6cbaa5f8605f9bf1 100644 (file)
@@ -146,6 +146,7 @@ export {
   type ChargingStationWorkerMessage,
   type ChargingStationWorkerMessageData,
   ChargingStationWorkerMessageEvents,
+  type EvseStatusWorkerType,
 } from './ChargingStationWorker';
 export type { ChargingStationInfo } from './ChargingStationInfo';
 export type {
index 3549fa7b634efaafc893def41ef74a68be149444..e61730c75170c958dd0c9644a3a10eb8449099a2 100644 (file)
@@ -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;
     }
   });
 };
index 06054d5e80ef3d2787831c0210b6a9e161ff2d1a..3fcdbb20921f1135db9c1dd0c3f8024f6f90668a 100644 (file)
@@ -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,