From: Jérôme Benoit Date: Tue, 25 Jul 2023 11:11:48 +0000 (+0200) Subject: refactor: factor out recurring charging profile handling X-Git-Tag: v1.2.20~152 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=76dab5a927a69adf1a540ff597a2a2cfa8cdaf7f;p=e-mobility-charging-stations-simulator.git refactor: factor out recurring charging profile handling Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 939c7b8e..94869574 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -57,10 +57,10 @@ import { isNotEmptyString, isNullOrUndefined, isUndefined, + isValidDate, logger, secureRandom, } from '../utils'; -import { isValidDate } from '../utils/Utils'; const moduleName = 'ChargingStationUtils'; @@ -690,11 +690,11 @@ const getLimitFromChargingProfiles = ( const connectorStatus = chargingStation.getConnectorStatus(connectorId); for (const chargingProfile of chargingProfiles) { if ( - chargingProfile.validFrom && - chargingProfile.validTo && + isValidDate(chargingProfile.validFrom) && + isValidDate(chargingProfile.validTo) && !isWithinInterval(currentDate, { - start: chargingProfile.validFrom, - end: chargingProfile.validTo, + start: chargingProfile.validFrom!, + end: chargingProfile.validTo!, }) ) { logger.debug( @@ -729,48 +729,7 @@ const getLimitFromChargingProfiles = ( } // Adjust recurring start schedule if (chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING) { - switch (chargingProfile.recurrencyKind) { - case RecurrencyKindType.DAILY: - if (isBefore(chargingSchedule.startSchedule, startOfDay(currentDate))) { - addDays( - chargingSchedule.startSchedule, - differenceInDays(chargingSchedule.startSchedule, endOfDay(currentDate)), - ); - 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`, - ); - } - } - break; - case RecurrencyKindType.WEEKLY: - if (isBefore(chargingSchedule.startSchedule, startOfWeek(currentDate))) { - addWeeks( - chargingSchedule.startSchedule, - differenceInWeeks(chargingSchedule.startSchedule, endOfWeek(currentDate)), - ); - 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`, - ); - } - } - break; - } + prepareRecurringChargingProfile(chargingProfile, currentDate, logPrefix); } else if ( chargingProfile.chargingProfileKind === ChargingProfileKindType.RELATIVE && connectorStatus?.transactionStarted @@ -835,6 +794,56 @@ const getLimitFromChargingProfiles = ( } }; +const prepareRecurringChargingProfile = ( + chargingProfile: ChargingProfile, + currentDate: Date, + logPrefix: string, +) => { + const chargingSchedule = chargingProfile.chargingSchedule; + switch (chargingProfile.recurrencyKind) { + case RecurrencyKindType.DAILY: + if (isBefore(chargingSchedule.startSchedule!, startOfDay(currentDate))) { + addDays( + chargingSchedule.startSchedule!, + differenceInDays(chargingSchedule.startSchedule!, endOfDay(currentDate)), + ); + 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`, + ); + } + } + break; + case RecurrencyKindType.WEEKLY: + if (isBefore(chargingSchedule.startSchedule!, startOfWeek(currentDate))) { + addWeeks( + chargingSchedule.startSchedule!, + differenceInWeeks(chargingSchedule.startSchedule!, endOfWeek(currentDate)), + ); + 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`, + ); + } + } + break; + } +}; + const getRandomSerialNumberSuffix = (params?: { randomBytesLength?: number; upperCase?: boolean; diff --git a/src/utils/index.ts b/src/utils/index.ts index 4d5a38ac..cdc98711 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -46,6 +46,7 @@ export { isNotEmptyString, isNullOrUndefined, isUndefined, + isValidDate, logPrefix, promiseWithTimeout, roundTo,