From feff11ec8fa83121c4cd7823eb677d6e3fefa079 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 9 Mar 2023 21:26:21 +0100 Subject: [PATCH] refactor(simulator): remove duplicate OCPP version validation code 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 | 64 ++++++++++++------------- 1 file changed, 30 insertions(+), 34 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 591b4644..ce816715 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -652,18 +652,6 @@ export class ChargingStation { if (params?.terminateOpened) { this.terminateWSConnection(); } - const ocppVersion = this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16; - let protocol: string; - switch (ocppVersion) { - case OCPPVersion.VERSION_16: - case OCPPVersion.VERSION_20: - case OCPPVersion.VERSION_201: - protocol = `ocpp${ocppVersion}`; - break; - default: - this.handleUnsupportedVersion(ocppVersion); - break; - } if (this.isWebSocketConnectionOpened() === true) { logger.warn( @@ -676,7 +664,11 @@ export class ChargingStation { `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}` ); - this.wsConnection = new WebSocket(this.wsConnectionUrl, protocol, options); + this.wsConnection = new WebSocket( + this.wsConnectionUrl, + `ocpp${this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16}`, + options + ); // Handle WebSocket message this.wsConnection.on( @@ -1011,27 +1003,7 @@ export class ChargingStation { // OCPP configuration this.ocppConfiguration = this.getOcppConfiguration(); this.initializeOcppConfiguration(); - const ocppVersion = this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16; - switch (ocppVersion) { - case OCPPVersion.VERSION_16: - this.ocppIncomingRequestService = - OCPP16IncomingRequestService.getInstance(); - this.ocppRequestService = OCPP16RequestService.getInstance( - OCPP16ResponseService.getInstance() - ); - break; - case OCPPVersion.VERSION_20: - case OCPPVersion.VERSION_201: - this.ocppIncomingRequestService = - OCPP20IncomingRequestService.getInstance(); - this.ocppRequestService = OCPP20RequestService.getInstance( - OCPP20ResponseService.getInstance() - ); - break; - default: - this.handleUnsupportedVersion(ocppVersion); - break; - } + this.initializeOcppServices(); if (this.stationInfo?.autoRegister === true) { this.bootNotificationResponse = { currentTime: new Date(), @@ -1059,6 +1031,30 @@ export class ChargingStation { } } + private initializeOcppServices(): void { + const ocppVersion = this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16; + switch (ocppVersion) { + case OCPPVersion.VERSION_16: + this.ocppIncomingRequestService = + OCPP16IncomingRequestService.getInstance(); + this.ocppRequestService = OCPP16RequestService.getInstance( + OCPP16ResponseService.getInstance() + ); + break; + case OCPPVersion.VERSION_20: + case OCPPVersion.VERSION_201: + this.ocppIncomingRequestService = + OCPP20IncomingRequestService.getInstance(); + this.ocppRequestService = OCPP20RequestService.getInstance( + OCPP20ResponseService.getInstance() + ); + break; + default: + this.handleUnsupportedVersion(ocppVersion); + break; + } + } + private initializeOcppConfiguration(): void { if ( !ChargingStationConfigurationUtils.getConfigurationKey( -- 2.34.1