refactor: add type for charging profiles limit
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStationUtils.ts
index 868d0adbf23970b8157ad9ca03256ec4eeaae533..b33a51083562e41668f5e441118f3da86937ae04 100644 (file)
@@ -4,7 +4,19 @@ 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,
+  differenceInWeeks,
+  endOfDay,
+  endOfWeek,
+  isAfter,
+  isBefore,
+  startOfDay,
+  startOfWeek,
+} from 'date-fns';
 
 import type { ChargingStation } from './ChargingStation';
 import { BaseError } from '../exception';
@@ -36,6 +48,7 @@ import {
   Constants,
   DCElectricUtils,
   cloneObject,
+  convertToDate,
   convertToInt,
   isEmptyObject,
   isEmptyString,
@@ -181,7 +194,7 @@ export const checkTemplate = (
     logger.error(`${logPrefix} ${errorMsg}`);
     throw new BaseError(errorMsg);
   }
-  if (isEmptyObject(stationTemplate.AutomaticTransactionGenerator)) {
+  if (isEmptyObject(stationTemplate.AutomaticTransactionGenerator!)) {
     stationTemplate.AutomaticTransactionGenerator = Constants.DEFAULT_ATG_CONFIGURATION;
     logger.warn(
       `${logPrefix} Empty automatic transaction generator configuration from template file ${templateFile}, set to default: %j`,
@@ -206,9 +219,9 @@ export const checkConnectorsConfiguration = (
 } => {
   const configuredMaxConnectors = getConfiguredNumberOfConnectors(stationTemplate);
   checkConfiguredMaxConnectors(configuredMaxConnectors, logPrefix, templateFile);
-  const templateMaxConnectors = getMaxNumberOfConnectors(stationTemplate.Connectors);
+  const templateMaxConnectors = getMaxNumberOfConnectors(stationTemplate.Connectors!);
   checkTemplateMaxConnectors(templateMaxConnectors, logPrefix, templateFile);
-  const templateMaxAvailableConnectors = stationTemplate?.Connectors[0]
+  const templateMaxAvailableConnectors = stationTemplate.Connectors![0]
     ? templateMaxConnectors - 1
     : templateMaxConnectors;
   if (
@@ -271,15 +284,15 @@ export const initializeConnectorsMapStatus = (
       );
     }
     if (connectorId === 0) {
-      connectors.get(connectorId).availability = AvailabilityType.Operative;
+      connectors.get(connectorId)!.availability = AvailabilityType.Operative;
       if (isUndefined(connectors.get(connectorId)?.chargingProfiles)) {
-        connectors.get(connectorId).chargingProfiles = [];
+        connectors.get(connectorId)!.chargingProfiles = [];
       }
     } else if (
       connectorId > 0 &&
       isNullOrUndefined(connectors.get(connectorId)?.transactionStarted)
     ) {
-      initializeConnectorStatus(connectors.get(connectorId));
+      initializeConnectorStatus(connectors.get(connectorId)!);
     }
   }
 };
@@ -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;
@@ -354,10 +368,10 @@ export const warnTemplateKeysDeprecation = (
   logPrefix: string,
   templateFile: string,
 ) => {
-  const templateKeys: { key: string; deprecatedKey: string }[] = [
-    { key: 'supervisionUrls', deprecatedKey: 'supervisionUrl' },
-    { key: 'idTagsFile', deprecatedKey: 'authorizationFile' },
-    { key: 'ocppStrictCompliance', deprecatedKey: 'payloadSchemaValidation' },
+  const templateKeys: { deprecatedKey: string; key?: string }[] = [
+    { deprecatedKey: 'supervisionUrl', key: 'supervisionUrls' },
+    { deprecatedKey: 'authorizationFile', key: 'idTagsFile' },
+    { deprecatedKey: 'payloadSchemaValidation', key: 'ocppStrictCompliance' },
   ];
   for (const templateKey of templateKeys) {
     warnDeprecatedTemplateKey(
@@ -365,7 +379,7 @@ export const warnTemplateKeysDeprecation = (
       templateKey.deprecatedKey,
       logPrefix,
       templateFile,
-      `Use '${templateKey.key}' instead`,
+      !isUndefined(templateKey.key) ? `Use '${templateKey.key}' instead` : undefined,
     );
     convertDeprecatedTemplateKey(stationTemplate, templateKey.deprecatedKey, templateKey.key);
   }
@@ -377,8 +391,8 @@ export const stationTemplateToStationInfo = (
   stationTemplate = cloneObject<ChargingStationTemplate>(stationTemplate);
   delete stationTemplate.power;
   delete stationTemplate.powerUnit;
-  delete stationTemplate?.Connectors;
-  delete stationTemplate?.Evses;
+  delete stationTemplate.Connectors;
+  delete stationTemplate.Evses;
   delete stationTemplate.Configuration;
   delete stationTemplate.AutomaticTransactionGenerator;
   delete stationTemplate.chargeBoxSerialNumberPrefix;
@@ -453,49 +467,54 @@ export const getChargingStationConnectorChargingProfilesPowerLimit = (
   chargingStation: ChargingStation,
   connectorId: number,
 ): number | undefined => {
-  let limit: number, matchingChargingProfile: ChargingProfile;
+  let limit: number | undefined, matchingChargingProfile: ChargingProfile | undefined;
   // Get charging profiles for connector and sort by stack level
   const chargingProfiles =
     cloneObject<ChargingProfile[]>(
-      chargingStation.getConnectorStatus(connectorId)?.chargingProfiles,
+      chargingStation.getConnectorStatus(connectorId)!.chargingProfiles!,
     )?.sort((a, b) => b.stackLevel - a.stackLevel) ?? [];
   // Get profiles on connector 0
   if (chargingStation.getConnectorStatus(0)?.chargingProfiles) {
     chargingProfiles.push(
       ...cloneObject<ChargingProfile[]>(
-        chargingStation.getConnectorStatus(0).chargingProfiles,
+        chargingStation.getConnectorStatus(0)!.chargingProfiles!,
       ).sort((a, b) => b.stackLevel - a.stackLevel),
     );
   }
   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;
       switch (chargingStation.getCurrentOutType()) {
         case CurrentType.AC:
           limit =
-            matchingChargingProfile.chargingSchedule.chargingRateUnit === ChargingRateUnitType.WATT
+            matchingChargingProfile?.chargingSchedule?.chargingRateUnit ===
+            ChargingRateUnitType.WATT
               ? limit
               : ACElectricUtils.powerTotal(
                   chargingStation.getNumberOfPhases(),
                   chargingStation.getVoltageOut(),
-                  limit,
+                  limit!,
                 );
           break;
         case CurrentType.DC:
           limit =
-            matchingChargingProfile.chargingSchedule.chargingRateUnit === ChargingRateUnitType.WATT
+            matchingChargingProfile?.chargingSchedule?.chargingRateUnit ===
+            ChargingRateUnitType.WATT
               ? limit
-              : DCElectricUtils.power(chargingStation.getVoltageOut(), limit);
+              : DCElectricUtils.power(chargingStation.getVoltageOut(), limit!);
       }
       const connectorMaximumPower =
         chargingStation.getMaximumPower() / chargingStation.powerDivider;
-      if (limit > connectorMaximumPower) {
+      if (limit! > connectorMaximumPower) {
         logger.error(
-          `${chargingStation.logPrefix()} Charging profile id ${
-            matchingChargingProfile.chargingProfileId
-          } limit ${limit} is greater than connector id ${connectorId} maximum ${connectorMaximumPower}: %j`,
+          `${chargingStation.logPrefix()} Charging profile id ${matchingChargingProfile?.chargingProfileId} limit ${limit} is greater than connector id ${connectorId} maximum ${connectorMaximumPower}: %j`,
           result,
         );
         limit = connectorMaximumPower;
@@ -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);
@@ -553,7 +572,7 @@ export const waitChargingStationEvents = async (
 };
 
 const getConfiguredNumberOfConnectors = (stationTemplate: ChargingStationTemplate): number => {
-  let configuredMaxConnectors: number;
+  let configuredMaxConnectors = 0;
   if (isNotEmptyArray(stationTemplate.numberOfConnectors) === true) {
     const numberOfConnectors = stationTemplate.numberOfConnectors as number[];
     configuredMaxConnectors =
@@ -561,11 +580,10 @@ const getConfiguredNumberOfConnectors = (stationTemplate: ChargingStationTemplat
   } else if (isUndefined(stationTemplate.numberOfConnectors) === false) {
     configuredMaxConnectors = stationTemplate.numberOfConnectors as number;
   } else if (stationTemplate.Connectors && !stationTemplate.Evses) {
-    configuredMaxConnectors = stationTemplate?.Connectors[0]
+    configuredMaxConnectors = stationTemplate.Connectors[0]
       ? getMaxNumberOfConnectors(stationTemplate.Connectors) - 1
       : getMaxNumberOfConnectors(stationTemplate.Connectors);
   } else if (stationTemplate.Evses && !stationTemplate.Connectors) {
-    configuredMaxConnectors = 0;
     for (const evse in stationTemplate.Evses) {
       if (evse === '0') {
         continue;
@@ -624,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}` : ''
     }`;
@@ -636,71 +654,107 @@ const warnDeprecatedTemplateKey = (
 const convertDeprecatedTemplateKey = (
   template: ChargingStationTemplate,
   deprecatedKey: string,
-  key: string,
+  key?: string,
 ): void => {
-  if (!isUndefined(template[deprecatedKey])) {
-    template[key] = template[deprecatedKey] as unknown;
-    delete template[deprecatedKey];
+  if (!isUndefined(template[deprecatedKey as keyof ChargingStationTemplate])) {
+    if (!isUndefined(key)) {
+      (template as unknown as Record<string, unknown>)[key!] =
+        template[deprecatedKey as keyof ChargingStationTemplate];
+    }
+    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();
   for (const chargingProfile of chargingProfiles) {
     // Set helpers
     const chargingSchedule = chargingProfile.chargingSchedule;
     if (!chargingSchedule?.startSchedule) {
       logger.warn(
-        `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: startSchedule is not defined in charging profile id ${chargingProfile.chargingProfileId}`,
+        `${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 =
+        chargingStation.getConnectorStatus(connectorId)?.transactionStart;
     }
-    // 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)),
+            );
+            if (
+              isBefore(chargingSchedule.startSchedule, startOfDay(currentDate)) ||
+              isAfter(chargingSchedule.startSchedule, endOfDay(currentDate))
+            ) {
+              logger.error(
+                `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring ${
+                  chargingProfile.recurrencyKind
+                } charging profile id ${
+                  chargingProfile.chargingProfileId
+                } startSchedule ${chargingSchedule.startSchedule.toISOString()} is not properly translated to the current day`,
+              );
+            }
+          }
+          break;
+        case RecurrencyKindType.WEEKLY:
+          if (isBefore(chargingSchedule.startSchedule, startOfWeek(currentDate))) {
+            addWeeks(
+              chargingSchedule.startSchedule,
+              differenceInWeeks(chargingSchedule.startSchedule, endOfWeek(currentDate)),
+            );
+            if (
+              isBefore(chargingSchedule.startSchedule, startOfWeek(currentDate)) ||
+              isAfter(chargingSchedule.startSchedule, endOfWeek(currentDate))
+            ) {
+              logger.error(
+                `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring ${
+                  chargingProfile.recurrencyKind
+                } charging profile id ${
+                  chargingProfile.chargingProfileId
+                } startSchedule ${chargingSchedule.startSchedule.toISOString()} is not properly translated to the current week`,
+              );
+            }
+          }
+          break;
       }
-    } else if (moment(chargingSchedule.startSchedule).isAfter(currentMoment)) {
-      return null;
+    } else if (chargingProfile.chargingProfileKind === ChargingProfileKindType.RELATIVE) {
+      chargingSchedule.startSchedule =
+        chargingStation.getConnectorStatus(connectorId)?.transactionStart;
     }
     // 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;
+      let lastButOneSchedule: ChargingSchedulePeriod | undefined;
       // Search the right schedule period
       for (const schedulePeriod of chargingSchedule.chargingSchedulePeriod) {
         // Handling of only one period
@@ -717,13 +771,14 @@ const getLimitFromChargingProfiles = (
         }
         // 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 = {
-            limit: lastButOneSchedule.limit,
+            limit: lastButOneSchedule!.limit,
             matchingChargingProfile: chargingProfile,
           };
           logger.debug(debugLogMsg, result);
@@ -748,7 +803,6 @@ const getLimitFromChargingProfiles = (
       }
     }
   }
-  return null;
 };
 
 const getRandomSerialNumberSuffix = (params?: {