From: Jérôme Benoit Date: Sat, 14 Jan 2023 00:29:44 +0000 (+0100) Subject: Fix OCPP request payloads building X-Git-Tag: v1.1.90~14 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=cda962601b73f2de23fa089ee102160dd055af2e;p=e-mobility-charging-stations-simulator.git Fix OCPP request payloads building Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index ce5677a4..f30bf189 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -632,7 +632,7 @@ export default class ChargingStation { if (params?.terminateOpened) { this.terminateWSConnection(); } - const ocppVersion = this.getOcppVersion(); + const ocppVersion = this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16; let protocol: string; switch (ocppVersion) { case OCPPVersion.VERSION_16: @@ -834,6 +834,7 @@ export default class ChargingStation { this.index, stationTemplate ); + stationInfo.ocppVersion = stationTemplate.ocppVersion ?? OCPPVersion.VERSION_16; ChargingStationUtils.createSerialNumber(stationTemplate, stationInfo); if (!Utils.isEmptyArray(stationTemplate.power)) { stationTemplate.power = stationTemplate.power as number[]; @@ -936,10 +937,6 @@ export default class ChargingStation { } } - private getOcppVersion(): OCPPVersion { - return this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16; - } - private getOcppPersistentConfiguration(): boolean { return this.stationInfo?.ocppPersistentConfiguration ?? true; } @@ -978,7 +975,8 @@ export default class ChargingStation { // OCPP configuration this.ocppConfiguration = this.getOcppConfiguration(); this.initializeOcppConfiguration(); - switch (this.getOcppVersion()) { + const ocppVersion = this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16; + switch (ocppVersion) { case OCPPVersion.VERSION_16: this.ocppIncomingRequestService = OCPP16IncomingRequestService.getInstance(); @@ -995,7 +993,7 @@ export default class ChargingStation { ); break; default: - this.handleUnsupportedVersion(this.getOcppVersion()); + this.handleUnsupportedVersion(ocppVersion); break; } if (this.stationInfo?.autoRegister === true) { diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index 0671af33..31772e89 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -223,11 +223,11 @@ export default class OCPP16RequestService extends OCPPRequestService { } as unknown as Request; case OCPP16RequestCommand.STOP_TRANSACTION: chargingStation.getTransactionDataMeterValues() && - Utils.isUndefined(commandParams?.transactionData) && + Utils.isNullOrUndefined(commandParams?.transactionData) && (connectorId = chargingStation.getConnectorIdByTransactionId( commandParams?.transactionId as number )); - !commandParams?.meterStop && + Utils.isNullOrUndefined(commandParams?.meterStop) && (energyActiveImportRegister = chargingStation.getEnergyActiveImportRegisterByTransactionId( commandParams?.transactionId as number, diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index f561a446..b72c2a8c 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -382,13 +382,13 @@ export default abstract class OCPPRequestService { // Request case MessageType.CALL_MESSAGE: // Build request + this.validateRequestPayload(chargingStation, commandName, messagePayload as JsonObject); chargingStation.requests.set(messageId, [ responseCallback, errorCallback, commandName, messagePayload as JsonType, ]); - this.validateRequestPayload(chargingStation, commandName, messagePayload as JsonObject); messageToSend = JSON.stringify([ messageType, messageId, diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index 06c22367..08618941 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -130,7 +130,7 @@ export class OCPPServiceUtils { connectorId: number, status: ConnectorStatusEnum ): StatusNotificationRequest { - switch (chargingStation.stationInfo.ocppVersion) { + switch (chargingStation.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16) { case OCPPVersion.VERSION_16: return { connectorId, @@ -145,6 +145,8 @@ export class OCPPServiceUtils { connectorId, evseId: connectorId, } as OCPP20StatusNotificationRequest; + default: + throw new BaseError('Cannot build status notification payload: OCPP version not supported'); } }