From 55f2ab602b8b52836cbf267e960b7c11633a2e97 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 25 Jul 2023 18:32:41 +0200 Subject: [PATCH] fix: ensure recurring profile duration is valid 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 | 11 +++++++++-- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 18 ++++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index fa7263e0..b26f9970 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -736,7 +736,14 @@ const getLimitFromChargingProfiles = ( isAfter(addSeconds(chargingSchedule.startSchedule!, chargingSchedule.duration!), currentDate) ) { if (isNotEmptyArray(chargingSchedule.chargingSchedulePeriod)) { - // Handling of only one schedule period + chargingSchedule.chargingSchedulePeriod.sort((a, b) => a.startPeriod - b.startPeriod); + 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 with startPeriod = 0 if ( chargingSchedule.chargingSchedulePeriod.length === 1 && chargingSchedule.chargingSchedulePeriod[0].startPeriod === 0 @@ -748,7 +755,6 @@ const getLimitFromChargingProfiles = ( logger.debug(debugLogMsg, result); return result; } - chargingSchedule.chargingSchedulePeriod.sort((a, b) => a.startPeriod - b.startPeriod); let lastButOneSchedule: ChargingSchedulePeriod | undefined; // Search for the right schedule period for (const schedulePeriod of chargingSchedule.chargingSchedulePeriod) { @@ -876,6 +882,7 @@ const checkRecurringChargingProfileDuration = ( interval.start, )}`, ); + chargingProfile.chargingSchedule.duration = differenceInSeconds(interval.end, interval.start); } }; diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 82647835..c7337ab6 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -979,20 +979,26 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { private setRemoteStartTransactionChargingProfile( chargingStation: ChargingStation, connectorId: number, - cp: OCPP16ChargingProfile, + chargingProfile: OCPP16ChargingProfile, ): boolean { - if (cp && cp.chargingProfilePurpose === OCPP16ChargingProfilePurposeType.TX_PROFILE) { - OCPP16ServiceUtils.setChargingProfile(chargingStation, connectorId, cp); + if ( + chargingProfile && + chargingProfile.chargingProfilePurpose === OCPP16ChargingProfilePurposeType.TX_PROFILE + ) { + OCPP16ServiceUtils.setChargingProfile(chargingStation, connectorId, chargingProfile); logger.debug( `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction on connector id ${connectorId}: %j`, - cp, + chargingProfile, ); return true; - } else if (cp && cp.chargingProfilePurpose !== OCPP16ChargingProfilePurposeType.TX_PROFILE) { + } else if ( + chargingProfile && + chargingProfile.chargingProfilePurpose !== OCPP16ChargingProfilePurposeType.TX_PROFILE + ) { logger.warn( `${chargingStation.logPrefix()} Not allowed to set ${ - cp.chargingProfilePurpose + chargingProfile.chargingProfilePurpose } charging profile(s) at remote start transaction`, ); return false; -- 2.34.1