X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStationUtils.ts;h=7865eb1f3d14433226aad8ddd16a8db496360645;hb=de3272506539665b7ae509b7c056423af1332fb2;hp=09e0b8e420334aaadf0aeb29ca21013fe6fea102;hpb=a675e34bb7d1711aace56f6ed8cdb4f91453e39d;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 09e0b8e4..7865eb1f 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, isTomorrow, isYesterday } from 'date-fns'; import type { ChargingStation } from './ChargingStation'; import { BaseError } from '../exception'; @@ -36,6 +36,7 @@ import { Constants, DCElectricUtils, cloneObject, + convertToDate, convertToInt, isEmptyObject, isEmptyString, @@ -670,34 +671,31 @@ 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 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 (isYesterday(chargingSchedule.startSchedule)) { + chargingSchedule.startSchedule.setFullYear( + currentDate.getFullYear(), + currentDate.getMonth(), + currentDate.getDate(), ); - chargingSchedule.startSchedule = new Date(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)) { + } else if (isTomorrow(chargingSchedule.startSchedule)) { chargingSchedule.startSchedule.setDate(currentDate.getDate() - 1); } - } else 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 @@ -717,7 +715,7 @@ const getLimitFromChargingProfiles = ( // Find the right schedule period if ( isAfter( - addSeconds(chargingSchedule.startSchedule!, schedulePeriod.startPeriod), + addSeconds(chargingSchedule.startSchedule, schedulePeriod.startPeriod), currentDate, ) ) {