From 225e32b0e38c3b120188f4c93418d4f98f8d1b28 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 31 Jul 2023 14:51:22 +0200 Subject: [PATCH] fix: properly handle changing availability on multiples connectors 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 | 20 ++++------ .../ocpp/1.6/OCPP16ServiceUtils.ts | 40 ++++++++++++------- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 20cedf50..567c3179 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -774,24 +774,20 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { let response: OCPP16ChangeAvailabilityResponse; if (chargingStation.hasEvses) { for (const evseStatus of chargingStation.evses.values()) { - for (const id of evseStatus.connectors.keys()) { - response = await OCPP16ServiceUtils.changeAvailability( - chargingStation, - id, - chargePointStatus, - commandPayload.type, - ); - } - } - } else { - for (const id of chargingStation.connectors.keys()) { response = await OCPP16ServiceUtils.changeAvailability( chargingStation, - id, + [...evseStatus.connectors.keys()], chargePointStatus, commandPayload.type, ); } + } else { + response = await OCPP16ServiceUtils.changeAvailability( + chargingStation, + [...chargingStation.connectors.keys()], + chargePointStatus, + commandPayload.type, + ); } return response!; } else if ( diff --git a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts index e2665090..35da1670 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts @@ -816,25 +816,37 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { public static changeAvailability = async ( chargingStation: ChargingStation, - connectorId: number, + connectorIds: number[], chargePointStatus: OCPP16ChargePointStatus, availabilityType: OCPP16AvailabilityType, ): Promise => { - let response: OCPP16ChangeAvailabilityResponse = - OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED; - const connectorStatus = chargingStation.getConnectorStatus(connectorId)!; - if (connectorStatus?.transactionStarted === true) { - response = OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED; + const responses: OCPP16ChangeAvailabilityResponse[] = []; + for (const connectorId of connectorIds) { + let response: OCPP16ChangeAvailabilityResponse = + OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED; + const connectorStatus = chargingStation.getConnectorStatus(connectorId)!; + if (connectorStatus?.transactionStarted === true) { + response = OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED; + } + connectorStatus.availability = availabilityType; + if (response === OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED) { + await OCPP16ServiceUtils.sendAndSetConnectorStatus( + chargingStation, + connectorId, + chargePointStatus, + ); + } + responses.push(response); } - connectorStatus.availability = availabilityType; - if (response === OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED) { - await OCPP16ServiceUtils.sendAndSetConnectorStatus( - chargingStation, - connectorId, - chargePointStatus, - ); + if ( + responses.includes(OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED) && + !responses.includes(OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_REJECTED) + ) { + return OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED; + } else if (responses.includes(OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_REJECTED)) { + return OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_REJECTED; } - return response; + return OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED; }; public static setChargingProfile( -- 2.34.1