fix: properly handle changing availability on multiples connectors
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 31 Jul 2023 12:51:22 +0000 (14:51 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 31 Jul 2023 12:51:22 +0000 (14:51 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts

index 20cedf507ab556b110504b6202c670ed7551f5e5..567c317931d927421f05cbe10e1470e370da3a63 100644 (file)
@@ -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 (
index e2665090ed3e44dbf6841f1519520adec91f2a4c..35da1670922aa1ee60212334e7f90fb562eec9a6 100644 (file)
@@ -816,25 +816,37 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
 
   public static changeAvailability = async (
     chargingStation: ChargingStation,
-    connectorId: number,
+    connectorIds: number[],
     chargePointStatus: OCPP16ChargePointStatus,
     availabilityType: OCPP16AvailabilityType,
   ): Promise<OCPP16ChangeAvailabilityResponse> => {
-    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(