Move OCPP log message formatting helper into static helpers class
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStation.ts
index f30bf18952b8a5233a3475cc20da2883756f723e..84930a5f628be1f99ed0e010778b55474961729c 100644 (file)
@@ -36,7 +36,6 @@ import type { ChargingStationOcppConfiguration } from '../types/ChargingStationO
 import {
   type ChargingStationTemplate,
   CurrentType,
-  type FirmwareUpgrade,
   PowerUnits,
   type WsOptions,
 } from '../types/ChargingStationTemplate';
@@ -44,7 +43,6 @@ import { SupervisionUrlDistribution } from '../types/ConfigurationData';
 import type { ConnectorStatus } from '../types/ConnectorStatus';
 import { FileType } from '../types/FileType';
 import type { JsonType } from '../types/JsonType';
-import { ChargingProfile, ChargingRateUnitType } from '../types/ocpp/ChargingProfile';
 import {
   ConnectorPhaseRotation,
   StandardParametersKey,
@@ -266,6 +264,11 @@ export default class ChargingStation {
       : defaultVoltageOut;
   }
 
+  public getMaximumPower(stationInfo?: ChargingStationInfo): number {
+    const localStationInfo = stationInfo ?? this.stationInfo;
+    return (localStationInfo['maxPower'] as number) ?? localStationInfo.maximumPower;
+  }
+
   public getConnectorMaximumAvailablePower(connectorId: number): number {
     let connectorAmperageLimitationPowerLimit: number;
     if (
@@ -283,13 +286,14 @@ export default class ChargingStation {
         this.powerDivider;
     }
     const connectorMaximumPower = this.getMaximumPower() / this.powerDivider;
-    const connectorChargingProfilePowerLimit = this.getChargingProfilePowerLimit(connectorId);
+    const connectorChargingProfilesPowerLimit =
+      ChargingStationUtils.getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId);
     return Math.min(
       isNaN(connectorMaximumPower) ? Infinity : connectorMaximumPower,
       isNaN(connectorAmperageLimitationPowerLimit)
         ? Infinity
         : connectorAmperageLimitationPowerLimit,
-      isNaN(connectorChargingProfilePowerLimit) ? Infinity : connectorChargingProfilePowerLimit
+      isNaN(connectorChargingProfilesPowerLimit) ? Infinity : connectorChargingProfilesPowerLimit
     );
   }
 
@@ -342,16 +346,16 @@ export default class ChargingStation {
 
   public getEnergyActiveImportRegisterByTransactionId(
     transactionId: number,
-    meterStop = false
+    rounded = false
   ): number {
     return this.getEnergyActiveImportRegister(
       this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId)),
-      meterStop
+      rounded
     );
   }
 
-  public getEnergyActiveImportRegisterByConnectorId(connectorId: number): number {
-    return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId));
+  public getEnergyActiveImportRegisterByConnectorId(connectorId: number, rounded = false): number {
+    return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId), rounded);
   }
 
   public getAuthorizeRemoteTxRequests(): boolean {
@@ -378,12 +382,15 @@ export default class ChargingStation {
       this.getHeartbeatInterval() > 0 &&
       !this.heartbeatSetInterval
     ) {
-      // 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.heartbeatSetInterval = setInterval(() => {
+        this.ocppRequestService
+          .requestHandler<HeartbeatRequest, HeartbeatResponse>(this, RequestCommand.HEARTBEAT)
+          .catch((error) => {
+            logger.error(
+              `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
+              error
+            );
+          });
       }, this.getHeartbeatInterval());
       logger.info(
         this.logPrefix() +
@@ -449,18 +456,16 @@ export default class ChargingStation {
       return;
     }
     if (interval > 0) {
-      // eslint-disable-next-line @typescript-eslint/no-misused-promises
-      this.getConnectorStatus(connectorId).transactionSetInterval = setInterval(
-        // eslint-disable-next-line @typescript-eslint/no-misused-promises
-        async (): Promise<void> => {
-          // FIXME: Implement OCPP version agnostic helpers
-          const meterValue: MeterValue = OCPP16ServiceUtils.buildMeterValue(
-            this,
-            connectorId,
-            this.getConnectorStatus(connectorId).transactionId,
-            interval
-          );
-          await this.ocppRequestService.requestHandler<MeterValuesRequest, MeterValuesResponse>(
+      this.getConnectorStatus(connectorId).transactionSetInterval = setInterval(() => {
+        // FIXME: Implement OCPP version agnostic helpers
+        const meterValue: MeterValue = OCPP16ServiceUtils.buildMeterValue(
+          this,
+          connectorId,
+          this.getConnectorStatus(connectorId).transactionId,
+          interval
+        );
+        this.ocppRequestService
+          .requestHandler<MeterValuesRequest, MeterValuesResponse>(
             this,
             RequestCommand.METER_VALUES,
             {
@@ -468,10 +473,14 @@ export default class ChargingStation {
               transactionId: this.getConnectorStatus(connectorId).transactionId,
               meterValue: [meterValue],
             }
-          );
-        },
-        interval
-      );
+          )
+          .catch((error) => {
+            logger.error(
+              `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
+              error
+            );
+          });
+      }, interval);
     } else {
       logger.error(
         `${this.logPrefix()} Charging station ${
@@ -1559,7 +1568,7 @@ export default class ChargingStation {
             logger.debug(
               `${this.logPrefix()} << Command '${
                 requestCommandName ?? Constants.UNKNOWN_COMMAND
-              }' received error payload: ${JSON.stringify(request)}`
+              }' received error response payload: ${JSON.stringify(request)}`
             );
             errorCallback(new OCPPError(errorType, errorMessage, requestCommandName, errorDetails));
             break;
@@ -1633,19 +1642,16 @@ export default class ChargingStation {
     logger.error(this.logPrefix() + ' WebSocket error:', error);
   }
 
-  private getEnergyActiveImportRegister(
-    connectorStatus: ConnectorStatus,
-    meterStop = false
-  ): number {
+  private getEnergyActiveImportRegister(connectorStatus: ConnectorStatus, rounded = false): number {
     if (this.getMeteringPerTransaction() === true) {
       return (
-        (meterStop === true
+        (rounded === true
           ? Math.round(connectorStatus?.transactionEnergyActiveImportRegisterValue)
           : connectorStatus?.transactionEnergyActiveImportRegisterValue) ?? 0
       );
     }
     return (
-      (meterStop === true
+      (rounded === true
         ? Math.round(connectorStatus?.energyActiveImportRegisterValue)
         : connectorStatus?.energyActiveImportRegisterValue) ?? 0
     );
@@ -1723,11 +1729,6 @@ export default class ChargingStation {
     return powerDivider;
   }
 
-  private getMaximumPower(stationInfo?: ChargingStationInfo): number {
-    const localStationInfo = stationInfo ?? this.stationInfo;
-    return (localStationInfo['maxPower'] as number) ?? localStationInfo.maximumPower;
-  }
-
   private getMaximumAmperage(stationInfo: ChargingStationInfo): number | undefined {
     const maximumPower = this.getMaximumPower(stationInfo);
     switch (this.getCurrentOutType(stationInfo)) {
@@ -1761,57 +1762,6 @@ export default class ChargingStation {
     }
   }
 
-  private getChargingProfilePowerLimit(connectorId: number): number | undefined {
-    let limit: number, matchingChargingProfile: ChargingProfile;
-    let chargingProfiles: ChargingProfile[] = [];
-    // Get charging profiles for connector and sort by stack level
-    chargingProfiles = this.getConnectorStatus(connectorId).chargingProfiles.sort(
-      (a, b) => b.stackLevel - a.stackLevel
-    );
-    // Get profiles on connector 0
-    if (this.getConnectorStatus(0).chargingProfiles) {
-      chargingProfiles.push(
-        ...this.getConnectorStatus(0).chargingProfiles.sort((a, b) => b.stackLevel - a.stackLevel)
-      );
-    }
-    if (!Utils.isEmptyArray(chargingProfiles)) {
-      const result = ChargingStationUtils.getLimitFromChargingProfiles(
-        chargingProfiles,
-        this.logPrefix()
-      );
-      if (!Utils.isNullOrUndefined(result)) {
-        limit = result.limit;
-        matchingChargingProfile = result.matchingChargingProfile;
-        switch (this.getCurrentOutType()) {
-          case CurrentType.AC:
-            limit =
-              matchingChargingProfile.chargingSchedule.chargingRateUnit ===
-              ChargingRateUnitType.WATT
-                ? limit
-                : ACElectricUtils.powerTotal(this.getNumberOfPhases(), this.getVoltageOut(), limit);
-            break;
-          case CurrentType.DC:
-            limit =
-              matchingChargingProfile.chargingSchedule.chargingRateUnit ===
-              ChargingRateUnitType.WATT
-                ? limit
-                : DCElectricUtils.power(this.getVoltageOut(), limit);
-        }
-        const connectorMaximumPower = this.getMaximumPower() / this.powerDivider;
-        if (limit > connectorMaximumPower) {
-          logger.error(
-            `${this.logPrefix()} Charging profile id ${
-              matchingChargingProfile.chargingProfileId
-            } limit ${limit} is greater than connector id ${connectorId} maximum ${connectorMaximumPower}, dump charging profiles' stack: %j`,
-            this.getConnectorStatus(connectorId).chargingProfiles
-          );
-          limit = connectorMaximumPower;
-        }
-      }
-    }
-    return limit;
-  }
-
   private async startMessageSequence(): Promise<void> {
     if (this.stationInfo?.autoRegister === true) {
       await this.ocppRequestService.requestHandler<