X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStationUtils.ts;h=0d349d22518def767afb3d535ba0fb5abbd96525;hb=d476bc1be87bb63cfd8c494ce1ed41204992806f;hp=948695744b303e04f5564b83abedb0fe7b95ff8c;hpb=76dab5a927a69adf1a540ff597a2a2cfa8cdaf7f;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 94869574..0d349d22 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -9,14 +9,12 @@ import { addSeconds, addWeeks, differenceInDays, + differenceInSeconds, differenceInWeeks, - endOfDay, - endOfWeek, isAfter, isBefore, isWithinInterval, - startOfDay, - startOfWeek, + toDate, } from 'date-fns'; import type { ChargingStation } from './ChargingStation'; @@ -727,7 +725,6 @@ const getLimitFromChargingProfiles = ( ); continue; } - // Adjust recurring start schedule if (chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING) { prepareRecurringChargingProfile(chargingProfile, currentDate, logPrefix); } else if ( @@ -794,54 +791,94 @@ const getLimitFromChargingProfiles = ( } }; +/** + * Adjust recurring charging profile startSchedule to the current recurrency time interval if needed + * + * @param chargingProfile - + * @param currentDate - + * @param logPrefix - + */ const prepareRecurringChargingProfile = ( chargingProfile: ChargingProfile, currentDate: Date, logPrefix: string, ) => { const chargingSchedule = chargingProfile.chargingSchedule; + let recurringInterval: Interval; switch (chargingProfile.recurrencyKind) { case RecurrencyKindType.DAILY: - if (isBefore(chargingSchedule.startSchedule!, startOfDay(currentDate))) { - addDays( + recurringInterval = { + start: chargingSchedule.startSchedule!, + end: addDays(chargingSchedule.startSchedule!, 1), + }; + checkRecurringChargingProfileDuration(chargingProfile, recurringInterval, logPrefix); + if ( + !isWithinInterval(currentDate, recurringInterval) && + isBefore(chargingSchedule.startSchedule!, currentDate) + ) { + chargingSchedule.startSchedule = addDays( chargingSchedule.startSchedule!, - differenceInDays(chargingSchedule.startSchedule!, endOfDay(currentDate)), + differenceInDays(chargingSchedule.startSchedule!, recurringInterval.end), ); - if ( - isBefore(chargingSchedule.startSchedule!, startOfDay(currentDate)) || - isAfter(chargingSchedule.startSchedule!, endOfDay(currentDate)) - ) { - logger.error( - `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring ${ - chargingProfile.recurrencyKind - } charging profile id ${ - chargingProfile.chargingProfileId - } startSchedule ${chargingSchedule.startSchedule!.toISOString()} is not properly translated to the current day`, - ); - } + recurringInterval = { + start: chargingSchedule.startSchedule, + end: addDays(chargingSchedule.startSchedule, 1), + }; } break; case RecurrencyKindType.WEEKLY: - if (isBefore(chargingSchedule.startSchedule!, startOfWeek(currentDate))) { - addWeeks( + recurringInterval = { + start: chargingSchedule.startSchedule!, + end: addWeeks(chargingSchedule.startSchedule!, 1), + }; + checkRecurringChargingProfileDuration(chargingProfile, recurringInterval, logPrefix); + if ( + !isWithinInterval(currentDate, recurringInterval) && + isBefore(chargingSchedule.startSchedule!, currentDate) + ) { + chargingSchedule.startSchedule = addWeeks( chargingSchedule.startSchedule!, - differenceInWeeks(chargingSchedule.startSchedule!, endOfWeek(currentDate)), + differenceInWeeks(chargingSchedule.startSchedule!, recurringInterval.end), ); - if ( - isBefore(chargingSchedule.startSchedule!, startOfWeek(currentDate)) || - isAfter(chargingSchedule.startSchedule!, endOfWeek(currentDate)) - ) { - logger.error( - `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring ${ - chargingProfile.recurrencyKind - } charging profile id ${ - chargingProfile.chargingProfileId - } startSchedule ${chargingSchedule.startSchedule!.toISOString()} is not properly translated to the current week`, - ); - } + recurringInterval = { + start: chargingSchedule.startSchedule, + end: addWeeks(chargingSchedule.startSchedule, 1), + }; } break; } + if (!isWithinInterval(currentDate, recurringInterval!)) { + logger.error( + `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring ${ + chargingProfile.recurrencyKind + } charging profile id ${ + chargingProfile.chargingProfileId + } startSchedule ${chargingSchedule.startSchedule!.toISOString()} is not properly translated to current recurrency time interval [${toDate( + recurringInterval!.start, + ).toISOString()}, ${toDate(recurringInterval!.end).toISOString()}]`, + ); + } +}; + +const checkRecurringChargingProfileDuration = ( + chargingProfile: ChargingProfile, + interval: Interval, + logPrefix: string, +) => { + if ( + chargingProfile.chargingSchedule.duration! > differenceInSeconds(interval.end, interval.start) + ) { + logger.warn( + `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring ${ + chargingProfile.chargingProfileKind + } charging profile id ${chargingProfile.chargingProfileId} duration ${ + chargingProfile.chargingSchedule.duration + } is greater than the recurrency time interval ${differenceInSeconds( + interval.end, + interval.start, + )} duration`, + ); + } }; const getRandomSerialNumberSuffix = (params?: {