fix: ensure recurring profile duration is valid
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 25 Jul 2023 16:32:41 +0000 (18:32 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 25 Jul 2023 16:32:41 +0000 (18:32 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStationUtils.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts

index fa7263e0e0c2f2d6e50e55c3c33d7e65eef04725..b26f9970bfa3be2e2b8d72ef226e3c799e761c38 100644 (file)
@@ -736,7 +736,14 @@ const getLimitFromChargingProfiles = (
       isAfter(addSeconds(chargingSchedule.startSchedule!, chargingSchedule.duration!), currentDate)
     ) {
       if (isNotEmptyArray(chargingSchedule.chargingSchedulePeriod)) {
-        // Handling of only one schedule period
+        chargingSchedule.chargingSchedulePeriod.sort((a, b) => a.startPeriod - b.startPeriod);
+        if (chargingSchedule.chargingSchedulePeriod[0].startPeriod !== 0) {
+          logger.error(
+            `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${chargingProfile.chargingProfileId} first schedule period start period ${chargingSchedule.chargingSchedulePeriod[0].startPeriod} is not equal to 0`,
+          );
+          // continue;
+        }
+        // Handling of only one schedule period with startPeriod = 0
         if (
           chargingSchedule.chargingSchedulePeriod.length === 1 &&
           chargingSchedule.chargingSchedulePeriod[0].startPeriod === 0
@@ -748,7 +755,6 @@ const getLimitFromChargingProfiles = (
           logger.debug(debugLogMsg, result);
           return result;
         }
-        chargingSchedule.chargingSchedulePeriod.sort((a, b) => a.startPeriod - b.startPeriod);
         let lastButOneSchedule: ChargingSchedulePeriod | undefined;
         // Search for the right schedule period
         for (const schedulePeriod of chargingSchedule.chargingSchedulePeriod) {
@@ -876,6 +882,7 @@ const checkRecurringChargingProfileDuration = (
         interval.start,
       )}`,
     );
+    chargingProfile.chargingSchedule.duration = differenceInSeconds(interval.end, interval.start);
   }
 };
 
index 82647835535d725a5bd89f545c17169790d32b14..c7337ab69033c6ce1aa7727642fd3b87f63d651d 100644 (file)
@@ -979,20 +979,26 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
   private setRemoteStartTransactionChargingProfile(
     chargingStation: ChargingStation,
     connectorId: number,
-    cp: OCPP16ChargingProfile,
+    chargingProfile: OCPP16ChargingProfile,
   ): boolean {
-    if (cp && cp.chargingProfilePurpose === OCPP16ChargingProfilePurposeType.TX_PROFILE) {
-      OCPP16ServiceUtils.setChargingProfile(chargingStation, connectorId, cp);
+    if (
+      chargingProfile &&
+      chargingProfile.chargingProfilePurpose === OCPP16ChargingProfilePurposeType.TX_PROFILE
+    ) {
+      OCPP16ServiceUtils.setChargingProfile(chargingStation, connectorId, chargingProfile);
       logger.debug(
         `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction
           on connector id ${connectorId}: %j`,
-        cp,
+        chargingProfile,
       );
       return true;
-    } else if (cp && cp.chargingProfilePurpose !== OCPP16ChargingProfilePurposeType.TX_PROFILE) {
+    } else if (
+      chargingProfile &&
+      chargingProfile.chargingProfilePurpose !== OCPP16ChargingProfilePurposeType.TX_PROFILE
+    ) {
       logger.warn(
         `${chargingStation.logPrefix()} Not allowed to set ${
-          cp.chargingProfilePurpose
+          chargingProfile.chargingProfilePurpose
         } charging profile(s) at remote start transaction`,
       );
       return false;