X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStationUtils.ts;h=1af1266110bd62d2e49f42e5ec801cc34fac8546;hb=80c580411036965e34b983809b663b812ed36997;hp=709d1dd11747f4106f5bac1ec9bc0531c6e0fa19;hpb=975e18ec2b21e99f182dc0df1e7fb0fe1454665c;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 709d1dd1..1af12661 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -49,6 +49,7 @@ import { cloneObject, convertToDate, convertToInt, + isArraySorted, isEmptyObject, isEmptyString, isNotEmptyArray, @@ -739,7 +740,21 @@ const getLimitFromChargingProfiles = ( }) ) { if (isNotEmptyArray(chargingSchedule.chargingSchedulePeriod)) { - chargingSchedule.chargingSchedulePeriod.sort((a, b) => a.startPeriod - b.startPeriod); + const chargingSchedulePeriodCompareFn = ( + a: ChargingSchedulePeriod, + b: ChargingSchedulePeriod, + ) => a.startPeriod - b.startPeriod; + if ( + isArraySorted( + chargingSchedule.chargingSchedulePeriod, + chargingSchedulePeriodCompareFn, + ) === false + ) { + logger.warn( + `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${chargingProfile.chargingProfileId} schedule periods are not sorted by start period`, + ); + chargingSchedule.chargingSchedulePeriod.sort(chargingSchedulePeriodCompareFn); + } // Check if the first schedule period start period is equal to 0 if (chargingSchedule.chargingSchedulePeriod[0].startPeriod !== 0) { logger.error( @@ -782,11 +797,11 @@ const getLimitFromChargingProfiles = ( (index < chargingSchedule.chargingSchedulePeriod.length - 1 && chargingSchedule.duration! > differenceInSeconds( - chargingSchedule.startSchedule!, addSeconds( chargingSchedule.startSchedule!, chargingSchedule.chargingSchedulePeriod[index + 1].startPeriod, ), + chargingSchedule.startSchedule!, )) ) { const result: ChargingProfilesLimit = { @@ -829,7 +844,7 @@ const prepareRecurringChargingProfile = ( ) { chargingSchedule.startSchedule = addDays( chargingSchedule.startSchedule!, - differenceInDays(chargingSchedule.startSchedule!, recurringInterval.end), + differenceInDays(currentDate, recurringInterval.start), ); recurringInterval = { start: chargingSchedule.startSchedule, @@ -849,7 +864,7 @@ const prepareRecurringChargingProfile = ( ) { chargingSchedule.startSchedule = addWeeks( chargingSchedule.startSchedule!, - differenceInWeeks(chargingSchedule.startSchedule!, recurringInterval.end), + differenceInWeeks(currentDate, recurringInterval.start), ); recurringInterval = { start: chargingSchedule.startSchedule,