fix: ensure composite schedule period start at 0
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 4 Aug 2023 13:05:08 +0000 (15:05 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 4 Aug 2023 13:05:08 +0000 (15:05 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts

index 537449dfbcc55a3149f5c25792ac3982b1640c3c..914886bd4d680ba15b9095dc5cc80200266341a7 100644 (file)
@@ -1086,7 +1086,6 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
                 },
               )
             ) {
-              schedulePeriod.startPeriod = 0;
               return false;
             }
             if (
@@ -1106,7 +1105,10 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
             }
             return true;
           })
-          .map((schedulePeriod) => {
+          .map((schedulePeriod, index) => {
+            if (index === 0 && schedulePeriod.startPeriod !== 0) {
+              schedulePeriod.startPeriod = 0;
+            }
             return {
               ...schedulePeriod,
               startPeriod: higherFirst
@@ -1178,8 +1180,8 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
           ...chargingSchedule,
           startSchedule: targetInterval.start as Date,
           duration: differenceInSeconds(chargingScheduleInterval.end, targetInterval.start as Date),
-          chargingSchedulePeriod: chargingSchedule.chargingSchedulePeriod.filter(
-            (schedulePeriod, index) => {
+          chargingSchedulePeriod: chargingSchedule.chargingSchedulePeriod
+            .filter((schedulePeriod, index) => {
               if (
                 isWithinInterval(
                   addSeconds(chargingScheduleInterval.start, schedulePeriod.startPeriod)!,
@@ -1202,12 +1204,16 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
                   targetInterval,
                 )
               ) {
-                schedulePeriod.startPeriod = 0;
                 return true;
               }
               return false;
-            },
-          ),
+            })
+            .map((schedulePeriod, index) => {
+              if (index === 0 && schedulePeriod.startPeriod !== 0) {
+                schedulePeriod.startPeriod = 0;
+              }
+              return schedulePeriod;
+            }),
         };
       }
       if (isAfter(chargingScheduleInterval.end, targetInterval.end)) {