refactor: improve tx changing profile checks
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 3 Aug 2023 16:06:49 +0000 (18:06 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 3 Aug 2023 16:06:49 +0000 (18:06 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
package.json
pnpm-lock.yaml
src/charging-station/Helpers.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts

index 3d334e97aa81172d4bb3a890e62dd7047c4a271c..3e077eb1e2f82f180d1a79be58c19bd5c93f0997 100644 (file)
     "logform": "^2.5.1",
     "mnemonist": "^0.39.5",
     "mongodb": "^5.7.0",
-    "poolifier": "^2.6.20",
+    "poolifier": "^2.6.21",
     "source-map-support": "^0.5.21",
     "tar": "^6.1.15",
     "tslib": "^2.6.1",
index d7db88978846d80ad7d8600e178017b1c0beedd4..bff3a5d6ffa9889ed347a368b96473038f089c9f 100644 (file)
@@ -58,8 +58,8 @@ dependencies:
     specifier: ^5.7.0
     version: 5.7.0
   poolifier:
-    specifier: ^2.6.20
-    version: 2.6.20
+    specifier: ^2.6.21
+    version: 2.6.21
   source-map-support:
     specifier: ^0.5.21
     version: 0.5.21
@@ -7736,8 +7736,8 @@ packages:
     dependencies:
       semver-compare: 1.0.0
 
-  /poolifier@2.6.20:
-    resolution: {integrity: sha512-1s4Ylz09IU6o6NkYMGKmzM885VNpBzAdqPtoTZmpHkWT1Plyeh1L8VtmmS2nFWYapjSQ46H/Ypr6AeLc4mjkmg==}
+  /poolifier@2.6.21:
+    resolution: {integrity: sha512-TOTocb/Ek8UfZBvoJNWlEWSQJUv6In0GCGwMyI8W+WHfaS2ukkxwfYJE+AcVA9ezPfZTDso+AsqLVrs1acTWDA==}
     engines: {node: '>=16.14.0', pnpm: '>=8.6.0'}
     requiresBuild: true
     dev: false
index 0384da3e3026f0f0ce6b006f2b2a640183a7d8a9..7069038b507e436fda96cb87104af3e331deb68f 100644 (file)
@@ -339,11 +339,12 @@ export const initializeConnectorsMapStatus = (
 };
 
 export const resetConnectorStatus = (connectorStatus: ConnectorStatus): void => {
-  connectorStatus.chargingProfiles = isNotEmptyArray(connectorStatus.chargingProfiles)
-    ? connectorStatus.chargingProfiles?.filter(
-        (chargingProfile) => chargingProfile.transactionId !== connectorStatus?.transactionId,
-      )
-    : [];
+  connectorStatus.chargingProfiles =
+    connectorStatus.transactionId && isNotEmptyArray(connectorStatus.chargingProfiles)
+      ? connectorStatus.chargingProfiles?.filter(
+          (chargingProfile) => chargingProfile.transactionId !== connectorStatus.transactionId,
+        )
+      : [];
   connectorStatus.idTagLocalAuthorized = false;
   connectorStatus.idTagAuthorized = false;
   connectorStatus.transactionRemoteStarted = false;
index 0215414862ae253d0a4bfe7455d04f218063ef27..9441f61b424010614b5adfdc6bf67fd90903a4ad 100644 (file)
@@ -626,8 +626,19 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
     }
     if (
       csChargingProfiles.chargingProfilePurpose === OCPP16ChargingProfilePurposeType.TX_PROFILE &&
-      (connectorId === 0 ||
-        chargingStation.getConnectorStatus(connectorId)?.transactionStarted === false)
+      connectorId === 0
+    ) {
+      logger.error(
+        `${chargingStation.logPrefix()} Trying to set transaction charging profile(s)
+          on connector ${connectorId}`,
+      );
+      return OCPP16Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED;
+    }
+    const connectorStatus = chargingStation.getConnectorStatus(connectorId);
+    if (
+      csChargingProfiles.chargingProfilePurpose === OCPP16ChargingProfilePurposeType.TX_PROFILE &&
+      connectorId > 0 &&
+      connectorStatus?.transactionStarted === false
     ) {
       logger.error(
         `${chargingStation.logPrefix()} Trying to set transaction charging profile(s)
@@ -635,6 +646,20 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
       );
       return OCPP16Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED;
     }
+    if (
+      csChargingProfiles.chargingProfilePurpose === OCPP16ChargingProfilePurposeType.TX_PROFILE &&
+      connectorId > 0 &&
+      connectorStatus?.transactionStarted === true &&
+      csChargingProfiles.transactionId !== connectorStatus?.transactionId
+    ) {
+      logger.error(
+        `${chargingStation.logPrefix()} Trying to set transaction charging profile(s)
+          on connector ${connectorId} with a different transaction id ${
+            csChargingProfiles.transactionId
+          } than the started transaction id ${connectorStatus?.transactionId}`,
+      );
+      return OCPP16Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED;
+    }
     OCPP16ServiceUtils.setChargingProfile(chargingStation, connectorId, csChargingProfiles);
     logger.debug(
       `${chargingStation.logPrefix()} Charging profile(s) set on connector id ${connectorId}: %j`,