From ec6dd88b156eac9a14afd0d349ed58bb02fa920e Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 5 Jun 2024 14:09:24 +0200 Subject: [PATCH] fix: fix clear charging profiles with no connector in payload MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 7c64c2ad..6ec517c0 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -1053,41 +1053,46 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { return OCPP16Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN } const { connectorId } = commandPayload - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - if (!chargingStation.hasConnector(connectorId!)) { - logger.error( - `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector id ${connectorId}` - ) - return OCPP16Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN - } - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const connectorStatus = chargingStation.getConnectorStatus(connectorId!) - if (connectorId != null && isNotEmptyArray(connectorStatus?.chargingProfiles)) { - connectorStatus.chargingProfiles = [] - logger.debug( - `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${connectorId}` - ) - return OCPP16Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED - } - if (connectorId == null) { + if (connectorId != null) { + if (!chargingStation.hasConnector(connectorId)) { + logger.error( + `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector id ${connectorId}` + ) + return OCPP16Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN + } + const connectorStatus = chargingStation.getConnectorStatus(connectorId) + if (isNotEmptyArray(connectorStatus?.chargingProfiles)) { + connectorStatus.chargingProfiles = [] + logger.debug( + `${chargingStation.logPrefix()} Charging profile(s) cleared on connector id ${connectorId}` + ) + return OCPP16Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED + } + } else { let clearedCP = false if (chargingStation.hasEvses) { for (const evseStatus of chargingStation.evses.values()) { for (const status of evseStatus.connectors.values()) { - clearedCP = OCPP16ServiceUtils.clearChargingProfiles( + const clearedConnectorCP = OCPP16ServiceUtils.clearChargingProfiles( chargingStation, commandPayload, status.chargingProfiles ) + if (clearedConnectorCP && !clearedCP) { + clearedCP = true + } } } } else { for (const id of chargingStation.connectors.keys()) { - clearedCP = OCPP16ServiceUtils.clearChargingProfiles( + const clearedConnectorCP = OCPP16ServiceUtils.clearChargingProfiles( chargingStation, commandPayload, chargingStation.getConnectorStatus(id)?.chargingProfiles ) + if (clearedConnectorCP && !clearedCP) { + clearedCP = true + } } } if (clearedCP) { -- 2.34.1