From 86f51b961d470ea555b25bf08664d40705111454 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 3 Aug 2023 18:06:49 +0200 Subject: [PATCH] refactor: improve tx changing profile checks MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- package.json | 2 +- pnpm-lock.yaml | 8 ++--- src/charging-station/Helpers.ts | 11 +++---- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 29 +++++++++++++++++-- 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 3d334e97..3e077eb1 100644 --- a/package.json +++ b/package.json @@ -111,7 +111,7 @@ "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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d7db8897..bff3a5d6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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 diff --git a/src/charging-station/Helpers.ts b/src/charging-station/Helpers.ts index 0384da3e..7069038b 100644 --- a/src/charging-station/Helpers.ts +++ b/src/charging-station/Helpers.ts @@ -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; diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 02154148..9441f61b 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -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`, -- 2.34.1