Fix OCPP request payloads building
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 14 Jan 2023 00:29:44 +0000 (01:29 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 14 Jan 2023 00:29:44 +0000 (01:29 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/ocpp/1.6/OCPP16RequestService.ts
src/charging-station/ocpp/OCPPRequestService.ts
src/charging-station/ocpp/OCPPServiceUtils.ts

index ce5677a41e8e757c67ed94b727acdf34b54d6a5e..f30bf18952b8a5233a3475cc20da2883756f723e 100644 (file)
@@ -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<OCPP16IncomingRequestService>();
@@ -995,7 +993,7 @@ export default class ChargingStation {
         );
         break;
       default:
-        this.handleUnsupportedVersion(this.getOcppVersion());
+        this.handleUnsupportedVersion(ocppVersion);
         break;
     }
     if (this.stationInfo?.autoRegister === true) {
index 0671af3386336e5025045d3c347f407a5e4d8951..31772e895bae169a105f652927cabd9daeaebe26 100644 (file)
@@ -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,
index f561a44635c16f4088503dbf01961143faca484f..b72c2a8c2da99460627fee6cb9d9fc7de8b87e3a 100644 (file)
@@ -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,
index 06c22367991bf5d6ac83d356b5aeeee7ec6218f2..08618941d3308dfe7602d92df1726c599efb6552 100644 (file)
@@ -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');
     }
   }