refactor: factor out voltage phase line to line computation
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16ServiceUtils.ts
index 17da98c08a6333c14a5fb83eba3725ad5321775a..502fd6a57c2fde8d27eca15f4b78ab62aba66f72 100644 (file)
@@ -45,7 +45,6 @@ import {
   type OCPP16SupportedFeatureProfiles,
   OCPPVersion,
   type SampledValueTemplate,
-  Voltage,
 } from '../../../types';
 import {
   ACElectricUtils,
@@ -57,6 +56,7 @@ import {
   getRandomFloatRounded,
   getRandomInteger,
   isNotEmptyArray,
+  isNotEmptyString,
   isNullOrUndefined,
   isUndefined,
   logger,
@@ -102,7 +102,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
     if (socSampledValueTemplate) {
       const socMaximumValue = 100;
       const socMinimumValue = socSampledValueTemplate.minimumValue ?? 0;
-      const socSampledValueTemplateValue = socSampledValueTemplate.value
+      const socSampledValueTemplateValue = isNotEmptyString(socSampledValueTemplate.value)
         ? getRandomFloatFluctuatedRounded(
             parseInt(socSampledValueTemplate.value),
             socSampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT,
@@ -123,7 +123,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
             OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER
           }: connector id ${connectorId}, transaction id ${connector?.transactionId}, value: ${socMinimumValue}/${
             meterValue.sampledValue[sampledValuesIndex].value
-          }/${socMaximumValue}}`,
+          }/${socMaximumValue}`,
         );
       }
     }
@@ -134,9 +134,9 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
       OCPP16MeterValueMeasurand.VOLTAGE,
     );
     if (voltageSampledValueTemplate) {
-      const voltageSampledValueTemplateValue = voltageSampledValueTemplate.value
+      const voltageSampledValueTemplateValue = isNotEmptyString(voltageSampledValueTemplate.value)
         ? parseInt(voltageSampledValueTemplate.value)
-        : chargingStation.getVoltageOut();
+        : chargingStation.stationInfo.voltageOut!;
       const fluctuationPercent =
         voltageSampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT;
       const voltageMeasurandValue = getRandomFloatFluctuatedRounded(
@@ -145,7 +145,8 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
       );
       if (
         chargingStation.getNumberOfPhases() !== 3 ||
-        (chargingStation.getNumberOfPhases() === 3 && chargingStation.getMainVoltageMeterValues())
+        (chargingStation.getNumberOfPhases() === 3 &&
+          chargingStation.stationInfo?.mainVoltageMeterValues)
       ) {
         meterValue.sampledValue.push(
           OCPP16ServiceUtils.buildSampledValue(voltageSampledValueTemplate, voltageMeasurandValue),
@@ -166,10 +167,11 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
           );
         let voltagePhaseLineToNeutralMeasurandValue: number | undefined;
         if (voltagePhaseLineToNeutralSampledValueTemplate) {
-          const voltagePhaseLineToNeutralSampledValueTemplateValue =
-            voltagePhaseLineToNeutralSampledValueTemplate.value
-              ? parseInt(voltagePhaseLineToNeutralSampledValueTemplate.value)
-              : chargingStation.getVoltageOut();
+          const voltagePhaseLineToNeutralSampledValueTemplateValue = isNotEmptyString(
+            voltagePhaseLineToNeutralSampledValueTemplate.value,
+          )
+            ? parseInt(voltagePhaseLineToNeutralSampledValueTemplate.value)
+            : chargingStation.stationInfo.voltageOut!;
           const fluctuationPhaseToNeutralPercent =
             voltagePhaseLineToNeutralSampledValueTemplate.fluctuationPercent ??
             Constants.DEFAULT_FLUCTUATION_PERCENT;
@@ -186,12 +188,17 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
             phaseLineToNeutralValue as OCPP16MeterValuePhase,
           ),
         );
-        if (chargingStation.getPhaseLineToLineVoltageMeterValues()) {
+        if (chargingStation.stationInfo?.phaseLineToLineVoltageMeterValues) {
           const phaseLineToLineValue = `L${phase}-L${
             (phase + 1) % chargingStation.getNumberOfPhases() !== 0
               ? (phase + 1) % chargingStation.getNumberOfPhases()
               : chargingStation.getNumberOfPhases()
           }`;
+          const voltagePhaseLineToLineValueRounded = roundTo(
+            Math.sqrt(chargingStation.getNumberOfPhases()) *
+              chargingStation.stationInfo.voltageOut!,
+            2,
+          );
           const voltagePhaseLineToLineSampledValueTemplate =
             OCPP16ServiceUtils.getSampledValueTemplate(
               chargingStation,
@@ -201,10 +208,11 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
             );
           let voltagePhaseLineToLineMeasurandValue: number | undefined;
           if (voltagePhaseLineToLineSampledValueTemplate) {
-            const voltagePhaseLineToLineSampledValueTemplateValue =
-              voltagePhaseLineToLineSampledValueTemplate.value
-                ? parseInt(voltagePhaseLineToLineSampledValueTemplate.value)
-                : Voltage.VOLTAGE_400;
+            const voltagePhaseLineToLineSampledValueTemplateValue = isNotEmptyString(
+              voltagePhaseLineToLineSampledValueTemplate.value,
+            )
+              ? parseInt(voltagePhaseLineToLineSampledValueTemplate.value)
+              : voltagePhaseLineToLineValueRounded;
             const fluctuationPhaseLineToLinePercent =
               voltagePhaseLineToLineSampledValueTemplate.fluctuationPercent ??
               Constants.DEFAULT_FLUCTUATION_PERCENT;
@@ -214,7 +222,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
             );
           }
           const defaultVoltagePhaseLineToLineMeasurandValue = getRandomFloatFluctuatedRounded(
-            Voltage.VOLTAGE_400,
+            voltagePhaseLineToLineValueRounded,
             fluctuationPercent,
           );
           meterValue.sampledValue.push(
@@ -265,7 +273,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
       const errMsg = `MeterValues measurand ${
         powerSampledValueTemplate.measurand ??
         OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER
-      }: Unknown ${chargingStation.getCurrentOutType()} currentOutType in template file ${
+      }: Unknown ${chargingStation.stationInfo?.currentOutType} currentOutType in template file ${
         chargingStation.templateFile
       }, cannot calculate ${
         powerSampledValueTemplate.measurand ??
@@ -283,7 +291,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
       const connectorMinimumPowerPerPhase = Math.round(
         connectorMinimumPower / chargingStation.getNumberOfPhases(),
       );
-      switch (chargingStation.getCurrentOutType()) {
+      switch (chargingStation.stationInfo?.currentOutType) {
         case CurrentType.AC:
           if (chargingStation.getNumberOfPhases() === 3) {
             const defaultFluctuatedPowerPerPhase =
@@ -292,7 +300,10 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
                 OCPP16ServiceUtils.getLimitFromSampledValueTemplateCustomValue(
                   powerSampledValueTemplate.value,
                   connectorMaximumPower / unitDivider,
-                  { limitationEnabled: chargingStation.getCustomValueLimitationMeterValues() },
+                  {
+                    limitationEnabled:
+                      chargingStation.stationInfo?.customValueLimitationMeterValues,
+                  },
                 ) / chargingStation.getNumberOfPhases(),
                 powerSampledValueTemplate.fluctuationPercent ??
                   Constants.DEFAULT_FLUCTUATION_PERCENT,
@@ -303,7 +314,10 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
                 OCPP16ServiceUtils.getLimitFromSampledValueTemplateCustomValue(
                   powerPerPhaseSampledValueTemplates.L1.value,
                   connectorMaximumPowerPerPhase / unitDivider,
-                  { limitationEnabled: chargingStation.getCustomValueLimitationMeterValues() },
+                  {
+                    limitationEnabled:
+                      chargingStation.stationInfo?.customValueLimitationMeterValues,
+                  },
                 ),
                 powerPerPhaseSampledValueTemplates.L1.fluctuationPercent ??
                   Constants.DEFAULT_FLUCTUATION_PERCENT,
@@ -314,7 +328,10 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
                 OCPP16ServiceUtils.getLimitFromSampledValueTemplateCustomValue(
                   powerPerPhaseSampledValueTemplates.L2.value,
                   connectorMaximumPowerPerPhase / unitDivider,
-                  { limitationEnabled: chargingStation.getCustomValueLimitationMeterValues() },
+                  {
+                    limitationEnabled:
+                      chargingStation.stationInfo?.customValueLimitationMeterValues,
+                  },
                 ),
                 powerPerPhaseSampledValueTemplates.L2.fluctuationPercent ??
                   Constants.DEFAULT_FLUCTUATION_PERCENT,
@@ -325,7 +342,10 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
                 OCPP16ServiceUtils.getLimitFromSampledValueTemplateCustomValue(
                   powerPerPhaseSampledValueTemplates.L3.value,
                   connectorMaximumPowerPerPhase / unitDivider,
-                  { limitationEnabled: chargingStation.getCustomValueLimitationMeterValues() },
+                  {
+                    limitationEnabled:
+                      chargingStation.stationInfo?.customValueLimitationMeterValues,
+                  },
                 ),
                 powerPerPhaseSampledValueTemplates.L3.fluctuationPercent ??
                   Constants.DEFAULT_FLUCTUATION_PERCENT,
@@ -352,12 +372,15 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
                 connectorMinimumPowerPerPhase / unitDivider,
               );
           } else {
-            powerMeasurandValues.L1 = powerSampledValueTemplate.value
+            powerMeasurandValues.L1 = isNotEmptyString(powerSampledValueTemplate.value)
               ? getRandomFloatFluctuatedRounded(
                   OCPP16ServiceUtils.getLimitFromSampledValueTemplateCustomValue(
                     powerSampledValueTemplate.value,
                     connectorMaximumPower / unitDivider,
-                    { limitationEnabled: chargingStation.getCustomValueLimitationMeterValues() },
+                    {
+                      limitationEnabled:
+                        chargingStation.stationInfo?.customValueLimitationMeterValues,
+                    },
                   ),
                   powerSampledValueTemplate.fluctuationPercent ??
                     Constants.DEFAULT_FLUCTUATION_PERCENT,
@@ -375,12 +398,15 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
           );
           break;
         case CurrentType.DC:
-          powerMeasurandValues.allPhases = powerSampledValueTemplate.value
+          powerMeasurandValues.allPhases = isNotEmptyString(powerSampledValueTemplate.value)
             ? getRandomFloatFluctuatedRounded(
                 OCPP16ServiceUtils.getLimitFromSampledValueTemplateCustomValue(
                   powerSampledValueTemplate.value,
                   connectorMaximumPower / unitDivider,
-                  { limitationEnabled: chargingStation.getCustomValueLimitationMeterValues() },
+                  {
+                    limitationEnabled:
+                      chargingStation.stationInfo?.customValueLimitationMeterValues,
+                  },
                 ),
                 powerSampledValueTemplate.fluctuationPercent ??
                   Constants.DEFAULT_FLUCTUATION_PERCENT,
@@ -501,7 +527,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
       const errMsg = `MeterValues measurand ${
         currentSampledValueTemplate.measurand ??
         OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER
-      }: Unknown ${chargingStation.getCurrentOutType()} currentOutType in template file ${
+      }: Unknown ${chargingStation.stationInfo?.currentOutType} currentOutType in template file ${
         chargingStation.templateFile
       }, cannot calculate ${
         currentSampledValueTemplate.measurand ??
@@ -512,12 +538,12 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
         chargingStation.getConnectorMaximumAvailablePower(connectorId);
       const connectorMinimumAmperage = currentSampledValueTemplate.minimumValue ?? 0;
       let connectorMaximumAmperage: number;
-      switch (chargingStation.getCurrentOutType()) {
+      switch (chargingStation.stationInfo?.currentOutType) {
         case CurrentType.AC:
           connectorMaximumAmperage = ACElectricUtils.amperagePerPhaseFromPower(
             chargingStation.getNumberOfPhases(),
             connectorMaximumAvailablePower,
-            chargingStation.getVoltageOut(),
+            chargingStation.stationInfo.voltageOut!,
           );
           if (chargingStation.getNumberOfPhases() === 3) {
             const defaultFluctuatedAmperagePerPhase =
@@ -526,7 +552,10 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
                 OCPP16ServiceUtils.getLimitFromSampledValueTemplateCustomValue(
                   currentSampledValueTemplate.value,
                   connectorMaximumAmperage,
-                  { limitationEnabled: chargingStation.getCustomValueLimitationMeterValues() },
+                  {
+                    limitationEnabled:
+                      chargingStation.stationInfo?.customValueLimitationMeterValues,
+                  },
                 ),
                 currentSampledValueTemplate.fluctuationPercent ??
                   Constants.DEFAULT_FLUCTUATION_PERCENT,
@@ -537,7 +566,10 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
                 OCPP16ServiceUtils.getLimitFromSampledValueTemplateCustomValue(
                   currentPerPhaseSampledValueTemplates.L1.value,
                   connectorMaximumAmperage,
-                  { limitationEnabled: chargingStation.getCustomValueLimitationMeterValues() },
+                  {
+                    limitationEnabled:
+                      chargingStation.stationInfo?.customValueLimitationMeterValues,
+                  },
                 ),
                 currentPerPhaseSampledValueTemplates.L1.fluctuationPercent ??
                   Constants.DEFAULT_FLUCTUATION_PERCENT,
@@ -548,7 +580,10 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
                 OCPP16ServiceUtils.getLimitFromSampledValueTemplateCustomValue(
                   currentPerPhaseSampledValueTemplates.L2.value,
                   connectorMaximumAmperage,
-                  { limitationEnabled: chargingStation.getCustomValueLimitationMeterValues() },
+                  {
+                    limitationEnabled:
+                      chargingStation.stationInfo?.customValueLimitationMeterValues,
+                  },
                 ),
                 currentPerPhaseSampledValueTemplates.L2.fluctuationPercent ??
                   Constants.DEFAULT_FLUCTUATION_PERCENT,
@@ -559,7 +594,10 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
                 OCPP16ServiceUtils.getLimitFromSampledValueTemplateCustomValue(
                   currentPerPhaseSampledValueTemplates.L3.value,
                   connectorMaximumAmperage,
-                  { limitationEnabled: chargingStation.getCustomValueLimitationMeterValues() },
+                  {
+                    limitationEnabled:
+                      chargingStation.stationInfo?.customValueLimitationMeterValues,
+                  },
                 ),
                 currentPerPhaseSampledValueTemplates.L3.fluctuationPercent ??
                   Constants.DEFAULT_FLUCTUATION_PERCENT,
@@ -577,12 +615,15 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
               (defaultFluctuatedAmperagePerPhase as number) ??
               getRandomFloatRounded(connectorMaximumAmperage, connectorMinimumAmperage);
           } else {
-            currentMeasurandValues.L1 = currentSampledValueTemplate.value
+            currentMeasurandValues.L1 = isNotEmptyString(currentSampledValueTemplate.value)
               ? getRandomFloatFluctuatedRounded(
                   OCPP16ServiceUtils.getLimitFromSampledValueTemplateCustomValue(
                     currentSampledValueTemplate.value,
                     connectorMaximumAmperage,
-                    { limitationEnabled: chargingStation.getCustomValueLimitationMeterValues() },
+                    {
+                      limitationEnabled:
+                        chargingStation.stationInfo?.customValueLimitationMeterValues,
+                    },
                   ),
                   currentSampledValueTemplate.fluctuationPercent ??
                     Constants.DEFAULT_FLUCTUATION_PERCENT,
@@ -600,14 +641,17 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
         case CurrentType.DC:
           connectorMaximumAmperage = DCElectricUtils.amperage(
             connectorMaximumAvailablePower,
-            chargingStation.getVoltageOut(),
+            chargingStation.stationInfo.voltageOut!,
           );
-          currentMeasurandValues.allPhases = currentSampledValueTemplate.value
+          currentMeasurandValues.allPhases = isNotEmptyString(currentSampledValueTemplate.value)
             ? getRandomFloatFluctuatedRounded(
                 OCPP16ServiceUtils.getLimitFromSampledValueTemplateCustomValue(
                   currentSampledValueTemplate.value,
                   connectorMaximumAmperage,
-                  { limitationEnabled: chargingStation.getCustomValueLimitationMeterValues() },
+                  {
+                    limitationEnabled:
+                      chargingStation.stationInfo?.customValueLimitationMeterValues,
+                  },
                 ),
                 currentSampledValueTemplate.fluctuationPercent ??
                   Constants.DEFAULT_FLUCTUATION_PERCENT,
@@ -696,14 +740,14 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
         (connectorMaximumAvailablePower * interval) / (3600 * 1000),
         2,
       );
-      const energyValueRounded = energySampledValueTemplate.value
+      const energyValueRounded = isNotEmptyString(energySampledValueTemplate.value)
         ? // Cumulate the fluctuated value around the static one
           getRandomFloatFluctuatedRounded(
             OCPP16ServiceUtils.getLimitFromSampledValueTemplateCustomValue(
               energySampledValueTemplate.value,
               connectorMaximumEnergyRounded,
               {
-                limitationEnabled: chargingStation.getCustomValueLimitationMeterValues(),
+                limitationEnabled: chargingStation.stationInfo?.customValueLimitationMeterValues,
                 unitMultiplier: unitDivider,
               },
             ),