From 8d75a4031f01609a0e788a62c2835ebc0a627330 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 24 Jul 2023 12:24:50 +0200 Subject: [PATCH] fix: fix daily recurring charging profiles handling 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 | 41 ++++++++++---------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index e8ba2db5..1d1119d3 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -4,7 +4,7 @@ import { basename, dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; import chalk from 'chalk'; -import { addSeconds, isAfter } from 'date-fns'; +import { addSeconds, isAfter, isBefore } from 'date-fns'; import type { ChargingStation } from './ChargingStation'; import { BaseError } from '../exception'; @@ -671,34 +671,35 @@ const getLimitFromChargingProfiles = ( `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: startSchedule is not defined in charging profile id ${chargingProfile.chargingProfileId}`, ); } + if (!(chargingSchedule?.startSchedule instanceof Date)) { + logger.warn( + `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: startSchedule is not a Date object in charging profile id ${chargingProfile.chargingProfileId}. Trying to convert it to a Date object`, + ); + chargingSchedule.startSchedule = convertToDate(chargingSchedule.startSchedule)!; + } // Check type (recurring) and if it is already active - // Adjust the daily recurring schedule to today + // Adjust the daily recurring schedule if ( chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING && - chargingProfile.recurrencyKind === RecurrencyKindType.DAILY && - isAfter(currentDate, chargingSchedule.startSchedule!) + chargingProfile.recurrencyKind === RecurrencyKindType.DAILY ) { - if (!(chargingSchedule?.startSchedule instanceof Date)) { - logger.warn( - `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: startSchedule is not a Date object in charging profile id ${chargingProfile.chargingProfileId}. Trying to convert it to a Date object`, + if (isBefore(chargingSchedule.startSchedule, currentDate)) { + chargingSchedule.startSchedule.setFullYear( + currentDate.getFullYear(), + currentDate.getMonth(), + currentDate.getDate(), ); - chargingSchedule.startSchedule = convertToDate(chargingSchedule.startSchedule)!; - } - chargingSchedule.startSchedule.setFullYear( - currentDate.getFullYear(), - currentDate.getMonth(), - currentDate.getDate(), - ); - // Check if the start of the schedule is yesterday - if (isAfter(chargingSchedule.startSchedule, currentDate)) { + // Check if the start of the schedule must be set to yesterday + } else if (isAfter(chargingSchedule.startSchedule, currentDate)) { chargingSchedule.startSchedule.setDate(currentDate.getDate() - 1); } - } else if (isAfter(chargingSchedule.startSchedule!, currentDate)) { - return null; } + // if (isAfter(chargingSchedule.startSchedule, currentDate)) { + // return null; + // } // Check if the charging profile is active if ( - isAfter(addSeconds(chargingSchedule.startSchedule!, chargingSchedule.duration!), currentDate) + isAfter(addSeconds(chargingSchedule.startSchedule, chargingSchedule.duration!), currentDate) ) { let lastButOneSchedule: ChargingSchedulePeriod | undefined; // Search the right schedule period @@ -718,7 +719,7 @@ const getLimitFromChargingProfiles = ( // Find the right schedule period if ( isAfter( - addSeconds(chargingSchedule.startSchedule!, schedulePeriod.startPeriod), + addSeconds(chargingSchedule.startSchedule, schedulePeriod.startPeriod), currentDate, ) ) { -- 2.34.1