refactor: add sanity check on recurring charging profile duration
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStationUtils.ts
index 0fae41f7ddd259b209d7d1a8132e3a00a8f9dbb7..0d349d22518def767afb3d535ba0fb5abbd96525 100644 (file)
@@ -4,7 +4,18 @@ import { basename, dirname, join } from 'node:path';
 import { fileURLToPath } from 'node:url';
 
 import chalk from 'chalk';
-import moment from 'moment';
+import {
+  addDays,
+  addSeconds,
+  addWeeks,
+  differenceInDays,
+  differenceInSeconds,
+  differenceInWeeks,
+  isAfter,
+  isBefore,
+  isWithinInterval,
+  toDate,
+} from 'date-fns';
 
 import type { ChargingStation } from './ChargingStation';
 import { BaseError } from '../exception';
@@ -36,6 +47,7 @@ import {
   Constants,
   DCElectricUtils,
   cloneObject,
+  convertToDate,
   convertToInt,
   isEmptyObject,
   isEmptyString,
@@ -43,6 +55,7 @@ import {
   isNotEmptyString,
   isNullOrUndefined,
   isUndefined,
+  isValidDate,
   logger,
   secureRandom,
 } from '../utils';
@@ -289,9 +302,10 @@ export const resetConnectorStatus = (connectorStatus: ConnectorStatus): void =>
   connectorStatus.idTagAuthorized = false;
   connectorStatus.transactionRemoteStarted = false;
   connectorStatus.transactionStarted = false;
+  delete connectorStatus?.transactionStart;
+  delete connectorStatus?.transactionId;
   delete connectorStatus?.localAuthorizeIdTag;
   delete connectorStatus?.authorizeIdTag;
-  delete connectorStatus?.transactionId;
   delete connectorStatus?.transactionIdTag;
   connectorStatus.transactionEnergyActiveImportRegisterValue = 0;
   delete connectorStatus?.transactionBeginMeterValue;
@@ -454,12 +468,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<ChargingProfile[]>(
       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<ChargingProfile[]>(
@@ -468,7 +482,12 @@ export const getChargingStationConnectorChargingProfilesPowerLimit = (
     );
   }
   if (isNotEmptyArray(chargingProfiles)) {
-    const result = getLimitFromChargingProfiles(chargingProfiles, chargingStation.logPrefix());
+    const result = getLimitFromChargingProfiles(
+      chargingStation,
+      connectorId,
+      chargingProfiles,
+      chargingStation.logPrefix(),
+    );
     if (!isNullOrUndefined(result)) {
       limit = result?.limit;
       matchingChargingProfile = result?.matchingChargingProfile;
@@ -538,7 +557,7 @@ export const waitChargingStationEvents = async (
   event: ChargingStationWorkerMessageEvents,
   eventsToWait: number,
 ): Promise<number> => {
-  return new Promise((resolve) => {
+  return new Promise<number>((resolve) => {
     let events = 0;
     if (eventsToWait === 0) {
       resolve(events);
@@ -623,7 +642,7 @@ const warnDeprecatedTemplateKey = (
   templateFile: string,
   logMsgToAppend = '',
 ): void => {
-  if (!isUndefined(template[key])) {
+  if (!isUndefined(template[key as keyof ChargingStationTemplate])) {
     const logMsg = `Deprecated template key '${key}' usage in file '${templateFile}'${
       isNotEmptyString(logMsgToAppend) ? `. ${logMsgToAppend}` : ''
     }`;
@@ -637,119 +656,229 @@ const convertDeprecatedTemplateKey = (
   deprecatedKey: string,
   key?: string,
 ): void => {
-  if (!isUndefined(template[deprecatedKey])) {
+  if (!isUndefined(template[deprecatedKey as keyof ChargingStationTemplate])) {
     if (!isUndefined(key)) {
-      template[key!] = template[deprecatedKey] as unknown;
+      (template as unknown as Record<string, unknown>)[key!] =
+        template[deprecatedKey as keyof ChargingStationTemplate];
     }
-    delete template[deprecatedKey];
+    delete template[deprecatedKey as keyof ChargingStationTemplate];
   }
 };
 
+interface ChargingProfilesLimit {
+  limit: number;
+  matchingChargingProfile: ChargingProfile;
+}
+
 /**
  * Charging profiles should already be sorted by connector id and stack level (highest stack level has priority)
  *
  * @param chargingProfiles -
  * @param logPrefix -
- * @returns
+ * @returns ChargingProfilesLimit
  */
 const getLimitFromChargingProfiles = (
+  chargingStation: ChargingStation,
+  connectorId: number,
   chargingProfiles: ChargingProfile[],
   logPrefix: string,
-): {
-  limit: number;
-  matchingChargingProfile: ChargingProfile;
-} | null => {
+): ChargingProfilesLimit | undefined => {
   const debugLogMsg = `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Matching charging profile found for power limitation: %j`;
-  const currentMoment = moment();
   const currentDate = new Date();
+  const connectorStatus = chargingStation.getConnectorStatus(connectorId);
   for (const chargingProfile of chargingProfiles) {
-    // Set helpers
+    if (
+      isValidDate(chargingProfile.validFrom) &&
+      isValidDate(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) {
+    if (connectorStatus?.transactionStarted && !chargingSchedule?.startSchedule) {
+      logger.debug(
+        `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: startSchedule is not defined in charging profile id ${chargingProfile.chargingProfileId}. Trying to set it to the connector transaction start date`,
+      );
+      // OCPP specifies that if startSchedule is not defined, it should be relative to start of the connector transaction
+      chargingSchedule.startSchedule = connectorStatus?.transactionStart;
+    }
+    if (!(chargingSchedule?.startSchedule instanceof Date)) {
       logger.warn(
-        `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: startSchedule is not defined in charging profile id ${chargingProfile.chargingProfileId}`,
+        `${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 &&
-      currentMoment.isAfter(chargingSchedule.startSchedule)
+      isNullOrUndefined(chargingProfile.recurrencyKind)
     ) {
-      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(),
+      logger.error(
+        `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring charging profile id ${chargingProfile.chargingProfileId} has no recurrencyKind defined`,
       );
-      // Check if the start of the schedule is yesterday
-      if (moment(chargingSchedule.startSchedule).isAfter(currentMoment)) {
-        chargingSchedule.startSchedule.setDate(currentDate.getDate() - 1);
-      }
-    } else if (moment(chargingSchedule.startSchedule).isAfter(currentMoment)) {
-      return null;
+      continue;
+    }
+    if (chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING) {
+      prepareRecurringChargingProfile(chargingProfile, currentDate, logPrefix);
+    } else if (
+      chargingProfile.chargingProfileKind === ChargingProfileKindType.RELATIVE &&
+      connectorStatus?.transactionStarted
+    ) {
+      chargingSchedule.startSchedule = connectorStatus?.transactionStart;
     }
     // Check if the charging profile is active
     if (
-      moment(chargingSchedule.startSchedule)
-        .add(chargingSchedule.duration, 's')
-        .isAfter(currentMoment)
+      isValidDate(chargingSchedule.startSchedule) &&
+      isAfter(addSeconds(chargingSchedule.startSchedule!, chargingSchedule.duration!), currentDate)
     ) {
-      let lastButOneSchedule: ChargingSchedulePeriod | undefined;
-      // Search the right schedule period
-      for (const schedulePeriod of chargingSchedule.chargingSchedulePeriod) {
-        // Handling of only one period
+      if (isNotEmptyArray(chargingSchedule.chargingSchedulePeriod)) {
+        // Handling of only one schedule 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 (
-          moment(chargingSchedule.startSchedule)
-            .add(schedulePeriod.startPeriod, 's')
-            .isAfter(currentMoment)
-        ) {
-          // 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 period: 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;
+          }
         }
       }
     }
   }
-  return null;
+};
+
+/**
+ *  Adjust recurring charging profile startSchedule to the current recurrency time interval if needed
+ *
+ * @param chargingProfile -
+ * @param currentDate -
+ * @param logPrefix -
+ */
+const prepareRecurringChargingProfile = (
+  chargingProfile: ChargingProfile,
+  currentDate: Date,
+  logPrefix: string,
+) => {
+  const chargingSchedule = chargingProfile.chargingSchedule;
+  let recurringInterval: Interval;
+  switch (chargingProfile.recurrencyKind) {
+    case RecurrencyKindType.DAILY:
+      recurringInterval = {
+        start: chargingSchedule.startSchedule!,
+        end: addDays(chargingSchedule.startSchedule!, 1),
+      };
+      checkRecurringChargingProfileDuration(chargingProfile, recurringInterval, logPrefix);
+      if (
+        !isWithinInterval(currentDate, recurringInterval) &&
+        isBefore(chargingSchedule.startSchedule!, currentDate)
+      ) {
+        chargingSchedule.startSchedule = addDays(
+          chargingSchedule.startSchedule!,
+          differenceInDays(chargingSchedule.startSchedule!, recurringInterval.end),
+        );
+        recurringInterval = {
+          start: chargingSchedule.startSchedule,
+          end: addDays(chargingSchedule.startSchedule, 1),
+        };
+      }
+      break;
+    case RecurrencyKindType.WEEKLY:
+      recurringInterval = {
+        start: chargingSchedule.startSchedule!,
+        end: addWeeks(chargingSchedule.startSchedule!, 1),
+      };
+      checkRecurringChargingProfileDuration(chargingProfile, recurringInterval, logPrefix);
+      if (
+        !isWithinInterval(currentDate, recurringInterval) &&
+        isBefore(chargingSchedule.startSchedule!, currentDate)
+      ) {
+        chargingSchedule.startSchedule = addWeeks(
+          chargingSchedule.startSchedule!,
+          differenceInWeeks(chargingSchedule.startSchedule!, recurringInterval.end),
+        );
+        recurringInterval = {
+          start: chargingSchedule.startSchedule,
+          end: addWeeks(chargingSchedule.startSchedule, 1),
+        };
+      }
+      break;
+  }
+  if (!isWithinInterval(currentDate, recurringInterval!)) {
+    logger.error(
+      `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring ${
+        chargingProfile.recurrencyKind
+      } charging profile id ${
+        chargingProfile.chargingProfileId
+      } startSchedule ${chargingSchedule.startSchedule!.toISOString()} is not properly translated to current recurrency time interval [${toDate(
+        recurringInterval!.start,
+      ).toISOString()}, ${toDate(recurringInterval!.end).toISOString()}]`,
+    );
+  }
+};
+
+const checkRecurringChargingProfileDuration = (
+  chargingProfile: ChargingProfile,
+  interval: Interval,
+  logPrefix: string,
+) => {
+  if (
+    chargingProfile.chargingSchedule.duration! > differenceInSeconds(interval.end, interval.start)
+  ) {
+    logger.warn(
+      `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring ${
+        chargingProfile.chargingProfileKind
+      } charging profile id ${chargingProfile.chargingProfileId} duration ${
+        chargingProfile.chargingSchedule.duration
+      } is greater than the recurrency time interval ${differenceInSeconds(
+        interval.end,
+        interval.start,
+      )} duration`,
+    );
+  }
 };
 
 const getRandomSerialNumberSuffix = (params?: {