fix: avoid endless loop at remote start transaction
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 6 Jun 2024 21:42:27 +0000 (23:42 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 6 Jun 2024 21:42:27 +0000 (23:42 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts

index fe25221050a8afa02e2f4b0091217c5726116b2b..017de863c678f8c66d9c66d83085dfa35226156b 100644 (file)
@@ -1162,17 +1162,27 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
     commandPayload: RemoteStartTransactionRequest
   ): Promise<GenericResponse> {
     if (commandPayload.connectorId == null) {
-      do {
-        commandPayload.connectorId = randomInt(1, chargingStation.getNumberOfConnectors())
-      } while (
-        chargingStation.getConnectorStatus(commandPayload.connectorId)?.transactionStarted ===
-          true &&
-        OCPP16ServiceUtils.hasReservation(
-          chargingStation,
-          commandPayload.connectorId,
-          commandPayload.idTag
+      for (
+        let connectorId = 1;
+        connectorId <= chargingStation.getNumberOfConnectors();
+        connectorId++
+      ) {
+        if (
+          chargingStation.getConnectorStatus(connectorId)?.transactionStarted === false &&
+          !OCPP16ServiceUtils.hasReservation(chargingStation, connectorId, commandPayload.idTag)
+        ) {
+          commandPayload.connectorId = connectorId
+          break
+        }
+      }
+      if (commandPayload.connectorId == null) {
+        logger.debug(
+          `${chargingStation.logPrefix()} Remote start transaction REJECTED on ${
+            chargingStation.stationInfo?.chargingStationId
+          }, idTag '${commandPayload.idTag}': no available connector found`
         )
-      )
+        return OCPP16Constants.OCPP_RESPONSE_REJECTED
+      }
     }
     const { connectorId: transactionConnectorId, idTag, chargingProfile } = commandPayload
     if (!chargingStation.hasConnector(transactionConnectorId)) {