X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStationUtils.ts;h=8ff88a333eb62157456aeeb4916a778698f14445;hb=dd41088e0253c8c25ca4ef74a8b96f9e2b968f10;hp=7865eb1f3d14433226aad8ddd16a8db496360645;hpb=de3272506539665b7ae509b7c056423af1332fb2;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 7865eb1f..8ff88a33 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -4,7 +4,15 @@ import { basename, dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; import chalk from 'chalk'; -import { addSeconds, isAfter, isTomorrow, isYesterday } from 'date-fns'; +import { + addDays, + addSeconds, + addWeeks, + isAfter, + isBefore, + isYesterday, + startOfWeek, +} from 'date-fns'; import type { ChargingStation } from './ChargingStation'; import { BaseError } from '../exception'; @@ -657,10 +665,12 @@ const convertDeprecatedTemplateKey = ( const getLimitFromChargingProfiles = ( chargingProfiles: ChargingProfile[], logPrefix: string, -): { - limit: number; - matchingChargingProfile: ChargingProfile; -} | null => { +): + | { + limit: number; + matchingChargingProfile: ChargingProfile; + } + | undefined => { const debugLogMsg = `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Matching charging profile found for power limitation: %j`; const currentDate = new Date(); for (const chargingProfile of chargingProfiles) { @@ -677,20 +687,19 @@ const getLimitFromChargingProfiles = ( ); chargingSchedule.startSchedule = convertToDate(chargingSchedule.startSchedule)!; } - // Check type (recurring) and if it is already active - // Adjust the daily recurring schedule to today - if ( - chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING && - chargingProfile.recurrencyKind === RecurrencyKindType.DAILY - ) { - if (isYesterday(chargingSchedule.startSchedule)) { - chargingSchedule.startSchedule.setFullYear( - currentDate.getFullYear(), - currentDate.getMonth(), - currentDate.getDate(), - ); - } else if (isTomorrow(chargingSchedule.startSchedule)) { - chargingSchedule.startSchedule.setDate(currentDate.getDate() - 1); + // Adjust recurring start schedule + if (chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING) { + switch (chargingProfile.recurrencyKind) { + case RecurrencyKindType.DAILY: + if (isYesterday(chargingSchedule.startSchedule)) { + addDays(chargingSchedule.startSchedule, 1); + } + break; + case RecurrencyKindType.WEEKLY: + if (isBefore(chargingSchedule.startSchedule, startOfWeek(currentDate))) { + addWeeks(chargingSchedule.startSchedule, 1); + } + break; } } // Check if the charging profile is active @@ -746,7 +755,6 @@ const getLimitFromChargingProfiles = ( } } } - return null; }; const getRandomSerialNumberSuffix = (params?: {