Merge dependabot/npm_and_yarn/poolifier-3.0.1 into combined-prs-branch
[e-mobility-charging-stations-simulator.git] / src / charging-station / Helpers.ts
index dd6bcee18fe5ccdf798e18e2df09a70ecebca3ff..99a8f82cb5dca431de4f3c6f49ca044129f16c7e 100644 (file)
@@ -1,6 +1,7 @@
 import { createHash, randomBytes } from 'node:crypto';
 import type { EventEmitter } from 'node:events';
 import { basename, dirname, join } from 'node:path';
+import { env } from 'node:process';
 import { fileURLToPath } from 'node:url';
 
 import chalk from 'chalk';
@@ -76,7 +77,7 @@ export const getChargingStationId = (
   stationTemplate: ChargingStationTemplate,
 ): string => {
   // In case of multiple instances: add instance index to charging station id
-  const instanceIndex = process.env.CF_INSTANCE_INDEX ?? 0;
+  const instanceIndex = env.CF_INSTANCE_INDEX ?? 0;
   const idSuffix = stationTemplate?.nameSuffix ?? '';
   const idStr = `000000000${index.toString()}`;
   return stationTemplate?.fixedName
@@ -262,7 +263,7 @@ export const checkConnectorsConfiguration = (
   checkConfiguredMaxConnectors(configuredMaxConnectors, logPrefix, templateFile);
   const templateMaxConnectors = getMaxNumberOfConnectors(stationTemplate.Connectors!);
   checkTemplateMaxConnectors(templateMaxConnectors, logPrefix, templateFile);
-  const templateMaxAvailableConnectors = stationTemplate.Connectors![0]
+  const templateMaxAvailableConnectors = stationTemplate.Connectors?.[0]
     ? templateMaxConnectors - 1
     : templateMaxConnectors;
   if (
@@ -339,6 +340,12 @@ export const initializeConnectorsMapStatus = (
 };
 
 export const resetConnectorStatus = (connectorStatus: ConnectorStatus): void => {
+  connectorStatus.chargingProfiles =
+    connectorStatus.transactionId && isNotEmptyArray(connectorStatus.chargingProfiles)
+      ? connectorStatus.chargingProfiles?.filter(
+          (chargingProfile) => chargingProfile.transactionId !== connectorStatus.transactionId,
+        )
+      : [];
   connectorStatus.idTagLocalAuthorized = false;
   connectorStatus.idTagAuthorized = false;
   connectorStatus.transactionRemoteStarted = false;
@@ -446,12 +453,9 @@ export const stationTemplateToStationInfo = (
 export const createSerialNumber = (
   stationTemplate: ChargingStationTemplate,
   stationInfo: ChargingStationInfo,
-  params: {
+  params?: {
     randomSerialNumberUpperCase?: boolean;
     randomSerialNumber?: boolean;
-  } = {
-    randomSerialNumberUpperCase: true,
-    randomSerialNumber: true,
   },
 ): void => {
   params = { ...{ randomSerialNumberUpperCase: true, randomSerialNumber: true }, ...params };
@@ -517,7 +521,7 @@ export const getAmperageLimitationUnitDivider = (stationInfo: ChargingStationInf
 
 /**
  * Gets the connector cloned charging profiles applying a power limitation
- * and sorted by connector id ascending then stack level descending
+ * and sorted by connector id descending then stack level descending
  *
  * @param chargingStation -
  * @param connectorId -
@@ -528,10 +532,10 @@ export const getConnectorChargingProfiles = (
   connectorId: number,
 ) => {
   return cloneObject<ChargingProfile[]>(
-    (chargingStation.getConnectorStatus(0)?.chargingProfiles ?? [])
+    (chargingStation.getConnectorStatus(connectorId)?.chargingProfiles ?? [])
       .sort((a, b) => b.stackLevel - a.stackLevel)
       .concat(
-        (chargingStation.getConnectorStatus(connectorId)?.chargingProfiles ?? []).sort(
+        (chargingStation.getConnectorStatus(0)?.chargingProfiles ?? []).sort(
           (a, b) => b.stackLevel - a.stackLevel,
         ),
       ),
@@ -642,7 +646,7 @@ const getConfiguredMaxNumberOfConnectors = (stationTemplate: ChargingStationTemp
   } else if (isUndefined(stationTemplate.numberOfConnectors) === false) {
     configuredMaxNumberOfConnectors = stationTemplate.numberOfConnectors as number;
   } else if (stationTemplate.Connectors && !stationTemplate.Evses) {
-    configuredMaxNumberOfConnectors = stationTemplate.Connectors[0]
+    configuredMaxNumberOfConnectors = stationTemplate.Connectors?.[0]
       ? getMaxNumberOfConnectors(stationTemplate.Connectors) - 1
       : getMaxNumberOfConnectors(stationTemplate.Connectors);
   } else if (stationTemplate.Evses && !stationTemplate.Connectors) {
@@ -706,12 +710,12 @@ const warnDeprecatedTemplateKey = (
   templateFile: string,
   logMsgToAppend = '',
 ): void => {
-  if (!isUndefined(template[key as keyof ChargingStationTemplate])) {
+  if (!isUndefined(template?.[key as keyof ChargingStationTemplate])) {
     const logMsg = `Deprecated template key '${key}' usage in file '${templateFile}'${
       isNotEmptyString(logMsgToAppend) ? `. ${logMsgToAppend}` : ''
     }`;
     logger.warn(`${logPrefix} ${logMsg}`);
-    console.warn(chalk.yellow(`${logMsg}`));
+    console.warn(`${chalk.green(logPrefix)} ${chalk.yellow(logMsg)}`);
   }
 };
 
@@ -720,7 +724,7 @@ const convertDeprecatedTemplateKey = (
   deprecatedKey: string,
   key?: string,
 ): void => {
-  if (!isUndefined(template[deprecatedKey as keyof ChargingStationTemplate])) {
+  if (!isUndefined(template?.[deprecatedKey as keyof ChargingStationTemplate])) {
     if (!isUndefined(key)) {
       (template as unknown as Record<string, unknown>)[key!] =
         template[deprecatedKey as keyof ChargingStationTemplate];
@@ -735,7 +739,7 @@ interface ChargingProfilesLimit {
 }
 
 /**
- * Charging profiles shall already be sorted by connector id ascending then stack level descending
+ * Charging profiles shall already be sorted by connector id descending then stack level descending
  *
  * @param chargingStation -
  * @param connectorId -
@@ -961,7 +965,10 @@ const canProceedRecurringChargingProfile = (
     );
     return false;
   }
-  if (isNullOrUndefined(chargingProfile.chargingSchedule.startSchedule)) {
+  if (
+    chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING &&
+    isNullOrUndefined(chargingProfile.chargingSchedule.startSchedule)
+  ) {
     logger.error(
       `${logPrefix} ${moduleName}.canProceedRecurringChargingProfile: Recurring charging profile id ${chargingProfile.chargingProfileId} has no startSchedule defined`,
     );