From 6fc0c6f3db444377c0fdea238183a14823278046 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 2 Aug 2023 18:55:46 +0200 Subject: [PATCH] fix: ensure charging profiles used for power limitation are properly sorted MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/Helpers.ts | 41 ++++++++++++++----- src/charging-station/index.ts | 1 + .../ocpp/1.6/OCPP16IncomingRequestService.ts | 12 +++--- 3 files changed, 37 insertions(+), 17 deletions(-) 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 ( -- 2.34.1