From e5f8aeda400d3b4ada694517733ad4dd625dab50 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 27 Mar 2026 20:10:32 +0100 Subject: [PATCH] fix: replace incorrect zero fallbacks for maximumPower and maximumAmperage Station physical properties (maximumPower, maximumAmperage) are validated > 0 at startup. Using ?? 0 as fallback silently produces invalid states: 0W power limits blocking all charging, 0A amperage in config keys. Replace with proper null guards that log errors and fail-open: - getConnectorMaximumAvailablePower: return Infinity (no limit known) - getMaximumAmperage: return undefined (caller already handles it) - initializeOcppConfiguration: skip config key, log error - getChargingStationChargingProfilesLimit: return limit uncapped - getConnectorChargingProfilesLimit: return limit uncapped --- src/charging-station/ChargingStation.ts | 42 ++++++++++++++++++++----- src/charging-station/Helpers.ts | 12 +++++-- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 4a59aac3..d2049f40 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -504,7 +504,16 @@ export class ChargingStation extends EventEmitter { ) : DCElectricUtils.power(voltageOut, amperageLimitation)) / (this.powerDivider ?? 1) } - const connectorMaximumPower = (this.stationInfo?.maximumPower ?? 0) / (this.powerDivider ?? 1) + const maximumPower = this.stationInfo?.maximumPower + if (maximumPower == null || maximumPower <= 0) { + logger.error( + `${this.logPrefix()} getConnectorMaximumAvailablePower: maximumPower is ${ + maximumPower?.toString() ?? 'undefined' + }, cannot compute connector maximum power` + ) + return Number.POSITIVE_INFINITY + } + const connectorMaximumPower = maximumPower / (this.powerDivider ?? 1) const chargingStationChargingProfilesLimit = (getChargingStationChargingProfilesLimit(this) ?? Number.POSITIVE_INFINITY) / (this.powerDivider ?? 1) @@ -1443,7 +1452,15 @@ export class ChargingStation extends EventEmitter { private getMaximumAmperage (stationInfo?: ChargingStationInfo): number | undefined { const localStationInfo = stationInfo ?? this.stationInfo - const maximumPower = localStationInfo?.maximumPower ?? 0 + const maximumPower = localStationInfo?.maximumPower + if (maximumPower == null || maximumPower <= 0) { + logger.error( + `${this.logPrefix()} getMaximumAmperage: maximumPower is ${ + maximumPower?.toString() ?? 'undefined' + }, cannot compute maximum amperage` + ) + return undefined + } switch (this.getCurrentOutType(stationInfo)) { case CurrentType.AC: return ACElectricUtils.amperagePerPhaseFromPower( @@ -2062,12 +2079,21 @@ export class ChargingStation extends EventEmitter { isNotEmptyString(this.stationInfo?.amperageLimitationOcppKey) && getConfigurationKey(this, this.stationInfo.amperageLimitationOcppKey) == null ) { - addConfigurationKey( - this, - this.stationInfo.amperageLimitationOcppKey, - // prettier-ignore - ((this.stationInfo.maximumAmperage ?? 0) * getAmperageLimitationUnitDivider(this.stationInfo)).toString() - ) + const maximumAmperage = this.stationInfo.maximumAmperage + if (maximumAmperage != null && maximumAmperage > 0) { + addConfigurationKey( + this, + this.stationInfo.amperageLimitationOcppKey, + // prettier-ignore + (maximumAmperage * getAmperageLimitationUnitDivider(this.stationInfo)).toString() + ) + } else { + logger.error( + `${this.logPrefix()} initializeOcppConfiguration: maximumAmperage is ${ + maximumAmperage?.toString() ?? 'undefined' + }, cannot set amperage limitation configuration key` + ) + } } if (getConfigurationKey(this, StandardParametersKey.SupportedFeatureProfiles) == null) { addConfigurationKey( diff --git a/src/charging-station/Helpers.ts b/src/charging-station/Helpers.ts index d3ffaace..f2c1c9f5 100644 --- a/src/charging-station/Helpers.ts +++ b/src/charging-station/Helpers.ts @@ -788,7 +788,10 @@ export const getChargingStationChargingProfilesLimit = ( const chargingProfilesLimit = getChargingProfilesLimit(chargingStation, 0, chargingProfiles) if (chargingProfilesLimit != null) { const limit = buildChargingProfilesLimit(chargingStation, chargingProfilesLimit) - const chargingStationMaximumPower = chargingStation.stationInfo?.maximumPower ?? 0 + const chargingStationMaximumPower = chargingStation.stationInfo?.maximumPower + if (chargingStationMaximumPower == null) { + return limit + } if (limit > chargingStationMaximumPower) { logger.error( `${chargingStation.logPrefix()} ${moduleName}.getChargingStationChargingProfilesLimit: Charging profile id ${getChargingProfileId(chargingProfilesLimit.chargingProfile)} limit ${limit.toString()} is greater than charging station maximum ${chargingStationMaximumPower.toString()}: %j`, @@ -851,8 +854,11 @@ export const getConnectorChargingProfilesLimit = ( ) if (chargingProfilesLimit != null) { const limit = buildChargingProfilesLimit(chargingStation, chargingProfilesLimit) - const connectorMaximumPower = - (chargingStation.stationInfo?.maximumPower ?? 0) / (chargingStation.powerDivider ?? 1) + const maximumPower = chargingStation.stationInfo?.maximumPower + if (maximumPower == null) { + return limit + } + const connectorMaximumPower = maximumPower / (chargingStation.powerDivider ?? 1) if (limit > connectorMaximumPower) { logger.error( `${chargingStation.logPrefix()} ${moduleName}.getConnectorChargingProfilesLimit: Charging profile id ${getChargingProfileId(chargingProfilesLimit.chargingProfile)} limit ${limit.toString()} is greater than connector ${connectorId.toString()} maximum ${connectorMaximumPower.toString()}: %j`, -- 2.43.0