Reduce charging station instance memory footprint
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 17 May 2022 20:47:20 +0000 (22:47 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 17 May 2022 20:47:20 +0000 (22:47 +0200)
Factor out helpers in static methods

Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/charging-station/Bootstrap.ts
src/charging-station/ChargingStation.ts
src/charging-station/ChargingStationConfigurationUtils.ts [new file with mode: 0644]
src/charging-station/ChargingStationUtils.ts [new file with mode: 0644]
src/charging-station/ChargingStationWorker.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/utils/Utils.ts

index 1f5c898f914102a010688155120f6db6ab662d04..54fcb6dba0712bd75bf485ac5774c22ccb493fc2 100644 (file)
@@ -8,6 +8,7 @@ import {
 
 import { AbstractUIServer } from './ui-server/AbstractUIServer';
 import { ApplicationProtocol } from '../types/UIProtocol';
+import { ChargingStationUtils } from './ChargingStationUtils';
 import Configuration from '../utils/Configuration';
 import { StationTemplateUrl } from '../types/ConfigurationData';
 import Statistics from '../types/Statistics';
@@ -102,11 +103,13 @@ export default class Bootstrap {
               `Charging stations simulator ${
                 this.version
               } started with ${this.numberOfChargingStations.toString()} charging station(s) and ${
-                Utils.workerDynamicPoolInUse()
+                ChargingStationUtils.workerDynamicPoolInUse()
                   ? `${Configuration.getWorkerPoolMinSize().toString()}/`
                   : ''
               }${this.workerImplementation.size}${
-                Utils.workerPoolInUse() ? `/${Configuration.getWorkerPoolMaxSize().toString()}` : ''
+                ChargingStationUtils.workerPoolInUse()
+                  ? `/${Configuration.getWorkerPoolMaxSize().toString()}`
+                  : ''
               } worker(s) concurrently running in '${Configuration.getWorkerProcess()}' mode${
                 this.workerImplementation.maxElementsPerWorker
                   ? ` (${this.workerImplementation.maxElementsPerWorker} charging station(s) per worker)`
index cd84076e29e3bf54e133af5258741d583b2f68fa..2b0b071137ceccbb5825001e1118d6f72b2aa75f 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
         )
@@ -340,7 +336,8 @@ export default class ChargingStation {
   }
 
   public getAuthorizeRemoteTxRequests(): boolean {
-    const authorizeRemoteTxRequests = this.getConfigurationKey(
+    const authorizeRemoteTxRequests = ChargingStationConfigurationUtils.getConfigurationKey(
+      this,
       StandardParametersKey.AuthorizeRemoteTxRequests
     );
     return authorizeRemoteTxRequests
@@ -349,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,
@@ -376,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 '${
@@ -407,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 (
@@ -485,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(
@@ -663,85 +664,9 @@ 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);
     }
   }
 
@@ -759,7 +684,10 @@ export default class ChargingStation {
       );
     }
     if (!Utils.isEmptyArray(chargingProfiles)) {
-      const result = this.getLimitFromChargingProfiles(chargingProfiles);
+      const result = ChargingStationUtils.getLimitFromChargingProfiles(
+        chargingProfiles,
+        Utils.logPrefix()
+      );
       if (!Utils.isNullOrUndefined(result)) {
         limit = result.limit;
         matchingChargingProfile = result.matchingChargingProfile;
@@ -840,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 {
@@ -867,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 {
@@ -918,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)) {
@@ -966,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,
+      this.logPrefix(),
       "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[];
@@ -999,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',
@@ -1042,7 +895,7 @@ export default class ChargingStation {
       }
       return stationInfoFromFile;
     }
-    this.createSerialNumber(stationInfoFromTemplate, stationInfoFromFile);
+    ChargingStationUtils.createSerialNumber(stationInfoFromTemplate, stationInfoFromFile);
     return stationInfoFromTemplate;
   }
 
@@ -1066,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(
@@ -1146,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;
@@ -1182,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
@@ -1245,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 (!this.getConfigurationKey(StandardParametersKey.ConnectionTimeOut)) {
-      this.addConfigurationKey(
+    if (
+      !ChargingStationConfigurationUtils.getConfigurationKey(
+        this,
+        StandardParametersKey.LocalAuthListEnabled
+      ) &&
+      ChargingStationConfigurationUtils.getConfigurationKey(
+        this,
+        StandardParametersKey.SupportedFeatureProfiles
+      )?.value.includes(SupportedFeatureProfiles.LocalAuthListManagement)
+    ) {
+      ChargingStationConfigurationUtils.addConfigurationKey(
+        this,
+        StandardParametersKey.LocalAuthListEnabled,
+        'false'
+      );
+    }
+    if (
+      !ChargingStationConfigurationUtils.getConfigurationKey(
+        this,
+        StandardParametersKey.ConnectionTimeOut
+      )
+    ) {
+      ChargingStationConfigurationUtils.addConfigurationKey(
+        this,
         StandardParametersKey.ConnectionTimeOut,
         Constants.DEFAULT_CONNECTION_TIMEOUT.toString()
       );
@@ -1471,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(
@@ -1545,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}'`
         );
@@ -1554,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}'`
         );
@@ -1759,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;
@@ -1836,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)
       );
     }
   }
@@ -2018,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) {
@@ -2062,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()
@@ -2127,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;
     }
@@ -2247,105 +2099,4 @@ export default class ChargingStation {
     this.getConnectorStatus(connectorId).energyActiveImportRegisterValue = 0;
     this.getConnectorStatus(connectorId).transactionEnergyActiveImportRegisterValue = 0;
   }
-
-  /**
-   * Charging profiles should already be sorted by connectorId and stack level (highest stack level has priority)
-   *
-   * @param {ChargingProfile[]} chargingProfiles
-   * @returns {{ limit, matchingChargingProfile }}
-   */
-  private getLimitFromChargingProfiles(chargingProfiles: ChargingProfile[]): {
-    limit: number;
-    matchingChargingProfile: ChargingProfile;
-  } | null {
-    for (const chargingProfile of chargingProfiles) {
-      // Set helpers
-      const currentMoment = 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 &&
-        currentMoment.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(currentMoment)) {
-          chargingSchedule.startSchedule.setDate(currentDate.getDate() - 1);
-        }
-      } else if (moment(chargingSchedule.startSchedule).isAfter(currentMoment)) {
-        return null;
-      }
-      // Check if the charging profile is active
-      if (
-        moment(chargingSchedule.startSchedule)
-          .add(chargingSchedule.duration, 's')
-          .isAfter(currentMoment)
-      ) {
-        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
-          ) {
-            const result = {
-              limit: schedulePeriod.limit,
-              matchingChargingProfile: chargingProfile,
-            };
-            logger.debug(
-              `${this.logPrefix()} Matching charging profile found for power limitation: %j`,
-              result
-            );
-            return result;
-          }
-          // Find the right schedule period
-          if (
-            moment(chargingSchedule.startSchedule)
-              .add(schedulePeriod.startPeriod, 's')
-              .isAfter(currentMoment)
-          ) {
-            // Found the schedule: last but one is the correct one
-            const result = {
-              limit: lastButOneSchedule.limit,
-              matchingChargingProfile: chargingProfile,
-            };
-            logger.debug(
-              `${this.logPrefix()} Matching charging profile found for power limitation: %j`,
-              result
-            );
-            return result;
-          }
-          // Keep it
-          lastButOneSchedule = schedulePeriod;
-          // Handle the last schedule period
-          if (
-            schedulePeriod.startPeriod ===
-            chargingSchedule.chargingSchedulePeriod[
-              chargingSchedule.chargingSchedulePeriod.length - 1
-            ].startPeriod
-          ) {
-            const result = {
-              limit: lastButOneSchedule.limit,
-              matchingChargingProfile: chargingProfile,
-            };
-            logger.debug(
-              `${this.logPrefix()} Matching charging profile found for power limitation: %j`,
-              result
-            );
-            return result;
-          }
-        }
-      }
-    }
-    return null;
-  }
 }
diff --git a/src/charging-station/ChargingStationConfigurationUtils.ts b/src/charging-station/ChargingStationConfigurationUtils.ts
new file mode 100644 (file)
index 0000000..9c182ea
--- /dev/null
@@ -0,0 +1,102 @@
+import ChargingStation from './ChargingStation';
+import { ConfigurationKey } from '../types/ChargingStationOcppConfiguration';
+import { StandardParametersKey } from '../types/ocpp/Configuration';
+import logger from '../utils/Logger';
+
+export class ChargingStationConfigurationUtils {
+  public static getConfigurationKey(
+    chargingStation: ChargingStation,
+    key: string | StandardParametersKey,
+    caseInsensitive = false
+  ): ConfigurationKey | undefined {
+    return chargingStation.ocppConfiguration.configurationKey.find((configElement) => {
+      if (caseInsensitive) {
+        return configElement.key.toLowerCase() === key.toLowerCase();
+      }
+      return configElement.key === key;
+    });
+  }
+
+  public static addConfigurationKey(
+    chargingStation: ChargingStation,
+    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 = ChargingStationConfigurationUtils.getConfigurationKey(chargingStation, key);
+    if (keyFound && params?.overwrite) {
+      ChargingStationConfigurationUtils.deleteConfigurationKey(chargingStation, keyFound.key, {
+        save: false,
+      });
+      keyFound = undefined;
+    }
+    if (!keyFound) {
+      chargingStation.ocppConfiguration.configurationKey.push({
+        key,
+        readonly: options.readonly,
+        value,
+        visible: options.visible,
+        reboot: options.reboot,
+      });
+      params?.save && chargingStation.saveOcppConfiguration();
+    } else {
+      logger.error(
+        `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`,
+        keyFound
+      );
+    }
+  }
+
+  public static setConfigurationKeyValue(
+    chargingStation: ChargingStation,
+    key: string | StandardParametersKey,
+    value: string,
+    caseInsensitive = false
+  ): void {
+    const keyFound = ChargingStationConfigurationUtils.getConfigurationKey(
+      chargingStation,
+      key,
+      caseInsensitive
+    );
+    if (keyFound) {
+      chargingStation.ocppConfiguration.configurationKey[
+        chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound)
+      ].value = value;
+      chargingStation.saveOcppConfiguration();
+    } else {
+      logger.error(
+        `${chargingStation.logPrefix()} Trying to set a value on a non existing configuration key: %j`,
+        { key, value }
+      );
+    }
+  }
+
+  public static deleteConfigurationKey(
+    chargingStation: ChargingStation,
+    key: string | StandardParametersKey,
+    params: { save?: boolean; caseInsensitive?: boolean } = { save: true, caseInsensitive: false }
+  ): ConfigurationKey[] {
+    const keyFound = ChargingStationConfigurationUtils.getConfigurationKey(
+      chargingStation,
+      key,
+      params?.caseInsensitive
+    );
+    if (keyFound) {
+      const deletedConfigurationKey = chargingStation.ocppConfiguration.configurationKey.splice(
+        chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound),
+        1
+      );
+      params?.save && chargingStation.saveOcppConfiguration();
+      return deletedConfigurationKey;
+    }
+  }
+}
diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts
new file mode 100644 (file)
index 0000000..da1a828
--- /dev/null
@@ -0,0 +1,341 @@
+import { ChargingProfile, ChargingSchedulePeriod } from '../types/ocpp/ChargingProfile';
+import { ChargingProfileKindType, RecurrencyKindType } from '../types/ocpp/1.6/ChargingProfile';
+import ChargingStationTemplate, { AmpereUnits } from '../types/ChargingStationTemplate';
+
+import { BootNotificationRequest } from '../types/ocpp/Requests';
+import ChargingStationInfo from '../types/ChargingStationInfo';
+import Configuration from '../utils/Configuration';
+import Constants from '../utils/Constants';
+import Utils from '../utils/Utils';
+import { WebSocketCloseEventStatusString } from '../types/WebSocket';
+import { WorkerProcessType } from '../types/Worker';
+import crypto from 'crypto';
+import logger from '../utils/Logger';
+import moment from 'moment';
+
+export class ChargingStationUtils {
+  public static getChargingStationId(
+    index: number,
+    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' + index.toString();
+    return stationTemplate.fixedName
+      ? stationTemplate.baseName
+      : stationTemplate.baseName +
+          '-' +
+          instanceIndex.toString() +
+          idStr.substring(idStr.length - 4) +
+          idSuffix;
+  }
+
+  public static 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');
+  }
+
+  public static 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,
+      }),
+    };
+  }
+
+  public static workerPoolInUse(): boolean {
+    return [WorkerProcessType.DYNAMIC_POOL, WorkerProcessType.STATIC_POOL].includes(
+      Configuration.getWorkerProcess()
+    );
+  }
+
+  public static workerDynamicPoolInUse(): boolean {
+    return Configuration.getWorkerProcess() === WorkerProcessType.DYNAMIC_POOL;
+  }
+
+  /**
+   * Convert websocket error code to human readable string message
+   *
+   * @param code websocket error code
+   * @returns human readable string message
+   */
+  public static getWebSocketCloseEventStatusString(code: number): string {
+    if (code >= 0 && code <= 999) {
+      return '(Unused)';
+    } else if (code >= 1016) {
+      if (code <= 1999) {
+        return '(For WebSocket standard)';
+      } else if (code <= 2999) {
+        return '(For WebSocket extensions)';
+      } else if (code <= 3999) {
+        return '(For libraries and frameworks)';
+      } else if (code <= 4999) {
+        return '(For applications)';
+      }
+    }
+    if (!Utils.isUndefined(WebSocketCloseEventStatusString[code])) {
+      return WebSocketCloseEventStatusString[code] as string;
+    }
+    return '(Unknown)';
+  }
+
+  public static warnDeprecatedTemplateKey(
+    template: ChargingStationTemplate,
+    key: string,
+    templateFile: string,
+    logPrefix: string,
+    logMsgToAppend = ''
+  ): void {
+    if (!Utils.isUndefined(template[key])) {
+      // const logPrefixStr = ` ${chargingStationId} |`;
+      logger.warn(
+        `${logPrefix} Deprecated template key '${key}' usage in file '${templateFile}'${
+          logMsgToAppend && '. ' + logMsgToAppend
+        }`
+      );
+    }
+  }
+
+  public static convertDeprecatedTemplateKey(
+    template: ChargingStationTemplate,
+    deprecatedKey: string,
+    key: string
+  ): void {
+    if (!Utils.isUndefined(template[deprecatedKey])) {
+      template[key] = template[deprecatedKey] as unknown;
+      delete template[deprecatedKey];
+    }
+  }
+
+  public static 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;
+  }
+
+  public static 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
+        ? ChargingStationUtils.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;
+    }
+  }
+
+  public static getAmperageLimitationUnitDivider(stationInfo: ChargingStationInfo): number {
+    let unitDivider = 1;
+    switch (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;
+  }
+
+  /**
+   * Charging profiles should already be sorted by connectorId and stack level (highest stack level has priority)
+   *
+   * @param {ChargingProfile[]} chargingProfiles
+   * @param {string} logPrefix
+   * @returns {{ limit, matchingChargingProfile }}
+   */
+  public static getLimitFromChargingProfiles(
+    chargingProfiles: ChargingProfile[],
+    logPrefix: string
+  ): {
+    limit: number;
+    matchingChargingProfile: ChargingProfile;
+  } | null {
+    for (const chargingProfile of chargingProfiles) {
+      // Set helpers
+      const currentMoment = 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 &&
+        currentMoment.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(currentMoment)) {
+          chargingSchedule.startSchedule.setDate(currentDate.getDate() - 1);
+        }
+      } else if (moment(chargingSchedule.startSchedule).isAfter(currentMoment)) {
+        return null;
+      }
+      // Check if the charging profile is active
+      if (
+        moment(chargingSchedule.startSchedule)
+          .add(chargingSchedule.duration, 's')
+          .isAfter(currentMoment)
+      ) {
+        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
+          ) {
+            const result = {
+              limit: schedulePeriod.limit,
+              matchingChargingProfile: chargingProfile,
+            };
+            logger.debug(
+              `${logPrefix} Matching charging profile found for power limitation: %j`,
+              result
+            );
+            return result;
+          }
+          // Find the right schedule period
+          if (
+            moment(chargingSchedule.startSchedule)
+              .add(schedulePeriod.startPeriod, 's')
+              .isAfter(currentMoment)
+          ) {
+            // Found the schedule: last but one is the correct one
+            const result = {
+              limit: lastButOneSchedule.limit,
+              matchingChargingProfile: chargingProfile,
+            };
+            logger.debug(
+              `${logPrefix} Matching charging profile found for power limitation: %j`,
+              result
+            );
+            return result;
+          }
+          // Keep it
+          lastButOneSchedule = schedulePeriod;
+          // Handle the last schedule period
+          if (
+            schedulePeriod.startPeriod ===
+            chargingSchedule.chargingSchedulePeriod[
+              chargingSchedule.chargingSchedulePeriod.length - 1
+            ].startPeriod
+          ) {
+            const result = {
+              limit: lastButOneSchedule.limit,
+              matchingChargingProfile: chargingProfile,
+            };
+            logger.debug(
+              `${logPrefix} Matching charging profile found for power limitation: %j`,
+              result
+            );
+            return result;
+          }
+        }
+      }
+    }
+    return null;
+  }
+
+  private static getRandomSerialNumberSuffix(params?: {
+    randomBytesLength?: number;
+    upperCase?: boolean;
+  }): string {
+    const randomSerialNumberSuffix = crypto
+      .randomBytes(params?.randomBytesLength ?? 16)
+      .toString('hex');
+    if (params?.upperCase) {
+      return randomSerialNumberSuffix.toUpperCase();
+    }
+    return randomSerialNumberSuffix;
+  }
+}
index 514fba8c6811c9652133a70f9b882b9818c698b7..85ea77fb8b58d1d18d6424ee2f8c927080b03180 100644 (file)
@@ -8,13 +8,14 @@ import {
 import { parentPort, workerData } from 'worker_threads';
 
 import ChargingStation from './ChargingStation';
+import { ChargingStationUtils } from './ChargingStationUtils';
 import { ThreadWorker } from 'poolifier';
 import Utils from '../utils/Utils';
 import WorkerConstants from '../worker/WorkerConstants';
 
 // Conditionally export ThreadWorker instance for pool usage
 export let threadWorker: ThreadWorker;
-if (Utils.workerPoolInUse()) {
+if (ChargingStationUtils.workerPoolInUse()) {
   threadWorker = new ThreadWorker<ChargingStationWorkerData>(startChargingStation, {
     maxInactiveTime: WorkerConstants.POOL_MAX_INACTIVE_TIME,
     async: false,
index 45f3723c5a3b7db22aa6fbc4cff2f1db4853d417..f51cc65526ecc684a99fd8045989cb6c982ea196 100644 (file)
@@ -60,6 +60,7 @@ import {
 } from '../../../types/ocpp/1.6/Configuration';
 
 import type ChargingStation from '../../ChargingStation';
+import { ChargingStationConfigurationUtils } from '../../ChargingStationConfigurationUtils';
 import Constants from '../../../utils/Constants';
 import { DefaultResponse } from '../../../types/ocpp/Responses';
 import { ErrorType } from '../../../types/ocpp/ErrorType';
@@ -301,7 +302,10 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       }
     } else {
       for (const key of commandPayload.key) {
-        const keyFound = chargingStation.getConfigurationKey(key);
+        const keyFound = ChargingStationConfigurationUtils.getConfigurationKey(
+          chargingStation,
+          key
+        );
         if (keyFound) {
           if (Utils.isUndefined(keyFound.visible)) {
             keyFound.visible = true;
@@ -346,7 +350,11 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         commandPayload
       );
     }
-    const keyToChange = chargingStation.getConfigurationKey(commandPayload.key, true);
+    const keyToChange = ChargingStationConfigurationUtils.getConfigurationKey(
+      chargingStation,
+      commandPayload.key,
+      true
+    );
     if (!keyToChange) {
       return Constants.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED;
     } else if (keyToChange && keyToChange.readonly) {
@@ -354,19 +362,26 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     } else if (keyToChange && !keyToChange.readonly) {
       let valueChanged = false;
       if (keyToChange.value !== commandPayload.value) {
-        chargingStation.setConfigurationKeyValue(commandPayload.key, commandPayload.value, true);
+        ChargingStationConfigurationUtils.setConfigurationKeyValue(
+          chargingStation,
+          commandPayload.key,
+          commandPayload.value,
+          true
+        );
         valueChanged = true;
       }
       let triggerHeartbeatRestart = false;
       if (keyToChange.key === OCPP16StandardParametersKey.HeartBeatInterval && valueChanged) {
-        chargingStation.setConfigurationKeyValue(
+        ChargingStationConfigurationUtils.setConfigurationKeyValue(
+          chargingStation,
           OCPP16StandardParametersKey.HeartbeatInterval,
           commandPayload.value
         );
         triggerHeartbeatRestart = true;
       }
       if (keyToChange.key === OCPP16StandardParametersKey.HeartbeatInterval && valueChanged) {
-        chargingStation.setConfigurationKeyValue(
+        ChargingStationConfigurationUtils.setConfigurationKeyValue(
+          chargingStation,
           OCPP16StandardParametersKey.HeartBeatInterval,
           commandPayload.value
         );
index c4e7eef879993860e5f94cff257cf6e3150115ff..1bcce7055b6814d4de4c7344f6b35d2757b9d2ea 100644 (file)
@@ -25,6 +25,7 @@ import {
 } from '../../../types/ocpp/1.6/MeterValues';
 
 import type ChargingStation from '../../ChargingStation';
+import { ChargingStationConfigurationUtils } from '../../ChargingStationConfigurationUtils';
 import { ErrorType } from '../../../types/ocpp/ErrorType';
 import { JsonType } from '../../../types/JsonType';
 import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointErrorCode';
@@ -102,13 +103,15 @@ export default class OCPP16ResponseService extends OCPPResponseService {
     payload: OCPP16BootNotificationResponse
   ): void {
     if (payload.status === OCPP16RegistrationStatus.ACCEPTED) {
-      chargingStation.addConfigurationKey(
+      ChargingStationConfigurationUtils.addConfigurationKey(
+        chargingStation,
         OCPP16StandardParametersKey.HeartbeatInterval,
         payload.interval.toString(),
         {},
         { overwrite: true, save: true }
       );
-      chargingStation.addConfigurationKey(
+      ChargingStationConfigurationUtils.addConfigurationKey(
+        chargingStation,
         OCPP16StandardParametersKey.HeartBeatInterval,
         payload.interval.toString(),
         { visible: false },
@@ -334,9 +337,11 @@ export default class OCPP16ResponseService extends OCPPResponseService {
       if (chargingStation.stationInfo.powerSharedByConnectors) {
         chargingStation.stationInfo.powerDivider++;
       }
-      const configuredMeterValueSampleInterval = chargingStation.getConfigurationKey(
-        OCPP16StandardParametersKey.MeterValueSampleInterval
-      );
+      const configuredMeterValueSampleInterval =
+        ChargingStationConfigurationUtils.getConfigurationKey(
+          chargingStation,
+          OCPP16StandardParametersKey.MeterValueSampleInterval
+        );
       chargingStation.startMeterValues(
         connectorId,
         configuredMeterValueSampleInterval
index 5035efdba22786f6f53e54cc1652073c9336e642..2411c605bfc27c44ebd2d70b6bc3643f12a635e3 100644 (file)
@@ -1,6 +1,3 @@
-import Configuration from './Configuration';
-import { WebSocketCloseEventStatusString } from '../types/WebSocket';
-import { WorkerProcessType } from '../types/Worker';
 import crypto from 'crypto';
 import { v4 as uuid } from 'uuid';
 
@@ -208,42 +205,6 @@ export default class Utils {
     return delay + randomSum;
   }
 
-  /**
-   * Convert websocket error code to human readable string message
-   *
-   * @param code websocket error code
-   * @returns human readable string message
-   */
-  public static getWebSocketCloseEventStatusString(code: number): string {
-    if (code >= 0 && code <= 999) {
-      return '(Unused)';
-    } else if (code >= 1016) {
-      if (code <= 1999) {
-        return '(For WebSocket standard)';
-      } else if (code <= 2999) {
-        return '(For WebSocket extensions)';
-      } else if (code <= 3999) {
-        return '(For libraries and frameworks)';
-      } else if (code <= 4999) {
-        return '(For applications)';
-      }
-    }
-    if (!Utils.isUndefined(WebSocketCloseEventStatusString[code])) {
-      return WebSocketCloseEventStatusString[code] as string;
-    }
-    return '(Unknown)';
-  }
-
-  public static workerPoolInUse(): boolean {
-    return [WorkerProcessType.DYNAMIC_POOL, WorkerProcessType.STATIC_POOL].includes(
-      Configuration.getWorkerProcess()
-    );
-  }
-
-  public static workerDynamicPoolInUse(): boolean {
-    return Configuration.getWorkerProcess() === WorkerProcessType.DYNAMIC_POOL;
-  }
-
   public static async promiseWithTimeout<T>(
     promise: Promise<T>,
     timeoutMs: number,