From: Jérôme Benoit Date: Wed, 2 Aug 2023 16:55:46 +0000 (+0200) Subject: fix: ensure charging profiles used for power limitation are properly sorted X-Git-Tag: v1.2.20~38 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=6fc0c6f3db444377c0fdea238183a14823278046;p=e-mobility-charging-stations-simulator.git fix: ensure charging profiles used for power limitation are properly sorted Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/Helpers.ts b/src/charging-station/Helpers.ts index 2f775ccf..9de54777 100644 --- a/src/charging-station/Helpers.ts +++ b/src/charging-station/Helpers.ts @@ -514,17 +514,36 @@ export const getAmperageLimitationUnitDivider = (stationInfo: ChargingStationInf return unitDivider; }; +/** + * Gets the connector cloned charging profiles applying a power limitation + * and sorted by connector id ascending then stack level descending + * + * @param chargingStation - + * @param connectorId - + * @returns connector charging profiles array + */ +export const getConnectorChargingProfiles = ( + chargingStation: ChargingStation, + connectorId: number, +) => { + return cloneObject( + (chargingStation.getConnectorStatus(0)?.chargingProfiles ?? []) + .sort((a, b) => b.stackLevel - a.stackLevel) + .concat( + (chargingStation.getConnectorStatus(connectorId)?.chargingProfiles ?? []).sort( + (a, b) => b.stackLevel - a.stackLevel, + ), + ), + ); +}; + export const getChargingStationConnectorChargingProfilesPowerLimit = ( chargingStation: ChargingStation, connectorId: number, ): number | undefined => { let limit: number | undefined, chargingProfile: ChargingProfile | undefined; - // Get charging profiles for connector id and sort by stack level - const chargingProfiles = cloneObject( - (chargingStation.getConnectorStatus(connectorId)?.chargingProfiles ?? []).concat( - chargingStation.getConnectorStatus(0)?.chargingProfiles ?? [], - ), - ).sort((a, b) => b.stackLevel - a.stackLevel); + // Get charging profiles sorted by connector id then stack level + const chargingProfiles = getConnectorChargingProfiles(chargingStation, connectorId); if (isNotEmptyArray(chargingProfiles)) { const result = getLimitFromChargingProfiles( chargingStation, @@ -715,7 +734,7 @@ interface ChargingProfilesLimit { } /** - * Charging profiles shall already be sorted by connector id and stack level (highest stack level has priority) + * Charging profiles should already be sorted by connector id ascending then stack level descending * * @param chargingStation - * @param connectorId - @@ -773,10 +792,10 @@ const getLimitFromChargingProfiles = ( b: ChargingSchedulePeriod, ) => a.startPeriod - b.startPeriod; if ( - isArraySorted( + !isArraySorted( chargingSchedule.chargingSchedulePeriod, chargingSchedulePeriodCompareFn, - ) === false + ) ) { logger.warn( `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${chargingProfile.chargingProfileId} schedule periods are not sorted by start period`, @@ -815,7 +834,7 @@ const getLimitFromChargingProfiles = ( // Found the schedule period: previous is the correct one const result: ChargingProfilesLimit = { limit: previousChargingSchedulePeriod!.limit, - chargingProfile: chargingProfile, + chargingProfile, }; logger.debug(debugLogMsg, result); return result; @@ -836,7 +855,7 @@ const getLimitFromChargingProfiles = ( ) { const result: ChargingProfilesLimit = { limit: previousChargingSchedulePeriod.limit, - chargingProfile: chargingProfile, + chargingProfile, }; logger.debug(debugLogMsg, result); return result; diff --git a/src/charging-station/index.ts b/src/charging-station/index.ts index 3eccd258..ed055a8d 100644 --- a/src/charging-station/index.ts +++ b/src/charging-station/index.ts @@ -8,6 +8,7 @@ export { export { canProceedChargingProfile, checkChargingStation, + getConnectorChargingProfiles, getIdTagsFile, hasFeatureProfile, hasReservationExpired, diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 1941b665..66a6ee59 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -28,6 +28,7 @@ import { canProceedChargingProfile, checkChargingStation, getConfigurationKey, + getConnectorChargingProfiles, prepareChargingProfileKind, removeExpiredReservations, setConfigurationKeyValue, @@ -105,7 +106,6 @@ import { } from '../../../types'; import { Constants, - cloneObject, convertToDate, convertToInt, formatDurationMilliSeconds, @@ -704,11 +704,11 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { start: currentDate, end: addSeconds(currentDate, duration), }; - const storedChargingProfiles: OCPP16ChargingProfile[] = cloneObject( - (connectorStatus?.chargingProfiles ?? []).concat( - chargingStation.getConnectorStatus(0)?.chargingProfiles ?? [], - ), - ).sort((a, b) => b.stackLevel - a.stackLevel); + // Get charging profiles sorted by connector id then stack level + const storedChargingProfiles: OCPP16ChargingProfile[] = getConnectorChargingProfiles( + chargingStation, + connectorId, + ); const chargingProfiles: OCPP16ChargingProfile[] = []; for (const storedChargingProfile of storedChargingProfiles) { if (