build(deps-dev): apply updates
[e-mobility-charging-stations-simulator.git] / src / charging-station / Helpers.ts
index 99a8f82cb5dca431de4f3c6f49ca044129f16c7e..492d26aa10dbeb5a5d028e1a7d6d111c291ac368 100644 (file)
@@ -6,6 +6,7 @@ import { fileURLToPath } from 'node:url';
 
 import chalk from 'chalk';
 import {
+  type Interval,
   addDays,
   addSeconds,
   addWeeks,
@@ -17,9 +18,9 @@ import {
   isDate,
   isPast,
   isWithinInterval,
-  maxTime,
   toDate,
 } from 'date-fns';
+import { maxTime } from 'date-fns/constants';
 
 import type { ChargingStation } from './ChargingStation';
 import { getConfigurationKey } from './ConfigurationKeyUtils';
@@ -33,6 +34,7 @@ import {
   ChargingProfileKindType,
   ChargingRateUnitType,
   type ChargingSchedulePeriod,
+  type ChargingStationConfiguration,
   type ChargingStationInfo,
   type ChargingStationTemplate,
   ChargingStationWorkerMessageEvents,
@@ -74,8 +76,11 @@ const moduleName = 'Helpers';
 
 export const getChargingStationId = (
   index: number,
-  stationTemplate: ChargingStationTemplate,
+  stationTemplate: ChargingStationTemplate | undefined,
 ): string => {
+  if (stationTemplate === undefined) {
+    return "Unknown 'chargingStationId'";
+  }
   // In case of multiple instances: add instance index to charging station id
   const instanceIndex = env.CF_INSTANCE_INDEX ?? 0;
   const idSuffix = stationTemplate?.nameSuffix ?? '';
@@ -175,9 +180,9 @@ export const getPhaseRotationValue = (
   } else if (connectorId > 0 && numberOfPhases === 0) {
     return `${connectorId}.${ConnectorPhaseRotation.NotApplicable}`;
     // AC
-  } else if (connectorId > 0 && numberOfPhases === 1) {
+  } else if (connectorId >= 0 && numberOfPhases === 1) {
     return `${connectorId}.${ConnectorPhaseRotation.NotApplicable}`;
-  } else if (connectorId > 0 && numberOfPhases === 3) {
+  } else if (connectorId >= 0 && numberOfPhases === 3) {
     return `${connectorId}.${ConnectorPhaseRotation.RST}`;
   }
 };
@@ -250,6 +255,23 @@ export const checkTemplate = (
   }
 };
 
+export const checkConfiguration = (
+  stationConfiguration: ChargingStationConfiguration | undefined,
+  logPrefix: string,
+  configurationFile: string,
+): void => {
+  if (isNullOrUndefined(stationConfiguration)) {
+    const errorMsg = `Failed to read charging station configuration file ${configurationFile}`;
+    logger.error(`${logPrefix} ${errorMsg}`);
+    throw new BaseError(errorMsg);
+  }
+  if (isEmptyObject(stationConfiguration!)) {
+    const errorMsg = `Empty charging station configuration from file ${configurationFile}`;
+    logger.error(`${logPrefix} ${errorMsg}`);
+    throw new BaseError(errorMsg);
+  }
+};
+
 export const checkConnectorsConfiguration = (
   stationTemplate: ChargingStationTemplate,
   logPrefix: string,
@@ -363,7 +385,7 @@ export const createBootNotificationRequest = (
   stationInfo: ChargingStationInfo,
   bootReason: BootReasonEnumType = BootReasonEnumType.PowerUp,
 ): BootNotificationRequest => {
-  const ocppVersion = stationInfo.ocppVersion ?? OCPPVersion.VERSION_16;
+  const ocppVersion = stationInfo.ocppVersion!;
   switch (ocppVersion) {
     case OCPPVersion.VERSION_16:
       return {
@@ -559,14 +581,14 @@ export const getChargingStationConnectorChargingProfilesPowerLimit = (
     if (!isNullOrUndefined(result)) {
       limit = result?.limit;
       chargingProfile = result?.chargingProfile;
-      switch (chargingStation.getCurrentOutType()) {
+      switch (chargingStation.stationInfo?.currentOutType) {
         case CurrentType.AC:
           limit =
             chargingProfile?.chargingSchedule?.chargingRateUnit === ChargingRateUnitType.WATT
               ? limit
               : ACElectricUtils.powerTotal(
                   chargingStation.getNumberOfPhases(),
-                  chargingStation.getVoltageOut(),
+                  chargingStation.stationInfo.voltageOut!,
                   limit!,
                 );
           break;
@@ -574,10 +596,10 @@ export const getChargingStationConnectorChargingProfilesPowerLimit = (
           limit =
             chargingProfile?.chargingSchedule?.chargingRateUnit === ChargingRateUnitType.WATT
               ? limit
-              : DCElectricUtils.power(chargingStation.getVoltageOut(), limit!);
+              : DCElectricUtils.power(chargingStation.stationInfo.voltageOut!, limit!);
       }
       const connectorMaximumPower =
-        chargingStation.getMaximumPower() / chargingStation.powerDivider;
+        chargingStation.stationInfo.maximumPower! / chargingStation.powerDivider;
       if (limit! > connectorMaximumPower) {
         logger.error(
           `${chargingStation.logPrefix()} ${moduleName}.getChargingStationConnectorChargingProfilesPowerLimit: Charging profile id ${chargingProfile?.chargingProfileId} limit ${limit} is greater than connector id ${connectorId} maximum ${connectorMaximumPower}: %j`,