From 5a47f72ce72f346273cdaae1907056bcb98dfe0f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 12 Nov 2023 23:47:35 +0100 Subject: [PATCH] fix: avoid NaN at meterValues generation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 3 +- .../ocpp/1.6/OCPP16ServiceUtils.ts | 37 +++++++++++-------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 3f3248c9..7d9895b8 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -114,6 +114,7 @@ import { SupervisionUrlDistribution, SupportedFeatureProfiles, VendorParametersKey, + Voltage, type WSError, WebSocketCloseEventStatusCode, type WsOptions, @@ -2070,7 +2071,7 @@ export class ChargingStation extends EventEmitter { return (stationInfo ?? this.stationInfo).currentOutType ?? CurrentType.AC; } - private getVoltageOut(stationInfo?: ChargingStationInfo): number { + private getVoltageOut(stationInfo?: ChargingStationInfo): Voltage { return ( (stationInfo ?? this.stationInfo).voltageOut ?? getDefaultVoltageOut(this.getCurrentOutType(stationInfo), this.logPrefix(), this.templateFile) diff --git a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts index 02cbac8d..0086877c 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts @@ -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; @@ -202,10 +203,15 @@ 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) + : roundTo( + Math.sqrt(chargingStation.getNumberOfPhases()) * + chargingStation.stationInfo.voltageOut!, + 2, + ); const fluctuationPhaseLineToLinePercent = voltagePhaseLineToLineSampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT; @@ -215,7 +221,8 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { ); } const defaultVoltagePhaseLineToLineMeasurandValue = getRandomFloatFluctuatedRounded( - Voltage.VOLTAGE_400, + Math.sqrt(chargingStation.getNumberOfPhases()) * + chargingStation.stationInfo.voltageOut!, fluctuationPercent, ); meterValue.sampledValue.push( @@ -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, -- 2.34.1