From c8faabc815e314d10188b9c285c61e1e4c367f8c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 28 May 2023 23:01:28 +0200 Subject: [PATCH] refactor(simulator): convert more class static helpers to arrow function MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 18 ++-- .../ocpp/1.6/OCPP16ResponseService.ts | 6 +- src/performance/PerformanceStatistics.ts | 6 +- src/utils/MessageChannelUtils.ts | 102 ++++++++---------- src/utils/index.ts | 7 +- 5 files changed, 68 insertions(+), 71 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 28138afb..9a8f468b 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -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, diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 1999473d..117c3686 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -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 { 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 '${ diff --git a/src/performance/PerformanceStatistics.ts b/src/performance/PerformanceStatistics.ts index 61ef1b01..cd0ff3df 100644 --- a/src/performance/PerformanceStatistics.ts +++ b/src/performance/PerformanceStatistics.ts @@ -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)); } } diff --git a/src/utils/MessageChannelUtils.ts b/src/utils/MessageChannelUtils.ts index 3fcdbb20..2bb43a96 100644 --- a/src/utils/MessageChannelUtils.ts +++ b/src/utils/MessageChannelUtils.ts @@ -12,62 +12,54 @@ import { type Statistics, } from '../types'; -export class MessageChannelUtils { - private constructor() { - // This is intentional - } +export const buildStartedMessage = ( + chargingStation: ChargingStation +): ChargingStationWorkerMessage => { + return { + id: ChargingStationWorkerMessageEvents.started, + data: buildChargingStationDataPayload(chargingStation), + }; +}; - public static buildStartedMessage( - chargingStation: ChargingStation - ): ChargingStationWorkerMessage { - return { - id: ChargingStationWorkerMessageEvents.started, - data: MessageChannelUtils.buildChargingStationDataPayload(chargingStation), - }; - } +export const buildStoppedMessage = ( + chargingStation: ChargingStation +): ChargingStationWorkerMessage => { + return { + id: ChargingStationWorkerMessageEvents.stopped, + data: buildChargingStationDataPayload(chargingStation), + }; +}; - public static buildStoppedMessage( - chargingStation: ChargingStation - ): ChargingStationWorkerMessage { - return { - id: ChargingStationWorkerMessageEvents.stopped, - data: MessageChannelUtils.buildChargingStationDataPayload(chargingStation), - }; - } +export const buildUpdatedMessage = ( + chargingStation: ChargingStation +): ChargingStationWorkerMessage => { + return { + id: ChargingStationWorkerMessageEvents.updated, + data: buildChargingStationDataPayload(chargingStation), + }; +}; - public static buildUpdatedMessage( - chargingStation: ChargingStation - ): ChargingStationWorkerMessage { - return { - id: ChargingStationWorkerMessageEvents.updated, - data: MessageChannelUtils.buildChargingStationDataPayload(chargingStation), - }; - } +export const buildPerformanceStatisticsMessage = ( + statistics: Statistics +): ChargingStationWorkerMessage => { + return { + id: ChargingStationWorkerMessageEvents.performanceStatistics, + data: statistics, + }; +}; - public static buildPerformanceStatisticsMessage( - statistics: Statistics - ): ChargingStationWorkerMessage { - 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), + }), + }; +}; diff --git a/src/utils/index.ts b/src/utils/index.ts index 2273562e..2506da85 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -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'; -- 2.34.1