perf(simulator): build once address for tags caches addressable indexes
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStationUtils.ts
index e73f4d50670628cd61dfc82a2604ef7f9ae2d9fe..ca9a93bce9a7c06f851cbea3e1af30c238931eb9 100644 (file)
@@ -1,34 +1,37 @@
 import crypto from 'node:crypto';
-import path from 'path';
-import { fileURLToPath } from 'url';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
 
 import moment from 'moment';
 
-import type ChargingStation from './ChargingStation';
-import BaseError from '../exception/BaseError';
-import type { ChargingStationInfo } from '../types/ChargingStationInfo';
+import type { ChargingStation } from './internal';
+import { BaseError } from '../exception';
 import {
   AmpereUnits,
+  type BootNotificationRequest,
+  BootReasonEnumType,
+  type ChargingProfile,
+  ChargingProfileKindType,
+  ChargingRateUnitType,
+  type ChargingSchedulePeriod,
+  type ChargingStationInfo,
   type ChargingStationTemplate,
   CurrentType,
+  type OCPP16BootNotificationRequest,
+  type OCPP20BootNotificationRequest,
+  OCPPVersion,
+  RecurrencyKindType,
   Voltage,
-} from '../types/ChargingStationTemplate';
-import { ChargingProfileKindType, RecurrencyKindType } from '../types/ocpp/1.6/ChargingProfile';
-import type { OCPP16BootNotificationRequest } from '../types/ocpp/1.6/Requests';
-import { BootReasonEnumType, type OCPP20BootNotificationRequest } from '../types/ocpp/2.0/Requests';
+} from '../types';
 import {
-  type ChargingProfile,
-  ChargingRateUnitType,
-  type ChargingSchedulePeriod,
-} from '../types/ocpp/ChargingProfile';
-import { OCPPVersion } from '../types/ocpp/OCPPVersion';
-import type { BootNotificationRequest } from '../types/ocpp/Requests';
-import { WorkerProcessType } from '../types/Worker';
-import Configuration from '../utils/Configuration';
-import Constants from '../utils/Constants';
-import { ACElectricUtils, DCElectricUtils } from '../utils/ElectricUtils';
-import logger from '../utils/Logger';
-import Utils from '../utils/Utils';
+  ACElectricUtils,
+  Configuration,
+  Constants,
+  DCElectricUtils,
+  Utils,
+  logger,
+} from '../utils';
+import { WorkerProcessType } from '../worker';
 
 const moduleName = 'ChargingStationUtils';
 
@@ -43,7 +46,7 @@ export class ChargingStationUtils {
   ): string {
     // In case of multiple instances: add instance index to charging station id
     const instanceIndex = process.env.CF_INSTANCE_INDEX ?? 0;
-    const idSuffix = stationTemplate.nameSuffix ?? '';
+    const idSuffix = stationTemplate?.nameSuffix ?? '';
     const idStr = `000000000${index.toString()}`;
     return stationTemplate?.fixedName
       ? stationTemplate.baseName
@@ -86,6 +89,14 @@ export class ChargingStationUtils {
       .digest('hex');
   }
 
+  public static checkChargingStation(chargingStation: ChargingStation, logPrefix: string): boolean {
+    if (chargingStation.started === false && chargingStation.starting === false) {
+      logger.warn(`${logPrefix} charging station is stopped, cannot proceed`);
+      return false;
+    }
+    return true;
+  }
+
   public static getTemplateMaxNumberOfConnectors(stationTemplate: ChargingStationTemplate): number {
     const templateConnectors = stationTemplate?.Connectors;
     if (!templateConnectors) {
@@ -112,7 +123,7 @@ export class ChargingStationUtils {
 
   public static getConfiguredNumberOfConnectors(stationTemplate: ChargingStationTemplate): number {
     let configuredMaxConnectors: number;
-    if (Utils.isEmptyArray(stationTemplate.numberOfConnectors) === false) {
+    if (Utils.isNotEmptyArray(stationTemplate.numberOfConnectors) === true) {
       const numberOfConnectors = stationTemplate.numberOfConnectors as number[];
       configuredMaxConnectors =
         numberOfConnectors[Math.floor(Utils.secureRandom() * numberOfConnectors.length)];
@@ -191,13 +202,13 @@ export class ChargingStationUtils {
   }
 
   public static workerPoolInUse(): boolean {
-    return [WorkerProcessType.DYNAMIC_POOL, WorkerProcessType.STATIC_POOL].includes(
+    return [WorkerProcessType.dynamicPool, WorkerProcessType.staticPool].includes(
       Configuration.getWorker().processType
     );
   }
 
   public static workerDynamicPoolInUse(): boolean {
-    return Configuration.getWorker().processType === WorkerProcessType.DYNAMIC_POOL;
+    return Configuration.getWorker().processType === WorkerProcessType.dynamicPool;
   }
 
   public static warnDeprecatedTemplateKey(
@@ -210,7 +221,7 @@ export class ChargingStationUtils {
     if (!Utils.isUndefined(template[key])) {
       logger.warn(
         `${logPrefix} Deprecated template key '${key}' usage in file '${templateFile}'${
-          !Utils.isEmptyString(logMsgToAppend) && `. ${logMsgToAppend}`
+          Utils.isNotEmptyString(logMsgToAppend) && `. ${logMsgToAppend}`
         }`
       );
     }
@@ -268,15 +279,19 @@ export class ChargingStationUtils {
           upperCase: params.randomSerialNumberUpperCase,
         })
       : '';
-    stationInfo.chargePointSerialNumber =
-      !Utils.isEmptyString(stationTemplate?.chargePointSerialNumberPrefix) &&
-      `${stationTemplate.chargePointSerialNumberPrefix}${serialNumberSuffix}`;
-    stationInfo.chargeBoxSerialNumber =
-      !Utils.isEmptyString(stationTemplate?.chargeBoxSerialNumberPrefix) &&
-      `${stationTemplate.chargeBoxSerialNumberPrefix}${serialNumberSuffix}`;
-    stationInfo.meterSerialNumber =
-      !Utils.isEmptyString(stationTemplate?.meterSerialNumberPrefix) &&
-      `${stationTemplate.meterSerialNumberPrefix}${serialNumberSuffix}`;
+    stationInfo.chargePointSerialNumber = Utils.isNotEmptyString(
+      stationTemplate?.chargePointSerialNumberPrefix
+    )
+      ? `${stationTemplate.chargePointSerialNumberPrefix}${serialNumberSuffix}`
+      : undefined;
+    stationInfo.chargeBoxSerialNumber = Utils.isNotEmptyString(
+      stationTemplate?.chargeBoxSerialNumberPrefix
+    )
+      ? `${stationTemplate.chargeBoxSerialNumberPrefix}${serialNumberSuffix}`
+      : undefined;
+    stationInfo.meterSerialNumber = Utils.isNotEmptyString(stationTemplate?.meterSerialNumberPrefix)
+      ? `${stationTemplate.meterSerialNumberPrefix}${serialNumberSuffix}`
+      : undefined;
   }
 
   public static propagateSerialNumber(
@@ -334,7 +349,7 @@ export class ChargingStationUtils {
           .chargingProfiles.sort((a, b) => b.stackLevel - a.stackLevel)
       );
     }
-    if (!Utils.isEmptyArray(chargingProfiles)) {
+    if (Utils.isNotEmptyArray(chargingProfiles)) {
       const result = ChargingStationUtils.getLimitFromChargingProfiles(
         chargingProfiles,
         chargingStation.logPrefix()