refactor: factor out voltage phase line to line computation
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16ServiceUtils.ts
index 02cbac8d48ae1710c638e592ea4f9b5569ea4f56..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,
@@ -134,7 +134,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
       OCPP16MeterValueMeasurand.VOLTAGE,
     );
     if (voltageSampledValueTemplate) {
-      const voltageSampledValueTemplateValue = voltageSampledValueTemplate.value
+      const voltageSampledValueTemplateValue = isNotEmptyString(voltageSampledValueTemplate.value)
         ? parseInt(voltageSampledValueTemplate.value)
         : chargingStation.stationInfo.voltageOut!;
       const fluctuationPercent =
@@ -167,10 +167,11 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
           );
         let voltagePhaseLineToNeutralMeasurandValue: number | undefined;
         if (voltagePhaseLineToNeutralSampledValueTemplate) {
-          const voltagePhaseLineToNeutralSampledValueTemplateValue =
-            voltagePhaseLineToNeutralSampledValueTemplate.value
-              ? parseInt(voltagePhaseLineToNeutralSampledValueTemplate.value)
-              : chargingStation.stationInfo.voltageOut!;
+          const voltagePhaseLineToNeutralSampledValueTemplateValue = isNotEmptyString(
+            voltagePhaseLineToNeutralSampledValueTemplate.value,
+          )
+            ? parseInt(voltagePhaseLineToNeutralSampledValueTemplate.value)
+            : chargingStation.stationInfo.voltageOut!;
           const fluctuationPhaseToNeutralPercent =
             voltagePhaseLineToNeutralSampledValueTemplate.fluctuationPercent ??
             Constants.DEFAULT_FLUCTUATION_PERCENT;
@@ -193,6 +194,11 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
               ? (phase + 1) % chargingStation.getNumberOfPhases()
               : chargingStation.getNumberOfPhases()
           }`;
+          const voltagePhaseLineToLineValueRounded = roundTo(
+            Math.sqrt(chargingStation.getNumberOfPhases()) *
+              chargingStation.stationInfo.voltageOut!,
+            2,
+          );
           const voltagePhaseLineToLineSampledValueTemplate =
             OCPP16ServiceUtils.getSampledValueTemplate(
               chargingStation,
@@ -202,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;
@@ -215,7 +222,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
             );
           }
           const defaultVoltagePhaseLineToLineMeasurandValue = getRandomFloatFluctuatedRounded(
-            Voltage.VOLTAGE_400,
+            voltagePhaseLineToLineValueRounded,
             fluctuationPercent,
           );
           meterValue.sampledValue.push(
@@ -365,7 +372,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
                 connectorMinimumPowerPerPhase / unitDivider,
               );
           } else {
-            powerMeasurandValues.L1 = powerSampledValueTemplate.value
+            powerMeasurandValues.L1 = isNotEmptyString(powerSampledValueTemplate.value)
               ? getRandomFloatFluctuatedRounded(
                   OCPP16ServiceUtils.getLimitFromSampledValueTemplateCustomValue(
                     powerSampledValueTemplate.value,
@@ -391,7 +398,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
           );
           break;
         case CurrentType.DC:
-          powerMeasurandValues.allPhases = powerSampledValueTemplate.value
+          powerMeasurandValues.allPhases = isNotEmptyString(powerSampledValueTemplate.value)
             ? getRandomFloatFluctuatedRounded(
                 OCPP16ServiceUtils.getLimitFromSampledValueTemplateCustomValue(
                   powerSampledValueTemplate.value,
@@ -608,7 +615,7 @@ 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,
@@ -636,7 +643,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
             connectorMaximumAvailablePower,
             chargingStation.stationInfo.voltageOut!,
           );
-          currentMeasurandValues.allPhases = currentSampledValueTemplate.value
+          currentMeasurandValues.allPhases = isNotEmptyString(currentSampledValueTemplate.value)
             ? getRandomFloatFluctuatedRounded(
                 OCPP16ServiceUtils.getLimitFromSampledValueTemplateCustomValue(
                   currentSampledValueTemplate.value,
@@ -733,7 +740,7 @@ 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(