fix: fix valid reservation detection at start transaction
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 31 Jul 2023 19:40:30 +0000 (21:40 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 31 Jul 2023 19:40:30 +0000 (21:40 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts

index b6aa7b98137a085eb9f4539b14edd8d50984ea59..5f4f58b4ca86ea224ead2612b1d44c8d5282ab83 100644 (file)
@@ -986,7 +986,7 @@ export class ChargingStation {
         break;
       default:
         // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
-        throw new Error(`Unknown reservation termination reason '${reason}'`);
+        throw new BaseError(`Unknown reservation termination reason '${reason}'`);
     }
   }
 
index 7a9e1a4ec2b2909cc73c5380d7316c4bd2373dba..96ed5a8c363553c10543ed3932863f3d146d95ad 100644 (file)
@@ -6,7 +6,7 @@ import { URL, fileURLToPath } from 'node:url';
 
 import type { JSONSchemaType } from 'ajv';
 import { Client, type FTPResponse } from 'basic-ftp';
-import { secondsToMilliseconds } from 'date-fns';
+import { isWithinInterval, secondsToMilliseconds } from 'date-fns';
 import { create } from 'tar';
 
 import { OCPP16Constants } from './OCPP16Constants';
@@ -678,8 +678,10 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
       .chargingProfiles!) {
       // FIXME: build the composite schedule including the local power limit, the stack level, the charging rate unit, etc.
       if (
-        chargingProfile.chargingSchedule.startSchedule! >= startDate &&
-        chargingProfile.chargingSchedule.startSchedule! <= endDate
+        isWithinInterval(chargingProfile.chargingSchedule.startSchedule!, {
+          start: startDate,
+          end: endDate,
+        })
       ) {
         compositeSchedule = chargingProfile.chargingSchedule;
         break;
@@ -999,7 +1001,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
       }
     }
     logger.warn(
-      `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id:
+      `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id
         ${transactionId.toString()}`,
     );
     return OCPP16Constants.OCPP_RESPONSE_REJECTED;
@@ -1245,7 +1247,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
         if (accessResponse.code === 220) {
           ftpClient.trackProgress((info) => {
             logger.info(
-              `${chargingStation.logPrefix()} ${
+              `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: ${
                 info.bytes / 1024
               } bytes transferred from diagnostics archive ${info.name}`,
             );
index f1a17068bc31ee7761fe9752eaa788de32c844ed..a2cfc53142900ad9cd12d32ed6cb1c8ec4a14f54 100644 (file)
@@ -647,7 +647,7 @@ export class OCPP16ResponseService extends OCPPResponseService {
         )!;
         if (reservation.idTag !== requestPayload.idTag) {
           logger.warn(
-            `${chargingStation.logPrefix()} Transaction reserved ${
+            `${chargingStation.logPrefix()} Reserved transaction ${
               payload.transactionId
             } started with a different idTag ${requestPayload.idTag} than the reservation one ${
               reservation.idTag
@@ -656,7 +656,7 @@ export class OCPP16ResponseService extends OCPPResponseService {
         }
         if (hasReservationExpired(reservation)) {
           logger.warn(
-            `${chargingStation.logPrefix()} Transaction reserved ${
+            `${chargingStation.logPrefix()} Reserved transaction ${
               payload.transactionId
             } started with expired reservation ${
               requestPayload.reservationId
@@ -704,8 +704,19 @@ export class OCPP16ResponseService extends OCPPResponseService {
       );
     } else {
       logger.warn(
-        `${chargingStation.logPrefix()} Starting transaction with id ${payload.transactionId.toString()} REJECTED with status '${payload
-          .idTagInfo?.status}', idTag '${requestPayload.idTag}'`,
+        `${chargingStation.logPrefix()} Starting transaction with id ${payload.transactionId.toString()} REJECTED on ${
+          chargingStation.stationInfo.chargingStationId
+        }#${transactionConnectorId.toString()} with status '${payload.idTagInfo?.status}', idTag '${
+          requestPayload.idTag
+        }'${
+          OCPP16ServiceUtils.hasReservation(
+            chargingStation,
+            transactionConnectorId,
+            requestPayload.idTag,
+          )
+            ? `, reservationId '${requestPayload.reservationId}'`
+            : ''
+        }`,
       );
       await this.resetConnectorOnStartTransactionError(chargingStation, transactionConnectorId);
     }
index b6229b90fb94960e88af423b24ebef1395fb3dc0..6765722c9b28b0276f592b03aacf02236759c44c 100644 (file)
@@ -939,16 +939,17 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
       (chargingStation.getConnectorStatus(connectorId)?.status ===
         OCPP16ChargePointStatus.Reserved &&
         connectorReservation &&
+        !hasReservationExpired(connectorReservation) &&
         // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
-        (hasReservationExpired(connectorReservation) || connectorReservation?.idTag !== idTag)) ||
+        connectorReservation?.idTag === idTag) ||
       (chargingStation.getConnectorStatus(0)?.status === OCPP16ChargePointStatus.Reserved &&
         chargingStationReservation &&
-        (hasReservationExpired(chargingStationReservation) ||
-          chargingStationReservation?.idTag !== idTag))
+        !hasReservationExpired(chargingStationReservation) &&
+        chargingStationReservation?.idTag === idTag)
     ) {
-      return false;
+      return true;
     }
-    return true;
+    return false;
   };
 
   public static parseJsonSchemaFile<T extends JsonType>(