Fix not initialized variables at startup
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStation.ts
index 82db1976e13a1d7c05509ab12abf78bce73a877f..89bb9d8ef40ac3819eef213f2ac3d657415574c6 100644 (file)
@@ -21,18 +21,9 @@ import {
   Response,
   StatusNotificationResponse,
 } from '../types/ocpp/Responses';
-import {
-  ChargingProfile,
-  ChargingRateUnitType,
-  ChargingSchedulePeriod,
-} from '../types/ocpp/ChargingProfile';
-import { ChargingProfileKindType, RecurrencyKindType } from '../types/ocpp/1.6/ChargingProfile';
+import { ChargingProfile, ChargingRateUnitType } from '../types/ocpp/ChargingProfile';
 import ChargingStationConfiguration, { Section } from '../types/ChargingStationConfiguration';
-import ChargingStationOcppConfiguration, {
-  ConfigurationKey,
-} from '../types/ChargingStationOcppConfiguration';
 import ChargingStationTemplate, {
-  AmpereUnits,
   CurrentType,
   PowerUnits,
   Voltage,
@@ -57,7 +48,10 @@ import AutomaticTransactionGenerator from './AutomaticTransactionGenerator';
 import BaseError from '../exception/BaseError';
 import { ChargePointErrorCode } from '../types/ocpp/ChargePointErrorCode';
 import { ChargePointStatus } from '../types/ocpp/ChargePointStatus';
+import { ChargingStationConfigurationUtils } from './ChargingStationConfigurationUtils';
 import ChargingStationInfo from '../types/ChargingStationInfo';
+import ChargingStationOcppConfiguration from '../types/ChargingStationOcppConfiguration';
+import { ChargingStationUtils } from './ChargingStationUtils';
 import { ChargingStationWorkerMessageEvents } from '../types/ChargingStationWorker';
 import Configuration from '../utils/Configuration';
 import { ConnectorStatus } from '../types/ConnectorStatus';
@@ -83,7 +77,6 @@ import Utils from '../utils/Utils';
 import crypto from 'crypto';
 import fs from 'fs';
 import logger from '../utils/Logger';
-import moment from 'moment';
 import { parentPort } from 'worker_threads';
 import path from 'path';
 
@@ -129,7 +122,10 @@ export default class ChargingStation {
   private get wsConnectionUrl(): URL {
     return this.getSupervisionUrlOcppConfiguration()
       ? new URL(
-          this.getConfigurationKey(this.getSupervisionUrlOcppKey()).value +
+          ChargingStationConfigurationUtils.getConfigurationKey(
+            this,
+            this.getSupervisionUrlOcppKey()
+          ).value +
             '/' +
             this.stationInfo.chargingStationId
         )
@@ -306,6 +302,10 @@ export default class ChargingStation {
     return this.stationInfo.phaseLineToLineVoltageMeterValues ?? false;
   }
 
+  public getCustomValueLimitationMeterValues(): boolean {
+    return this.stationInfo.customValueLimitationMeterValues ?? true;
+  }
+
   public getConnectorIdByTransactionId(transactionId: number): number | undefined {
     for (const connectorId of this.connectors.keys()) {
       if (
@@ -336,7 +336,8 @@ export default class ChargingStation {
   }
 
   public getAuthorizeRemoteTxRequests(): boolean {
-    const authorizeRemoteTxRequests = this.getConfigurationKey(
+    const authorizeRemoteTxRequests = ChargingStationConfigurationUtils.getConfigurationKey(
+      this,
       StandardParametersKey.AuthorizeRemoteTxRequests
     );
     return authorizeRemoteTxRequests
@@ -345,19 +346,13 @@ export default class ChargingStation {
   }
 
   public getLocalAuthListEnabled(): boolean {
-    const localAuthListEnabled = this.getConfigurationKey(
+    const localAuthListEnabled = ChargingStationConfigurationUtils.getConfigurationKey(
+      this,
       StandardParametersKey.LocalAuthListEnabled
     );
     return localAuthListEnabled ? Utils.convertToBoolean(localAuthListEnabled.value) : false;
   }
 
-  public restartWebSocketPing(): void {
-    // Stop WebSocket ping
-    this.stopWebSocketPing();
-    // Start WebSocket ping
-    this.startWebSocketPing();
-  }
-
   public getSampledValueTemplate(
     connectorId: number,
     measurand: MeterValueMeasurand = MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER,
@@ -372,9 +367,10 @@ export default class ChargingStation {
     }
     if (
       measurand !== MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER &&
-      !this.getConfigurationKey(StandardParametersKey.MeterValuesSampledData)?.value.includes(
-        measurand
-      )
+      !ChargingStationConfigurationUtils.getConfigurationKey(
+        this,
+        StandardParametersKey.MeterValuesSampledData
+      )?.value.includes(measurand)
     ) {
       logger.debug(
         `${this.logPrefix()} Trying to get MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId} not found in '${
@@ -403,18 +399,20 @@ export default class ChargingStation {
         phase &&
         sampledValueTemplates[index]?.phase === phase &&
         sampledValueTemplates[index]?.measurand === measurand &&
-        this.getConfigurationKey(StandardParametersKey.MeterValuesSampledData)?.value.includes(
-          measurand
-        )
+        ChargingStationConfigurationUtils.getConfigurationKey(
+          this,
+          StandardParametersKey.MeterValuesSampledData
+        )?.value.includes(measurand)
       ) {
         return sampledValueTemplates[index];
       } else if (
         !phase &&
         !sampledValueTemplates[index].phase &&
         sampledValueTemplates[index]?.measurand === measurand &&
-        this.getConfigurationKey(StandardParametersKey.MeterValuesSampledData)?.value.includes(
-          measurand
-        )
+        ChargingStationConfigurationUtils.getConfigurationKey(
+          this,
+          StandardParametersKey.MeterValuesSampledData
+        )?.value.includes(measurand)
       ) {
         return sampledValueTemplates[index];
       } else if (
@@ -448,6 +446,7 @@ export default class ChargingStation {
       // eslint-disable-next-line @typescript-eslint/no-misused-promises
       this.heartbeatSetInterval = setInterval(async (): Promise<void> => {
         await this.ocppRequestService.requestHandler<HeartbeatRequest, HeartbeatResponse>(
+          this,
           RequestCommand.HEARTBEAT
         );
       }, this.getHeartbeatInterval());
@@ -480,6 +479,13 @@ export default class ChargingStation {
     this.startHeartbeat();
   }
 
+  public restartWebSocketPing(): void {
+    // Stop WebSocket ping
+    this.stopWebSocketPing();
+    // Start WebSocket ping
+    this.startWebSocketPing();
+  }
+
   public startMeterValues(connectorId: number, interval: number): void {
     if (connectorId === 0) {
       logger.error(
@@ -520,6 +526,7 @@ export default class ChargingStation {
             interval
           );
           await this.ocppRequestService.requestHandler<MeterValuesRequest, MeterValuesResponse>(
+            this,
             RequestCommand.METER_VALUES,
             {
               connectorId,
@@ -627,7 +634,7 @@ export default class ChargingStation {
         await this.ocppRequestService.requestHandler<
           StatusNotificationRequest,
           StatusNotificationResponse
-        >(RequestCommand.STATUS_NOTIFICATION, {
+        >(this, RequestCommand.STATUS_NOTIFICATION, {
           connectorId,
           status: ChargePointStatus.UNAVAILABLE,
           errorCode: ChargePointErrorCode.NO_ERROR,
@@ -657,93 +664,16 @@ export default class ChargingStation {
     this.start();
   }
 
-  public getConfigurationKey(
-    key: string | StandardParametersKey,
-    caseInsensitive = false
-  ): ConfigurationKey | undefined {
-    return this.ocppConfiguration.configurationKey.find((configElement) => {
-      if (caseInsensitive) {
-        return configElement.key.toLowerCase() === key.toLowerCase();
-      }
-      return configElement.key === key;
-    });
-  }
-
-  public addConfigurationKey(
-    key: string | StandardParametersKey,
-    value: string,
-    options: { readonly?: boolean; visible?: boolean; reboot?: boolean } = {
-      readonly: false,
-      visible: true,
-      reboot: false,
-    },
-    params: { overwrite?: boolean; save?: boolean } = { overwrite: false, save: false }
-  ): void {
-    options = options ?? ({} as { readonly?: boolean; visible?: boolean; reboot?: boolean });
-    options.readonly = options?.readonly ?? false;
-    options.visible = options?.visible ?? true;
-    options.reboot = options?.reboot ?? false;
-    let keyFound = this.getConfigurationKey(key);
-    if (keyFound && params?.overwrite) {
-      this.deleteConfigurationKey(keyFound.key, { save: false });
-      keyFound = undefined;
-    }
-    if (!keyFound) {
-      this.ocppConfiguration.configurationKey.push({
-        key,
-        readonly: options.readonly,
-        value,
-        visible: options.visible,
-        reboot: options.reboot,
-      });
-      params?.save && this.saveOcppConfiguration();
-    } else {
-      logger.error(
-        `${this.logPrefix()} Trying to add an already existing configuration key: %j`,
-        keyFound
-      );
-    }
-  }
-
-  public setConfigurationKeyValue(
-    key: string | StandardParametersKey,
-    value: string,
-    caseInsensitive = false
-  ): void {
-    const keyFound = this.getConfigurationKey(key, caseInsensitive);
-    if (keyFound) {
-      this.ocppConfiguration.configurationKey[
-        this.ocppConfiguration.configurationKey.indexOf(keyFound)
-      ].value = value;
-      this.saveOcppConfiguration();
-    } else {
-      logger.error(
-        `${this.logPrefix()} Trying to set a value on a non existing configuration key: %j`,
-        { key, value }
-      );
-    }
-  }
-
-  public deleteConfigurationKey(
-    key: string | StandardParametersKey,
-    params: { save?: boolean; caseInsensitive?: boolean } = { save: true, caseInsensitive: false }
-  ): ConfigurationKey[] {
-    const keyFound = this.getConfigurationKey(key, params?.caseInsensitive);
-    if (keyFound) {
-      const deletedConfigurationKey = this.ocppConfiguration.configurationKey.splice(
-        this.ocppConfiguration.configurationKey.indexOf(keyFound),
-        1
-      );
-      params?.save && this.saveOcppConfiguration();
-      return deletedConfigurationKey;
+  public saveOcppConfiguration(): void {
+    if (this.getOcppPersistentConfiguration()) {
+      this.saveConfiguration(Section.ocppConfiguration);
     }
   }
 
   public getChargingProfilePowerLimit(connectorId: number): number | undefined {
-    let limit: number;
-    let matchingChargingProfile: ChargingProfile;
+    let limit: number, matchingChargingProfile: ChargingProfile;
     let chargingProfiles: ChargingProfile[] = [];
-    // Get profiles for connector and sort by stack level
+    // Get charging profiles for connector and sort by stack level
     chargingProfiles = this.getConnectorStatus(connectorId).chargingProfiles.sort(
       (a, b) => b.stackLevel - a.stackLevel
     );
@@ -754,7 +684,10 @@ export default class ChargingStation {
       );
     }
     if (!Utils.isEmptyArray(chargingProfiles)) {
-      const result = this.getCurrentLimitFromOCPPProfiles(chargingProfiles);
+      const result = ChargingStationUtils.getLimitFromChargingProfiles(
+        chargingProfiles,
+        Utils.logPrefix()
+      );
       if (!Utils.isNullOrUndefined(result)) {
         limit = result.limit;
         matchingChargingProfile = result.matchingChargingProfile;
@@ -790,8 +723,16 @@ export default class ChargingStation {
   }
 
   public setChargingProfile(connectorId: number, cp: ChargingProfile): void {
-    // Add support for connectorID 0
+    if (Utils.isNullOrUndefined(this.getConnectorStatus(connectorId).chargingProfiles)) {
+      logger.error(
+        `${this.logPrefix()} Trying to set a charging profile on connectorId ${connectorId} with an uninitialized charging profiles array attribute, applying deferred initialization`
+      );
+      this.getConnectorStatus(connectorId).chargingProfiles = [];
+    }
     if (!Array.isArray(this.getConnectorStatus(connectorId).chargingProfiles)) {
+      logger.error(
+        `${this.logPrefix()} Trying to set a charging profile on connectorId ${connectorId} with an improper attribute type for the charging profiles array, applying proper type initialization`
+      );
       this.getConnectorStatus(connectorId).chargingProfiles = [];
     }
     let cpReplaced = false;
@@ -827,9 +768,10 @@ export default class ChargingStation {
   }
 
   public hasFeatureProfile(featureProfile: SupportedFeatureProfiles) {
-    return this.getConfigurationKey(StandardParametersKey.SupportedFeatureProfiles)?.value.includes(
-      featureProfile
-    );
+    return ChargingStationConfigurationUtils.getConfigurationKey(
+      this,
+      StandardParametersKey.SupportedFeatureProfiles
+    )?.value.includes(featureProfile);
   }
 
   public bufferMessage(message: string): void {
@@ -854,33 +796,6 @@ export default class ChargingStation {
     return this.stationInfo.supervisionUrlOcppKey ?? VendorDefaultParametersKey.ConnectionUrl;
   }
 
-  private 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 idSuffix = stationTemplate.nameSuffix ?? '';
-    const idStr = '000000000' + this.index.toString();
-    return stationTemplate.fixedName
-      ? stationTemplate.baseName
-      : stationTemplate.baseName +
-          '-' +
-          instanceIndex.toString() +
-          idStr.substring(idStr.length - 4) +
-          idSuffix;
-  }
-
-  private getRandomSerialNumberSuffix(params?: {
-    randomBytesLength?: number;
-    upperCase?: boolean;
-  }): string {
-    const randomSerialNumberSuffix = crypto
-      .randomBytes(params?.randomBytesLength ?? 16)
-      .toString('hex');
-    if (params?.upperCase) {
-      return randomSerialNumberSuffix.toUpperCase();
-    }
-    return randomSerialNumberSuffix;
-  }
-
   private getTemplateFromFile(): ChargingStationTemplate | null {
     let template: ChargingStationTemplate = null;
     try {
@@ -905,40 +820,6 @@ export default class ChargingStation {
     return template;
   }
 
-  private createSerialNumber(
-    stationInfo: ChargingStationInfo,
-    existingStationInfo?: ChargingStationInfo,
-    params: { randomSerialNumberUpperCase?: boolean; randomSerialNumber?: boolean } = {
-      randomSerialNumberUpperCase: true,
-      randomSerialNumber: true,
-    }
-  ): void {
-    params = params ?? {};
-    params.randomSerialNumberUpperCase = params?.randomSerialNumberUpperCase ?? true;
-    params.randomSerialNumber = params?.randomSerialNumber ?? true;
-    if (!Utils.isEmptyObject(existingStationInfo)) {
-      existingStationInfo?.chargePointSerialNumber &&
-        (stationInfo.chargePointSerialNumber = existingStationInfo.chargePointSerialNumber);
-      existingStationInfo?.chargeBoxSerialNumber &&
-        (stationInfo.chargeBoxSerialNumber = existingStationInfo.chargeBoxSerialNumber);
-      existingStationInfo?.meterSerialNumber &&
-        (stationInfo.meterSerialNumber = existingStationInfo.meterSerialNumber);
-    } else {
-      const serialNumberSuffix = params?.randomSerialNumber
-        ? this.getRandomSerialNumberSuffix({ upperCase: params.randomSerialNumberUpperCase })
-        : '';
-      stationInfo.chargePointSerialNumber =
-        stationInfo?.chargePointSerialNumberPrefix &&
-        stationInfo.chargePointSerialNumberPrefix + serialNumberSuffix;
-      stationInfo.chargeBoxSerialNumber =
-        stationInfo?.chargeBoxSerialNumberPrefix &&
-        stationInfo.chargeBoxSerialNumberPrefix + serialNumberSuffix;
-      stationInfo.meterSerialNumber =
-        stationInfo?.meterSerialNumberPrefix &&
-        stationInfo.meterSerialNumberPrefix + serialNumberSuffix;
-    }
-  }
-
   private getStationInfoFromTemplate(): ChargingStationInfo {
     const stationInfo: ChargingStationInfo = this.getTemplateFromFile();
     if (Utils.isNullOrUndefined(stationInfo)) {
@@ -953,15 +834,20 @@ export default class ChargingStation {
         }`
       );
     }
-    const chargingStationId = this.getChargingStationId(stationInfo);
+    const chargingStationId = ChargingStationUtils.getChargingStationId(this.index, stationInfo);
     // Deprecation template keys section
-    this.warnDeprecatedTemplateKey(
+    ChargingStationUtils.warnDeprecatedTemplateKey(
       stationInfo,
       'supervisionUrl',
-      chargingStationId,
+      this.templateFile,
+      Utils.logPrefix(` ${chargingStationId} |`),
       "Use 'supervisionUrls' instead"
     );
-    this.convertDeprecatedTemplateKey(stationInfo, 'supervisionUrl', 'supervisionUrls');
+    ChargingStationUtils.convertDeprecatedTemplateKey(
+      stationInfo,
+      'supervisionUrl',
+      'supervisionUrls'
+    );
     stationInfo.wsOptions = stationInfo?.wsOptions ?? {};
     if (!Utils.isEmptyArray(stationInfo.power)) {
       stationInfo.power = stationInfo.power as number[];
@@ -986,35 +872,15 @@ export default class ChargingStation {
     return stationInfo;
   }
 
-  private createStationInfoHash(stationInfo: ChargingStationInfo): ChargingStationInfo {
-    if (!Utils.isEmptyObject(stationInfo)) {
-      const previousInfoHash = stationInfo?.infoHash ?? '';
-      delete stationInfo.infoHash;
-      const currentInfoHash = crypto
-        .createHash(Constants.DEFAULT_HASH_ALGORITHM)
-        .update(JSON.stringify(stationInfo))
-        .digest('hex');
-      if (
-        Utils.isEmptyString(previousInfoHash) ||
-        (!Utils.isEmptyString(previousInfoHash) && currentInfoHash !== previousInfoHash)
-      ) {
-        stationInfo.infoHash = currentInfoHash;
-      } else {
-        stationInfo.infoHash = previousInfoHash;
-      }
-    }
-    return stationInfo;
-  }
-
   private getStationInfoFromFile(): ChargingStationInfo {
     let stationInfo = this.getConfigurationFromFile()?.stationInfo ?? ({} as ChargingStationInfo);
-    stationInfo = this.createStationInfoHash(stationInfo);
+    stationInfo = ChargingStationUtils.createStationInfoHash(stationInfo);
     return stationInfo;
   }
 
   private getStationInfo(): ChargingStationInfo {
     const stationInfoFromTemplate: ChargingStationInfo = this.getStationInfoFromTemplate();
-    this.hashId = this.getHashId(stationInfoFromTemplate);
+    this.hashId = ChargingStationUtils.getHashId(stationInfoFromTemplate);
     this.configurationFile = path.join(
       path.resolve(__dirname, '../'),
       'assets',
@@ -1029,7 +895,7 @@ export default class ChargingStation {
       }
       return stationInfoFromFile;
     }
-    this.createSerialNumber(stationInfoFromTemplate, stationInfoFromFile);
+    ChargingStationUtils.createSerialNumber(stationInfoFromTemplate, stationInfoFromFile);
     return stationInfoFromTemplate;
   }
 
@@ -1053,62 +919,12 @@ export default class ChargingStation {
     throw new Error(errMsg);
   }
 
-  private createBootNotificationRequest(stationInfo: ChargingStationInfo): BootNotificationRequest {
-    return {
-      chargePointModel: stationInfo.chargePointModel,
-      chargePointVendor: stationInfo.chargePointVendor,
-      ...(!Utils.isUndefined(stationInfo.chargeBoxSerialNumber) && {
-        chargeBoxSerialNumber: stationInfo.chargeBoxSerialNumber,
-      }),
-      ...(!Utils.isUndefined(stationInfo.chargePointSerialNumber) && {
-        chargePointSerialNumber: stationInfo.chargePointSerialNumber,
-      }),
-      ...(!Utils.isUndefined(stationInfo.firmwareVersion) && {
-        firmwareVersion: stationInfo.firmwareVersion,
-      }),
-      ...(!Utils.isUndefined(stationInfo.iccid) && { iccid: stationInfo.iccid }),
-      ...(!Utils.isUndefined(stationInfo.imsi) && { imsi: stationInfo.imsi }),
-      ...(!Utils.isUndefined(stationInfo.meterSerialNumber) && {
-        meterSerialNumber: stationInfo.meterSerialNumber,
-      }),
-      ...(!Utils.isUndefined(stationInfo.meterType) && {
-        meterType: stationInfo.meterType,
-      }),
-    };
-  }
-
-  private getHashId(stationInfo: ChargingStationInfo): string {
-    const hashBootNotificationRequest = {
-      chargePointModel: stationInfo.chargePointModel,
-      chargePointVendor: stationInfo.chargePointVendor,
-      ...(!Utils.isUndefined(stationInfo.chargeBoxSerialNumberPrefix) && {
-        chargeBoxSerialNumber: stationInfo.chargeBoxSerialNumberPrefix,
-      }),
-      ...(!Utils.isUndefined(stationInfo.chargePointSerialNumberPrefix) && {
-        chargePointSerialNumber: stationInfo.chargePointSerialNumberPrefix,
-      }),
-      ...(!Utils.isUndefined(stationInfo.firmwareVersion) && {
-        firmwareVersion: stationInfo.firmwareVersion,
-      }),
-      ...(!Utils.isUndefined(stationInfo.iccid) && { iccid: stationInfo.iccid }),
-      ...(!Utils.isUndefined(stationInfo.imsi) && { imsi: stationInfo.imsi }),
-      ...(!Utils.isUndefined(stationInfo.meterSerialNumberPrefix) && {
-        meterSerialNumber: stationInfo.meterSerialNumberPrefix,
-      }),
-      ...(!Utils.isUndefined(stationInfo.meterType) && {
-        meterType: stationInfo.meterType,
-      }),
-    };
-    return crypto
-      .createHash(Constants.DEFAULT_HASH_ALGORITHM)
-      .update(JSON.stringify(hashBootNotificationRequest) + stationInfo.chargingStationId)
-      .digest('hex');
-  }
-
   private initialize(): void {
     this.stationInfo = this.getStationInfo();
     logger.info(`${this.logPrefix()} Charging station hashId '${this.hashId}'`);
-    this.bootNotificationRequest = this.createBootNotificationRequest(this.stationInfo);
+    this.bootNotificationRequest = ChargingStationUtils.createBootNotificationRequest(
+      this.stationInfo
+    );
     this.ocppConfiguration = this.getOcppConfiguration();
     this.stationInfo?.Configuration && delete this.stationInfo.Configuration;
     this.wsConfiguredConnectionUrl = new URL(
@@ -1133,7 +949,7 @@ export default class ChargingStation {
     }
     this.initializeConnectors(this.stationInfo, maxConnectors, templateMaxConnectors);
     this.stationInfo.maximumAmperage = this.getMaximumAmperage();
-    this.stationInfo = this.createStationInfoHash(this.stationInfo);
+    this.stationInfo = ChargingStationUtils.createStationInfoHash(this.stationInfo);
     this.saveStationInfo();
     // Avoid duplication of connectors related information in RAM
     this.stationInfo?.Connectors && delete this.stationInfo.Connectors;
@@ -1149,10 +965,9 @@ export default class ChargingStation {
     switch (this.getOcppVersion()) {
       case OCPPVersion.VERSION_16:
         this.ocppIncomingRequestService =
-          OCPP16IncomingRequestService.getInstance<OCPP16IncomingRequestService>(this);
+          OCPP16IncomingRequestService.getInstance<OCPP16IncomingRequestService>();
         this.ocppRequestService = OCPP16RequestService.getInstance<OCPP16RequestService>(
-          this,
-          OCPP16ResponseService.getInstance<OCPP16ResponseService>(this)
+          OCPP16ResponseService.getInstance<OCPP16ResponseService>()
         );
         break;
       default:
@@ -1170,55 +985,104 @@ export default class ChargingStation {
   }
 
   private initializeOcppConfiguration(): void {
-    if (!this.getConfigurationKey(StandardParametersKey.HeartbeatInterval)) {
-      this.addConfigurationKey(StandardParametersKey.HeartbeatInterval, '0');
+    if (
+      !ChargingStationConfigurationUtils.getConfigurationKey(
+        this,
+        StandardParametersKey.HeartbeatInterval
+      )
+    ) {
+      ChargingStationConfigurationUtils.addConfigurationKey(
+        this,
+        StandardParametersKey.HeartbeatInterval,
+        '0'
+      );
     }
-    if (!this.getConfigurationKey(StandardParametersKey.HeartBeatInterval)) {
-      this.addConfigurationKey(StandardParametersKey.HeartBeatInterval, '0', { visible: false });
+    if (
+      !ChargingStationConfigurationUtils.getConfigurationKey(
+        this,
+        StandardParametersKey.HeartBeatInterval
+      )
+    ) {
+      ChargingStationConfigurationUtils.addConfigurationKey(
+        this,
+        StandardParametersKey.HeartBeatInterval,
+        '0',
+        { visible: false }
+      );
     }
     if (
       this.getSupervisionUrlOcppConfiguration() &&
-      !this.getConfigurationKey(this.getSupervisionUrlOcppKey())
+      !ChargingStationConfigurationUtils.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
     ) {
-      this.addConfigurationKey(
+      ChargingStationConfigurationUtils.addConfigurationKey(
+        this,
         this.getSupervisionUrlOcppKey(),
         this.getConfiguredSupervisionUrl().href,
         { reboot: true }
       );
     } else if (
       !this.getSupervisionUrlOcppConfiguration() &&
-      this.getConfigurationKey(this.getSupervisionUrlOcppKey())
+      ChargingStationConfigurationUtils.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
     ) {
-      this.deleteConfigurationKey(this.getSupervisionUrlOcppKey(), { save: false });
+      ChargingStationConfigurationUtils.deleteConfigurationKey(
+        this,
+        this.getSupervisionUrlOcppKey(),
+        { save: false }
+      );
     }
     if (
       this.stationInfo.amperageLimitationOcppKey &&
-      !this.getConfigurationKey(this.stationInfo.amperageLimitationOcppKey)
+      !ChargingStationConfigurationUtils.getConfigurationKey(
+        this,
+        this.stationInfo.amperageLimitationOcppKey
+      )
     ) {
-      this.addConfigurationKey(
+      ChargingStationConfigurationUtils.addConfigurationKey(
+        this,
         this.stationInfo.amperageLimitationOcppKey,
-        (this.stationInfo.maximumAmperage * this.getAmperageLimitationUnitDivider()).toString()
+        (
+          this.stationInfo.maximumAmperage *
+          ChargingStationUtils.getAmperageLimitationUnitDivider(this.stationInfo)
+        ).toString()
       );
     }
-    if (!this.getConfigurationKey(StandardParametersKey.SupportedFeatureProfiles)) {
-      this.addConfigurationKey(
+    if (
+      !ChargingStationConfigurationUtils.getConfigurationKey(
+        this,
+        StandardParametersKey.SupportedFeatureProfiles
+      )
+    ) {
+      ChargingStationConfigurationUtils.addConfigurationKey(
+        this,
         StandardParametersKey.SupportedFeatureProfiles,
         `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`
       );
     }
-    this.addConfigurationKey(
+    ChargingStationConfigurationUtils.addConfigurationKey(
+      this,
       StandardParametersKey.NumberOfConnectors,
       this.getNumberOfConnectors().toString(),
       { readonly: true },
       { overwrite: true }
     );
-    if (!this.getConfigurationKey(StandardParametersKey.MeterValuesSampledData)) {
-      this.addConfigurationKey(
+    if (
+      !ChargingStationConfigurationUtils.getConfigurationKey(
+        this,
+        StandardParametersKey.MeterValuesSampledData
+      )
+    ) {
+      ChargingStationConfigurationUtils.addConfigurationKey(
+        this,
         StandardParametersKey.MeterValuesSampledData,
         MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER
       );
     }
-    if (!this.getConfigurationKey(StandardParametersKey.ConnectorPhaseRotation)) {
+    if (
+      !ChargingStationConfigurationUtils.getConfigurationKey(
+        this,
+        StandardParametersKey.ConnectorPhaseRotation
+      )
+    ) {
       const connectorPhaseRotation = [];
       for (const connectorId of this.connectors.keys()) {
         // AC/DC
@@ -1233,24 +1097,48 @@ export default class ChargingStation {
           connectorPhaseRotation.push(`${connectorId}.${ConnectorPhaseRotation.RST}`);
         }
       }
-      this.addConfigurationKey(
+      ChargingStationConfigurationUtils.addConfigurationKey(
+        this,
         StandardParametersKey.ConnectorPhaseRotation,
         connectorPhaseRotation.toString()
       );
     }
-    if (!this.getConfigurationKey(StandardParametersKey.AuthorizeRemoteTxRequests)) {
-      this.addConfigurationKey(StandardParametersKey.AuthorizeRemoteTxRequests, 'true');
-    }
     if (
-      !this.getConfigurationKey(StandardParametersKey.LocalAuthListEnabled) &&
-      this.getConfigurationKey(StandardParametersKey.SupportedFeatureProfiles)?.value.includes(
-        SupportedFeatureProfiles.LocalAuthListManagement
+      !ChargingStationConfigurationUtils.getConfigurationKey(
+        this,
+        StandardParametersKey.AuthorizeRemoteTxRequests
       )
     ) {
-      this.addConfigurationKey(StandardParametersKey.LocalAuthListEnabled, 'false');
+      ChargingStationConfigurationUtils.addConfigurationKey(
+        this,
+        StandardParametersKey.AuthorizeRemoteTxRequests,
+        'true'
+      );
+    }
+    if (
+      !ChargingStationConfigurationUtils.getConfigurationKey(
+        this,
+        StandardParametersKey.LocalAuthListEnabled
+      ) &&
+      ChargingStationConfigurationUtils.getConfigurationKey(
+        this,
+        StandardParametersKey.SupportedFeatureProfiles
+      )?.value.includes(SupportedFeatureProfiles.LocalAuthListManagement)
+    ) {
+      ChargingStationConfigurationUtils.addConfigurationKey(
+        this,
+        StandardParametersKey.LocalAuthListEnabled,
+        'false'
+      );
     }
-    if (!this.getConfigurationKey(StandardParametersKey.ConnectionTimeOut)) {
-      this.addConfigurationKey(
+    if (
+      !ChargingStationConfigurationUtils.getConfigurationKey(
+        this,
+        StandardParametersKey.ConnectionTimeOut
+      )
+    ) {
+      ChargingStationConfigurationUtils.addConfigurationKey(
+        this,
         StandardParametersKey.ConnectionTimeOut,
         Constants.DEFAULT_CONNECTION_TIMEOUT.toString()
       );
@@ -1459,12 +1347,6 @@ export default class ChargingStation {
     return ocppConfiguration;
   }
 
-  private saveOcppConfiguration(): void {
-    if (this.getOcppPersistentConfiguration()) {
-      this.saveConfiguration(Section.ocppConfiguration);
-    }
-  }
-
   private async onOpen(): Promise<void> {
     if (this.isWebSocketConnectionOpened()) {
       logger.info(
@@ -1478,6 +1360,7 @@ export default class ChargingStation {
             BootNotificationRequest,
             BootNotificationResponse
           >(
+            this,
             RequestCommand.BOOT_NOTIFICATION,
             {
               chargePointModel: this.bootNotificationRequest.chargePointModel,
@@ -1532,7 +1415,7 @@ export default class ChargingStation {
       case WebSocketCloseEventStatusCode.CLOSE_NORMAL:
       case WebSocketCloseEventStatusCode.CLOSE_NO_STATUS:
         logger.info(
-          `${this.logPrefix()} WebSocket normally closed with status '${Utils.getWebSocketCloseEventStatusString(
+          `${this.logPrefix()} WebSocket normally closed with status '${ChargingStationUtils.getWebSocketCloseEventStatusString(
             code
           )}' and reason '${reason}'`
         );
@@ -1541,7 +1424,7 @@ export default class ChargingStation {
       // Abnormal close
       default:
         logger.error(
-          `${this.logPrefix()} WebSocket abnormally closed with status '${Utils.getWebSocketCloseEventStatusString(
+          `${this.logPrefix()} WebSocket abnormally closed with status '${ChargingStationUtils.getWebSocketCloseEventStatusString(
             code
           )}' and reason '${reason}'`
         );
@@ -1583,6 +1466,7 @@ export default class ChargingStation {
             );
             // Process the message
             await this.ocppIncomingRequestService.incomingRequestHandler(
+              this,
               messageId,
               commandName,
               commandPayload
@@ -1673,6 +1557,7 @@ export default class ChargingStation {
       // Send error
       messageType === MessageType.CALL_MESSAGE &&
         (await this.ocppRequestService.sendError(
+          this,
           messageId,
           error as OCPPError,
           commandName ?? requestCommandName ?? null
@@ -1744,10 +1629,19 @@ export default class ChargingStation {
 
   // 0 for disabling
   private getConnectionTimeout(): number | undefined {
-    if (this.getConfigurationKey(StandardParametersKey.ConnectionTimeOut)) {
+    if (
+      ChargingStationConfigurationUtils.getConfigurationKey(
+        this,
+        StandardParametersKey.ConnectionTimeOut
+      )
+    ) {
       return (
-        parseInt(this.getConfigurationKey(StandardParametersKey.ConnectionTimeOut).value) ??
-        Constants.DEFAULT_CONNECTION_TIMEOUT
+        parseInt(
+          ChargingStationConfigurationUtils.getConfigurationKey(
+            this,
+            StandardParametersKey.ConnectionTimeOut
+          ).value
+        ) ?? Constants.DEFAULT_CONNECTION_TIMEOUT
       );
     }
     return Constants.DEFAULT_CONNECTION_TIMEOUT;
@@ -1821,31 +1715,21 @@ export default class ChargingStation {
     }
   }
 
-  private getAmperageLimitationUnitDivider(): number {
-    let unitDivider = 1;
-    switch (this.stationInfo.amperageLimitationUnit) {
-      case AmpereUnits.DECI_AMPERE:
-        unitDivider = 10;
-        break;
-      case AmpereUnits.CENTI_AMPERE:
-        unitDivider = 100;
-        break;
-      case AmpereUnits.MILLI_AMPERE:
-        unitDivider = 1000;
-        break;
-    }
-    return unitDivider;
-  }
-
   private getAmperageLimitation(): number | undefined {
     if (
       this.stationInfo.amperageLimitationOcppKey &&
-      this.getConfigurationKey(this.stationInfo.amperageLimitationOcppKey)
+      ChargingStationConfigurationUtils.getConfigurationKey(
+        this,
+        this.stationInfo.amperageLimitationOcppKey
+      )
     ) {
       return (
         Utils.convertToInt(
-          this.getConfigurationKey(this.stationInfo.amperageLimitationOcppKey).value
-        ) / this.getAmperageLimitationUnitDivider()
+          ChargingStationConfigurationUtils.getConfigurationKey(
+            this,
+            this.stationInfo.amperageLimitationOcppKey
+          ).value
+        ) / ChargingStationUtils.getAmperageLimitationUnitDivider(this.stationInfo)
       );
     }
   }
@@ -1856,6 +1740,7 @@ export default class ChargingStation {
         BootNotificationRequest,
         BootNotificationResponse
       >(
+        this,
         RequestCommand.BOOT_NOTIFICATION,
         {
           chargePointModel: this.bootNotificationRequest.chargePointModel,
@@ -1888,7 +1773,7 @@ export default class ChargingStation {
         await this.ocppRequestService.requestHandler<
           StatusNotificationRequest,
           StatusNotificationResponse
-        >(RequestCommand.STATUS_NOTIFICATION, {
+        >(this, RequestCommand.STATUS_NOTIFICATION, {
           connectorId,
           status: this.getConnectorStatus(connectorId).bootStatus,
           errorCode: ChargePointErrorCode.NO_ERROR,
@@ -1904,7 +1789,7 @@ export default class ChargingStation {
         await this.ocppRequestService.requestHandler<
           StatusNotificationRequest,
           StatusNotificationResponse
-        >(RequestCommand.STATUS_NOTIFICATION, {
+        >(this, RequestCommand.STATUS_NOTIFICATION, {
           connectorId,
           status: this.getConnectorStatus(connectorId).bootStatus,
           errorCode: ChargePointErrorCode.NO_ERROR,
@@ -1916,7 +1801,7 @@ export default class ChargingStation {
         await this.ocppRequestService.requestHandler<
           StatusNotificationRequest,
           StatusNotificationResponse
-        >(RequestCommand.STATUS_NOTIFICATION, {
+        >(this, RequestCommand.STATUS_NOTIFICATION, {
           connectorId,
           status: this.getConnectorStatus(connectorId).status,
           errorCode: ChargePointErrorCode.NO_ERROR,
@@ -1926,7 +1811,7 @@ export default class ChargingStation {
         await this.ocppRequestService.requestHandler<
           StatusNotificationRequest,
           StatusNotificationResponse
-        >(RequestCommand.STATUS_NOTIFICATION, {
+        >(this, RequestCommand.STATUS_NOTIFICATION, {
           connectorId,
           status: ChargePointStatus.AVAILABLE,
           errorCode: ChargePointErrorCode.NO_ERROR,
@@ -1978,6 +1863,7 @@ export default class ChargingStation {
               this.getEnergyActiveImportRegisterByTransactionId(transactionId)
             );
             await this.ocppRequestService.requestHandler<MeterValuesRequest, MeterValuesResponse>(
+              this,
               RequestCommand.METER_VALUES,
               {
                 connectorId,
@@ -1989,7 +1875,7 @@ export default class ChargingStation {
           await this.ocppRequestService.requestHandler<
             StopTransactionRequest,
             StopTransactionResponse
-          >(RequestCommand.STOP_TRANSACTION, {
+          >(this, RequestCommand.STOP_TRANSACTION, {
             transactionId,
             meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId),
             idTag: this.getTransactionIdTag(transactionId),
@@ -2001,11 +1887,15 @@ export default class ChargingStation {
   }
 
   private startWebSocketPing(): void {
-    const webSocketPingInterval: number = this.getConfigurationKey(
+    const webSocketPingInterval: number = ChargingStationConfigurationUtils.getConfigurationKey(
+      this,
       StandardParametersKey.WebSocketPingInterval
     )
       ? Utils.convertToInt(
-          this.getConfigurationKey(StandardParametersKey.WebSocketPingInterval).value
+          ChargingStationConfigurationUtils.getConfigurationKey(
+            this,
+            StandardParametersKey.WebSocketPingInterval
+          ).value
         )
       : 0;
     if (webSocketPingInterval > 0 && !this.webSocketPingSetInterval) {
@@ -2045,33 +1935,6 @@ export default class ChargingStation {
     }
   }
 
-  private warnDeprecatedTemplateKey(
-    template: ChargingStationTemplate,
-    key: string,
-    chargingStationId: string,
-    logMsgToAppend = ''
-  ): void {
-    if (!Utils.isUndefined(template[key])) {
-      const logPrefixStr = ` ${chargingStationId} |`;
-      logger.warn(
-        `${Utils.logPrefix(logPrefixStr)} Deprecated template key '${key}' usage in file '${
-          this.templateFile
-        }'${logMsgToAppend && '. ' + logMsgToAppend}`
-      );
-    }
-  }
-
-  private convertDeprecatedTemplateKey(
-    template: ChargingStationTemplate,
-    deprecatedKey: string,
-    key: string
-  ): void {
-    if (!Utils.isUndefined(template[deprecatedKey])) {
-      template[key] = template[deprecatedKey] as unknown;
-      delete template[deprecatedKey];
-    }
-  }
-
   private getConfiguredSupervisionUrl(): URL {
     const supervisionUrls = Utils.cloneObject<string | string[]>(
       this.stationInfo.supervisionUrls ?? Configuration.getSupervisionUrls()
@@ -2110,11 +1973,17 @@ export default class ChargingStation {
   }
 
   private getHeartbeatInterval(): number | undefined {
-    const HeartbeatInterval = this.getConfigurationKey(StandardParametersKey.HeartbeatInterval);
+    const HeartbeatInterval = ChargingStationConfigurationUtils.getConfigurationKey(
+      this,
+      StandardParametersKey.HeartbeatInterval
+    );
     if (HeartbeatInterval) {
       return Utils.convertToInt(HeartbeatInterval.value) * 1000;
     }
-    const HeartBeatInterval = this.getConfigurationKey(StandardParametersKey.HeartBeatInterval);
+    const HeartBeatInterval = ChargingStationConfigurationUtils.getConfigurationKey(
+      this,
+      StandardParametersKey.HeartBeatInterval
+    );
     if (HeartBeatInterval) {
       return Utils.convertToInt(HeartBeatInterval.value) * 1000;
     }
@@ -2230,70 +2099,4 @@ export default class ChargingStation {
     this.getConnectorStatus(connectorId).energyActiveImportRegisterValue = 0;
     this.getConnectorStatus(connectorId).transactionEnergyActiveImportRegisterValue = 0;
   }
-
-  private getCurrentLimitFromOCPPProfiles(chargingProfiles: ChargingProfile[]): {
-    limit: number;
-    matchingChargingProfile: ChargingProfile;
-  } {
-    for (const chargingProfile of chargingProfiles) {
-      // Profiles should already be sorted by connectorID and Stack Level (highest stack level has prio)
-      // Set helpers
-      const now = moment();
-      const chargingSchedule = chargingProfile.chargingSchedule;
-      // 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 &&
-        now.isAfter(chargingSchedule.startSchedule)
-      ) {
-        const currentDate = new Date();
-        chargingSchedule.startSchedule = new Date(chargingSchedule.startSchedule);
-        chargingSchedule.startSchedule.setFullYear(
-          currentDate.getFullYear(),
-          currentDate.getMonth(),
-          currentDate.getDate()
-        );
-        // Check if the start of the schedule is yesterday
-        if (moment(chargingSchedule.startSchedule).isAfter(now)) {
-          chargingSchedule.startSchedule.setDate(currentDate.getDate() - 1);
-        }
-      } else if (moment(chargingSchedule.startSchedule).isAfter(now)) {
-        return null;
-      }
-      // Check if the Charging Profile is active
-      if (moment(chargingSchedule.startSchedule).add(chargingSchedule.duration, 's').isAfter(now)) {
-        let lastButOneSchedule: ChargingSchedulePeriod;
-        // Search the right Schedule Period
-        for (const schedulePeriod of chargingSchedule.chargingSchedulePeriod) {
-          // Handling of only one period
-          if (
-            chargingSchedule.chargingSchedulePeriod.length === 1 &&
-            schedulePeriod.startPeriod === 0
-          ) {
-            return { limit: schedulePeriod.limit, matchingChargingProfile: chargingProfile };
-          }
-          // Find the right Schedule Periods
-          if (
-            moment(chargingSchedule.startSchedule).add(schedulePeriod.startPeriod, 's').isAfter(now)
-          ) {
-            // Found the schedule: Last but one is the correct one
-            return { limit: lastButOneSchedule.limit, matchingChargingProfile: chargingProfile };
-          }
-          // Keep it
-          lastButOneSchedule = schedulePeriod;
-          // Handle the last schedule period
-          if (
-            schedulePeriod.startPeriod ===
-            chargingSchedule.chargingSchedulePeriod[
-              chargingSchedule.chargingSchedulePeriod.length - 1
-            ].startPeriod
-          ) {
-            return { limit: lastButOneSchedule.limit, matchingChargingProfile: chargingProfile };
-          }
-        }
-      }
-    }
-    return null;
-  }
 }