refactor(simulator): convert more class static helpers to arrow function
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 28 May 2023 21:01:28 +0000 (23:01 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 28 May 2023 21:01:28 +0000 (23:01 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/performance/PerformanceStatistics.ts
src/utils/MessageChannelUtils.ts
src/utils/index.ts

index 28138afb0b3764514ee374235edf5b89cafca01c..9a8f468b37bee455378b890a2392a4e02a9dfefb 100644 (file)
@@ -89,11 +89,13 @@ import {
   Configuration,
   Constants,
   DCElectricUtils,
-  MessageChannelUtils,
   Utils,
   buildChargingStationAutomaticTransactionGeneratorConfiguration,
   buildConnectorsStatus,
   buildEvsesStatus,
+  buildStartedMessage,
+  buildStoppedMessage,
+  buildUpdatedMessage,
   handleFileException,
   logger,
   watchJsonFile,
@@ -682,7 +684,7 @@ export class ChargingStation {
           }
         );
         this.started = true;
-        parentPort?.postMessage(MessageChannelUtils.buildStartedMessage(this));
+        parentPort?.postMessage(buildStartedMessage(this));
         this.starting = false;
       } else {
         logger.warn(`${this.logPrefix()} Charging station is already starting...`);
@@ -707,7 +709,7 @@ export class ChargingStation {
         delete this.bootNotificationResponse;
         this.started = false;
         this.saveConfiguration();
-        parentPort?.postMessage(MessageChannelUtils.buildStoppedMessage(this));
+        parentPort?.postMessage(buildStoppedMessage(this));
         this.stopping = false;
       } else {
         logger.warn(`${this.logPrefix()} Charging station is already stopping...`);
@@ -854,7 +856,7 @@ export class ChargingStation {
       this.automaticTransactionGenerator?.start();
     }
     this.saveAutomaticTransactionGeneratorConfiguration();
-    parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
+    parentPort?.postMessage(buildUpdatedMessage(this));
   }
 
   public stopAutomaticTransactionGenerator(connectorIds?: number[]): void {
@@ -866,7 +868,7 @@ export class ChargingStation {
       this.automaticTransactionGenerator?.stop();
     }
     this.saveAutomaticTransactionGeneratorConfiguration();
-    parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
+    parentPort?.postMessage(buildUpdatedMessage(this));
   }
 
   public async stopTransactionOnConnector(
@@ -1721,7 +1723,7 @@ export class ChargingStation {
       }
       this.wsConnectionRestarted = false;
       this.autoReconnectRetryCount = 0;
-      parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
+      parentPort?.postMessage(buildUpdatedMessage(this));
     } else {
       logger.warn(
         `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`
@@ -1751,7 +1753,7 @@ export class ChargingStation {
         this.started === true && (await this.reconnect());
         break;
     }
-    parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
+    parentPort?.postMessage(buildUpdatedMessage(this));
   }
 
   private getCachedRequest(messageType: MessageType, messageId: string): CachedRequest | undefined {
@@ -1861,7 +1863,7 @@ export class ChargingStation {
             logger.error(`${this.logPrefix()} ${errorMsg}`);
             throw new OCPPError(ErrorType.PROTOCOL_ERROR, errorMsg);
         }
-        parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
+        parentPort?.postMessage(buildUpdatedMessage(this));
       } else {
         throw new OCPPError(ErrorType.PROTOCOL_ERROR, 'Incoming message is not an array', null, {
           request,
index 1999473d74690e03fce39af5a261d44b450c5930..117c36863beefe8184a2bec5e94c7b3d97fbd494 100644 (file)
@@ -49,7 +49,7 @@ import {
   type SetChargingProfileResponse,
   type UnlockConnectorResponse,
 } from '../../../types';
-import { Constants, MessageChannelUtils, Utils, logger } from '../../../utils';
+import { Constants, Utils, buildUpdatedMessage, logger } from '../../../utils';
 import { OCPPResponseService } from '../OCPPResponseService';
 
 const moduleName = 'OCPP16ResponseService';
@@ -641,7 +641,7 @@ export class OCPP16ResponseService extends OCPPResponseService {
   ): Promise<void> {
     ChargingStationUtils.resetConnectorStatus(chargingStation.getConnectorStatus(connectorId));
     chargingStation.stopMeterValues(connectorId);
-    parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(chargingStation));
+    parentPort?.postMessage(buildUpdatedMessage(chargingStation));
     if (
       chargingStation.getConnectorStatus(connectorId)?.status !== OCPP16ChargePointStatus.Available
     ) {
@@ -707,7 +707,7 @@ export class OCPP16ResponseService extends OCPPResponseService {
       chargingStation.getConnectorStatus(transactionConnectorId)
     );
     chargingStation.stopMeterValues(transactionConnectorId);
-    parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(chargingStation));
+    parentPort?.postMessage(buildUpdatedMessage(chargingStation));
     const logMsg = `${chargingStation.logPrefix()} Transaction with id ${requestPayload.transactionId.toString()} STOPPED on ${
       chargingStation.stationInfo.chargingStationId
     }#${transactionConnectorId?.toString()} with status '${
index 61ef1b012946319ac1080fac1d9ca89b672fefd9..cd0ff3dff6592e1709cc1655f253efe56fec1eb0 100644 (file)
@@ -15,8 +15,8 @@ import {
   CircularArray,
   Configuration,
   Constants,
-  MessageChannelUtils,
   Utils,
+  buildPerformanceStatisticsMessage,
   logger,
 } from '../utils';
 
@@ -249,9 +249,7 @@ export class PerformanceStatistics {
       )
     );
     if (Configuration.getPerformanceStorage().enabled) {
-      parentPort?.postMessage(
-        MessageChannelUtils.buildPerformanceStatisticsMessage(this.statistics)
-      );
+      parentPort?.postMessage(buildPerformanceStatisticsMessage(this.statistics));
     }
   }
 
index 3fcdbb20921f1135db9c1dd0c3f8024f6f90668a..2bb43a961ccfa11686896321a52c02b5b0f74eb9 100644 (file)
@@ -12,62 +12,54 @@ import {
   type Statistics,
 } from '../types';
 
-export class MessageChannelUtils {
-  private constructor() {
-    // This is intentional
-  }
+export const buildStartedMessage = (
+  chargingStation: ChargingStation
+): ChargingStationWorkerMessage<ChargingStationData> => {
+  return {
+    id: ChargingStationWorkerMessageEvents.started,
+    data: buildChargingStationDataPayload(chargingStation),
+  };
+};
 
-  public static buildStartedMessage(
-    chargingStation: ChargingStation
-  ): ChargingStationWorkerMessage<ChargingStationData> {
-    return {
-      id: ChargingStationWorkerMessageEvents.started,
-      data: MessageChannelUtils.buildChargingStationDataPayload(chargingStation),
-    };
-  }
+export const buildStoppedMessage = (
+  chargingStation: ChargingStation
+): ChargingStationWorkerMessage<ChargingStationData> => {
+  return {
+    id: ChargingStationWorkerMessageEvents.stopped,
+    data: buildChargingStationDataPayload(chargingStation),
+  };
+};
 
-  public static buildStoppedMessage(
-    chargingStation: ChargingStation
-  ): ChargingStationWorkerMessage<ChargingStationData> {
-    return {
-      id: ChargingStationWorkerMessageEvents.stopped,
-      data: MessageChannelUtils.buildChargingStationDataPayload(chargingStation),
-    };
-  }
+export const buildUpdatedMessage = (
+  chargingStation: ChargingStation
+): ChargingStationWorkerMessage<ChargingStationData> => {
+  return {
+    id: ChargingStationWorkerMessageEvents.updated,
+    data: buildChargingStationDataPayload(chargingStation),
+  };
+};
 
-  public static buildUpdatedMessage(
-    chargingStation: ChargingStation
-  ): ChargingStationWorkerMessage<ChargingStationData> {
-    return {
-      id: ChargingStationWorkerMessageEvents.updated,
-      data: MessageChannelUtils.buildChargingStationDataPayload(chargingStation),
-    };
-  }
+export const buildPerformanceStatisticsMessage = (
+  statistics: Statistics
+): ChargingStationWorkerMessage<Statistics> => {
+  return {
+    id: ChargingStationWorkerMessageEvents.performanceStatistics,
+    data: statistics,
+  };
+};
 
-  public static buildPerformanceStatisticsMessage(
-    statistics: Statistics
-  ): ChargingStationWorkerMessage<Statistics> {
-    return {
-      id: ChargingStationWorkerMessageEvents.performanceStatistics,
-      data: statistics,
-    };
-  }
-
-  private static buildChargingStationDataPayload(
-    chargingStation: ChargingStation
-  ): ChargingStationData {
-    return {
-      started: chargingStation.started,
-      stationInfo: chargingStation.stationInfo,
-      connectors: buildConnectorsStatus(chargingStation),
-      evses: buildEvsesStatus(chargingStation, OutputFormat.worker),
-      ocppConfiguration: chargingStation.ocppConfiguration,
-      wsState: chargingStation?.wsConnection?.readyState,
-      bootNotificationResponse: chargingStation.bootNotificationResponse,
-      ...(chargingStation.automaticTransactionGenerator && {
-        automaticTransactionGenerator:
-          buildChargingStationAutomaticTransactionGeneratorConfiguration(chargingStation),
-      }),
-    };
-  }
-}
+const buildChargingStationDataPayload = (chargingStation: ChargingStation): ChargingStationData => {
+  return {
+    started: chargingStation.started,
+    stationInfo: chargingStation.stationInfo,
+    connectors: buildConnectorsStatus(chargingStation),
+    evses: buildEvsesStatus(chargingStation, OutputFormat.worker),
+    ocppConfiguration: chargingStation.ocppConfiguration,
+    wsState: chargingStation?.wsConnection?.readyState,
+    bootNotificationResponse: chargingStation.bootNotificationResponse,
+    ...(chargingStation.automaticTransactionGenerator && {
+      automaticTransactionGenerator:
+        buildChargingStationAutomaticTransactionGeneratorConfiguration(chargingStation),
+    }),
+  };
+};
index 2273562e6589c53ceef77da8686bcb3095b9b7f5..2506da85375da705a69eb0eb516b22ec71fbaa8d 100644 (file)
@@ -17,6 +17,11 @@ export {
   setDefaultErrorParams,
 } from './ErrorUtils';
 export { watchJsonFile } from './FileUtils';
-export { MessageChannelUtils } from './MessageChannelUtils';
+export {
+  buildPerformanceStatisticsMessage,
+  buildUpdatedMessage,
+  buildStartedMessage,
+  buildStoppedMessage,
+} from './MessageChannelUtils';
 export { Utils } from './Utils';
 export { logger } from './Logger';