X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStationUtils.ts;h=1af1266110bd62d2e49f42e5ec801cc34fac8546;hb=80c580411036965e34b983809b663b812ed36997;hp=0d349d22518def767afb3d535ba0fb5abbd96525;hpb=d476bc1be87bb63cfd8c494ce1ed41204992806f;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 0d349d22..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, @@ -474,7 +475,7 @@ export const getChargingStationConnectorChargingProfilesPowerLimit = ( chargingStation.getConnectorStatus(connectorId)!.chargingProfiles!, )?.sort((a, b) => b.stackLevel - a.stackLevel) ?? []; // Get charging profiles on connector 0 and sort by stack level - if (chargingStation.getConnectorStatus(0)?.chargingProfiles) { + if (isNotEmptyArray(chargingStation.getConnectorStatus(0)?.chargingProfiles)) { chargingProfiles.push( ...cloneObject( chargingStation.getConnectorStatus(0)!.chargingProfiles!, @@ -514,7 +515,7 @@ export const getChargingStationConnectorChargingProfilesPowerLimit = ( chargingStation.getMaximumPower() / chargingStation.powerDivider; if (limit! > connectorMaximumPower) { logger.error( - `${chargingStation.logPrefix()} Charging profile id ${matchingChargingProfile?.chargingProfileId} limit ${limit} is greater than connector id ${connectorId} maximum ${connectorMaximumPower}: %j`, + `${chargingStation.logPrefix()} ${moduleName}.getChargingStationConnectorChargingProfilesPowerLimit: Charging profile id ${matchingChargingProfile?.chargingProfileId} limit ${limit} is greater than connector id ${connectorId} maximum ${connectorMaximumPower}: %j`, result, ); limit = connectorMaximumPower; @@ -688,12 +689,9 @@ const getLimitFromChargingProfiles = ( const connectorStatus = chargingStation.getConnectorStatus(connectorId); for (const chargingProfile of chargingProfiles) { if ( - isValidDate(chargingProfile.validFrom) && - isValidDate(chargingProfile.validTo) && - !isWithinInterval(currentDate, { - start: chargingProfile.validFrom!, - end: chargingProfile.validTo!, - }) + (isValidDate(chargingProfile.validFrom) && + isBefore(currentDate, chargingProfile.validFrom!)) || + (isValidDate(chargingProfile.validTo) && isAfter(currentDate, chargingProfile.validTo!)) ) { logger.debug( `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${ @@ -736,14 +734,36 @@ const getLimitFromChargingProfiles = ( // Check if the charging profile is active if ( isValidDate(chargingSchedule.startSchedule) && - isAfter(addSeconds(chargingSchedule.startSchedule!, chargingSchedule.duration!), currentDate) + isWithinInterval(currentDate, { + start: chargingSchedule.startSchedule!, + end: addSeconds(chargingSchedule.startSchedule!, chargingSchedule.duration!), + }) ) { if (isNotEmptyArray(chargingSchedule.chargingSchedulePeriod)) { - // Handling of only one schedule period + const chargingSchedulePeriodCompareFn = ( + a: ChargingSchedulePeriod, + b: ChargingSchedulePeriod, + ) => a.startPeriod - b.startPeriod; if ( - chargingSchedule.chargingSchedulePeriod.length === 1 && - chargingSchedule.chargingSchedulePeriod[0].startPeriod === 0 + 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( + `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${chargingProfile.chargingProfileId} first schedule period start period ${chargingSchedule.chargingSchedulePeriod[0].startPeriod} is not equal to 0`, + ); + continue; + } + // Handling of only one schedule period + if (chargingSchedule.chargingSchedulePeriod.length === 1) { const result: ChargingProfilesLimit = { limit: chargingSchedule.chargingSchedulePeriod[0].limit, matchingChargingProfile: chargingProfile, @@ -753,7 +773,7 @@ const getLimitFromChargingProfiles = ( } let lastButOneSchedule: ChargingSchedulePeriod | undefined; // Search for the right schedule period - for (const schedulePeriod of chargingSchedule.chargingSchedulePeriod) { + for (const [index, schedulePeriod] of chargingSchedule.chargingSchedulePeriod.entries()) { // Find the right schedule period if ( isAfter( @@ -771,12 +791,18 @@ const getLimitFromChargingProfiles = ( } // Keep it lastButOneSchedule = schedulePeriod; - // Handle the last schedule period + // Handle the last schedule period within the charging profile duration if ( - schedulePeriod.startPeriod === - chargingSchedule.chargingSchedulePeriod[ - chargingSchedule.chargingSchedulePeriod.length - 1 - ].startPeriod + index === chargingSchedule.chargingSchedulePeriod.length - 1 || + (index < chargingSchedule.chargingSchedulePeriod.length - 1 && + chargingSchedule.duration! > + differenceInSeconds( + addSeconds( + chargingSchedule.startSchedule!, + chargingSchedule.chargingSchedulePeriod[index + 1].startPeriod, + ), + chargingSchedule.startSchedule!, + )) ) { const result: ChargingProfilesLimit = { limit: lastButOneSchedule.limit, @@ -818,7 +844,7 @@ const prepareRecurringChargingProfile = ( ) { chargingSchedule.startSchedule = addDays( chargingSchedule.startSchedule!, - differenceInDays(chargingSchedule.startSchedule!, recurringInterval.end), + differenceInDays(currentDate, recurringInterval.start), ); recurringInterval = { start: chargingSchedule.startSchedule, @@ -838,7 +864,7 @@ const prepareRecurringChargingProfile = ( ) { chargingSchedule.startSchedule = addWeeks( chargingSchedule.startSchedule!, - differenceInWeeks(chargingSchedule.startSchedule!, recurringInterval.end), + differenceInWeeks(currentDate, recurringInterval.start), ); recurringInterval = { start: chargingSchedule.startSchedule, @@ -849,7 +875,7 @@ const prepareRecurringChargingProfile = ( } if (!isWithinInterval(currentDate, recurringInterval!)) { logger.error( - `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring ${ + `${logPrefix} ${moduleName}.prepareRecurringChargingProfile: Recurring ${ chargingProfile.recurrencyKind } charging profile id ${ chargingProfile.chargingProfileId @@ -869,15 +895,16 @@ const checkRecurringChargingProfileDuration = ( chargingProfile.chargingSchedule.duration! > differenceInSeconds(interval.end, interval.start) ) { logger.warn( - `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring ${ + `${logPrefix} ${moduleName}.checkRecurringChargingProfileDuration: Recurring ${ chargingProfile.chargingProfileKind } charging profile id ${chargingProfile.chargingProfileId} duration ${ chargingProfile.chargingSchedule.duration - } is greater than the recurrency time interval ${differenceInSeconds( + } is greater than the recurrency time interval duration ${differenceInSeconds( interval.end, interval.start, - )} duration`, + )}`, ); + chargingProfile.chargingSchedule.duration = differenceInSeconds(interval.end, interval.start); } };