From: Jérôme Benoit Date: Thu, 9 Mar 2023 20:26:21 +0000 (+0100) Subject: refactor(simulator): remove duplicate OCPP version validation code X-Git-Tag: v1.1.95~20 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=feff11ec8fa83121c4cd7823eb677d6e3fefa079;p=e-mobility-charging-stations-simulator.git refactor(simulator): remove duplicate OCPP version validation code Signed-off-by: Jérôme Benoit --- 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(