From 252a7d22e33f6c4a9bfcc83cb4fb941f76d15dab Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 25 Jul 2023 12:33:50 +0200 Subject: [PATCH] feat: add support charging profile validity interval MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStationUtils.ts | 104 ++++++++++++------- 1 file changed, 65 insertions(+), 39 deletions(-) diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 81096ef1..8d595b22 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -14,6 +14,7 @@ import { endOfWeek, isAfter, isBefore, + isWithinInterval, startOfDay, startOfWeek, } from 'date-fns'; @@ -468,12 +469,12 @@ export const getChargingStationConnectorChargingProfilesPowerLimit = ( connectorId: number, ): number | undefined => { let limit: number | undefined, matchingChargingProfile: ChargingProfile | undefined; - // Get charging profiles for connector and sort by stack level + // Get charging profiles for connector id and sort by stack level const chargingProfiles = cloneObject( chargingStation.getConnectorStatus(connectorId)!.chargingProfiles!, )?.sort((a, b) => b.stackLevel - a.stackLevel) ?? []; - // Get profiles on connector 0 + // Get charging profiles on connector 0 and sort by stack level if (chargingStation.getConnectorStatus(0)?.chargingProfiles) { chargingProfiles.push( ...cloneObject( @@ -686,7 +687,21 @@ const getLimitFromChargingProfiles = ( const debugLogMsg = `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Matching charging profile found for power limitation: %j`; const currentDate = new Date(); for (const chargingProfile of chargingProfiles) { - // Set helpers + if ( + chargingProfile.validFrom && + chargingProfile.validTo && + !isWithinInterval(currentDate, { + start: chargingProfile.validFrom, + end: chargingProfile.validTo, + }) + ) { + logger.debug( + `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${ + chargingProfile.chargingProfileId + } is not valid for the current date ${currentDate.toISOString()}`, + ); + continue; + } const chargingSchedule = chargingProfile.chargingSchedule; if (!chargingSchedule?.startSchedule) { logger.debug( @@ -702,6 +717,15 @@ const getLimitFromChargingProfiles = ( ); chargingSchedule.startSchedule = convertToDate(chargingSchedule.startSchedule)!; } + if ( + chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING && + isNullOrUndefined(chargingProfile.recurrencyKind) + ) { + logger.error( + `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring charging profile id ${chargingProfile.chargingProfileId} has no recurrencyKind defined`, + ); + continue; + } // Adjust recurring start schedule if (chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING) { switch (chargingProfile.recurrencyKind) { @@ -754,51 +778,53 @@ const getLimitFromChargingProfiles = ( if ( isAfter(addSeconds(chargingSchedule.startSchedule!, chargingSchedule.duration!), currentDate) ) { - let lastButOneSchedule: ChargingSchedulePeriod | undefined; - // Search the right schedule period - for (const schedulePeriod of chargingSchedule.chargingSchedulePeriod) { + if (isNotEmptyArray(chargingSchedule.chargingSchedulePeriod)) { // Handling of only one period if ( chargingSchedule.chargingSchedulePeriod.length === 1 && - schedulePeriod.startPeriod === 0 + chargingSchedule.chargingSchedulePeriod[0].startPeriod === 0 ) { - const result = { - limit: schedulePeriod.limit, + const result: ChargingProfilesLimit = { + limit: chargingSchedule.chargingSchedulePeriod[0].limit, matchingChargingProfile: chargingProfile, }; logger.debug(debugLogMsg, result); return result; } - // Find the right schedule period - if ( - isAfter( - addSeconds(chargingSchedule.startSchedule!, schedulePeriod.startPeriod), - currentDate, - ) - ) { - // Found the schedule: last but one is the correct one - const result = { - limit: lastButOneSchedule!.limit, - matchingChargingProfile: chargingProfile, - }; - logger.debug(debugLogMsg, result); - return result; - } - // Keep it - lastButOneSchedule = schedulePeriod; - // Handle the last schedule period - if ( - schedulePeriod.startPeriod === - chargingSchedule.chargingSchedulePeriod[ - chargingSchedule.chargingSchedulePeriod.length - 1 - ].startPeriod - ) { - const result = { - limit: lastButOneSchedule.limit, - matchingChargingProfile: chargingProfile, - }; - logger.debug(debugLogMsg, result); - return result; + let lastButOneSchedule: ChargingSchedulePeriod | undefined; + // Search for the right schedule period + for (const schedulePeriod of chargingSchedule.chargingSchedulePeriod) { + // Find the right schedule period + if ( + isAfter( + addSeconds(chargingSchedule.startSchedule!, schedulePeriod.startPeriod), + currentDate, + ) + ) { + // Found the schedule: last but one is the correct one + const result: ChargingProfilesLimit = { + limit: lastButOneSchedule!.limit, + matchingChargingProfile: chargingProfile, + }; + logger.debug(debugLogMsg, result); + return result; + } + // Keep it + lastButOneSchedule = schedulePeriod; + // Handle the last schedule period + if ( + schedulePeriod.startPeriod === + chargingSchedule.chargingSchedulePeriod[ + chargingSchedule.chargingSchedulePeriod.length - 1 + ].startPeriod + ) { + const result: ChargingProfilesLimit = { + limit: lastButOneSchedule.limit, + matchingChargingProfile: chargingProfile, + }; + logger.debug(debugLogMsg, result); + return result; + } } } } -- 2.34.1