import { fileURLToPath } from 'node:url';
import chalk from 'chalk';
-import moment from 'moment';
+import {
+ addDays,
+ addSeconds,
+ addWeeks,
+ differenceInDays,
+ differenceInWeeks,
+ endOfDay,
+ endOfWeek,
+ isAfter,
+ isBefore,
+ startOfDay,
+ startOfWeek,
+} from 'date-fns';
import type { ChargingStation } from './ChargingStation';
import { BaseError } from '../exception';
Constants,
DCElectricUtils,
cloneObject,
+ convertToDate,
convertToInt,
isEmptyObject,
isEmptyString,
event: ChargingStationWorkerMessageEvents,
eventsToWait: number,
): Promise<number> => {
- return new Promise((resolve) => {
+ return new Promise<number>((resolve) => {
let events = 0;
if (eventsToWait === 0) {
resolve(events);
const getLimitFromChargingProfiles = (
chargingProfiles: ChargingProfile[],
logPrefix: string,
-): {
- limit: number;
- matchingChargingProfile: ChargingProfile;
-} | null => {
+):
+ | {
+ limit: number;
+ matchingChargingProfile: ChargingProfile;
+ }
+ | undefined => {
const debugLogMsg = `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Matching charging profile found for power limitation: %j`;
- const currentMoment = moment();
const currentDate = new Date();
for (const chargingProfile of chargingProfiles) {
// Set helpers
`${logPrefix} ${moduleName}.getLimitFromChargingProfiles: startSchedule is not defined in charging profile id ${chargingProfile.chargingProfileId}`,
);
}
- // 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 &&
- currentMoment.isAfter(chargingSchedule.startSchedule)
- ) {
- 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 = new Date(chargingSchedule.startSchedule!);
- }
- chargingSchedule.startSchedule.setFullYear(
- currentDate.getFullYear(),
- currentDate.getMonth(),
- currentDate.getDate(),
+ 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`,
);
- // Check if the start of the schedule is yesterday
- if (moment(chargingSchedule.startSchedule).isAfter(currentMoment)) {
- chargingSchedule.startSchedule.setDate(currentDate.getDate() - 1);
+ chargingSchedule.startSchedule = convertToDate(chargingSchedule.startSchedule)!;
+ }
+ // Adjust recurring start schedule
+ if (chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING) {
+ switch (chargingProfile.recurrencyKind) {
+ case RecurrencyKindType.DAILY:
+ if (isBefore(chargingSchedule.startSchedule, startOfDay(currentDate))) {
+ addDays(
+ chargingSchedule.startSchedule,
+ differenceInDays(chargingSchedule.startSchedule, endOfDay(currentDate)),
+ );
+ }
+ break;
+ case RecurrencyKindType.WEEKLY:
+ if (isBefore(chargingSchedule.startSchedule, startOfWeek(currentDate))) {
+ addWeeks(
+ chargingSchedule.startSchedule,
+ differenceInWeeks(chargingSchedule.startSchedule, endOfWeek(currentDate)),
+ );
+ }
+ break;
}
- } else if (moment(chargingSchedule.startSchedule).isAfter(currentMoment)) {
- return null;
}
// Check if the charging profile is active
if (
- moment(chargingSchedule.startSchedule)
- .add(chargingSchedule.duration, 's')
- .isAfter(currentMoment)
+ isAfter(addSeconds(chargingSchedule.startSchedule, chargingSchedule.duration!), currentDate)
) {
let lastButOneSchedule: ChargingSchedulePeriod | undefined;
// Search the right schedule period
}
// Find the right schedule period
if (
- moment(chargingSchedule.startSchedule)
- .add(schedulePeriod.startPeriod, 's')
- .isAfter(currentMoment)
+ isAfter(
+ addSeconds(chargingSchedule.startSchedule, schedulePeriod.startPeriod),
+ currentDate,
+ )
) {
// Found the schedule: last but one is the correct one
const result = {
}
}
}
- return null;
};
const getRandomSerialNumberSuffix = (params?: {