From 492cf6ab7b4e5231974ca1a9c219afbf0d53f4b9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 18 May 2022 15:26:56 +0200 Subject: [PATCH] Reduce a bit more the charging station instance memory footprint 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 | 104 +--------------- src/charging-station/ChargingStationUtils.ts | 113 +++++++++++++++++- .../ocpp/1.6/OCPP16ServiceUtils.ts | 52 +++++--- 3 files changed, 155 insertions(+), 114 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 89bb9d8e..fed9f42c 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -26,7 +26,6 @@ import ChargingStationConfiguration, { Section } from '../types/ChargingStationC import ChargingStationTemplate, { CurrentType, PowerUnits, - Voltage, WsOptions, } from '../types/ChargingStationTemplate'; import { @@ -35,7 +34,7 @@ import { SupportedFeatureProfiles, VendorDefaultParametersKey, } from '../types/ocpp/Configuration'; -import { MeterValue, MeterValueMeasurand, MeterValuePhase } from '../types/ocpp/MeterValues'; +import { MeterValue, MeterValueMeasurand } from '../types/ocpp/MeterValues'; import { StopTransactionReason, StopTransactionRequest, @@ -70,7 +69,6 @@ import OCPPIncomingRequestService from './ocpp/OCPPIncomingRequestService'; import OCPPRequestService from './ocpp/OCPPRequestService'; import { OCPPVersion } from '../types/ocpp/OCPPVersion'; import PerformanceStatistics from '../performance/PerformanceStatistics'; -import { SampledValueTemplate } from '../types/MeasurandPerPhaseSampledValueTemplates'; import { SupervisionUrlDistribution } from '../types/ConfigurationData'; import { URL } from 'url'; import Utils from '../utils/Utils'; @@ -223,21 +221,11 @@ export default class ChargingStation { } public getVoltageOut(): number | undefined { - const errMsg = `${this.logPrefix()} Unknown ${this.getCurrentOutType()} currentOutType in template file ${ - this.templateFile - }, cannot define default voltage out`; - let defaultVoltageOut: number; - switch (this.getCurrentOutType()) { - case CurrentType.AC: - defaultVoltageOut = Voltage.VOLTAGE_230; - break; - case CurrentType.DC: - defaultVoltageOut = Voltage.VOLTAGE_400; - break; - default: - logger.error(errMsg); - throw new Error(errMsg); - } + const defaultVoltageOut = ChargingStationUtils.getDefaultVoltageOut( + this.getCurrentOutType(), + this.templateFile, + this.logPrefix() + ); return !Utils.isUndefined(this.stationInfo.voltageOut) ? this.stationInfo.voltageOut : defaultVoltageOut; @@ -353,86 +341,6 @@ export default class ChargingStation { return localAuthListEnabled ? Utils.convertToBoolean(localAuthListEnabled.value) : false; } - public getSampledValueTemplate( - connectorId: number, - measurand: MeterValueMeasurand = MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER, - phase?: MeterValuePhase - ): SampledValueTemplate | undefined { - const onPhaseStr = phase ? `on phase ${phase} ` : ''; - if (!Constants.SUPPORTED_MEASURANDS.includes(measurand)) { - logger.warn( - `${this.logPrefix()} Trying to get unsupported MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}` - ); - return; - } - if ( - measurand !== MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER && - !ChargingStationConfigurationUtils.getConfigurationKey( - this, - StandardParametersKey.MeterValuesSampledData - )?.value.includes(measurand) - ) { - logger.debug( - `${this.logPrefix()} Trying to get MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId} not found in '${ - StandardParametersKey.MeterValuesSampledData - }' OCPP parameter` - ); - return; - } - const sampledValueTemplates: SampledValueTemplate[] = - this.getConnectorStatus(connectorId).MeterValues; - for ( - let index = 0; - !Utils.isEmptyArray(sampledValueTemplates) && index < sampledValueTemplates.length; - index++ - ) { - if ( - !Constants.SUPPORTED_MEASURANDS.includes( - sampledValueTemplates[index]?.measurand ?? - MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER - ) - ) { - logger.warn( - `${this.logPrefix()} Unsupported MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}` - ); - } else if ( - phase && - sampledValueTemplates[index]?.phase === phase && - sampledValueTemplates[index]?.measurand === measurand && - ChargingStationConfigurationUtils.getConfigurationKey( - this, - StandardParametersKey.MeterValuesSampledData - )?.value.includes(measurand) - ) { - return sampledValueTemplates[index]; - } else if ( - !phase && - !sampledValueTemplates[index].phase && - sampledValueTemplates[index]?.measurand === measurand && - ChargingStationConfigurationUtils.getConfigurationKey( - this, - StandardParametersKey.MeterValuesSampledData - )?.value.includes(measurand) - ) { - return sampledValueTemplates[index]; - } else if ( - measurand === MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER && - (!sampledValueTemplates[index].measurand || - sampledValueTemplates[index].measurand === measurand) - ) { - return sampledValueTemplates[index]; - } - } - if (measurand === MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER) { - const errorMsg = `${this.logPrefix()} Missing MeterValues for default measurand '${measurand}' in template on connectorId ${connectorId}`; - logger.error(errorMsg); - throw new Error(errorMsg); - } - logger.debug( - `${this.logPrefix()} No MeterValues for measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}` - ); - } - public getAutomaticTransactionGeneratorRequireAuthorize(): boolean { return this.stationInfo.AutomaticTransactionGenerator.requireAuthorize ?? true; } diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 26774b80..3e30fabc 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -1,11 +1,20 @@ import { ChargingProfile, ChargingSchedulePeriod } from '../types/ocpp/ChargingProfile'; import { ChargingProfileKindType, RecurrencyKindType } from '../types/ocpp/1.6/ChargingProfile'; -import ChargingStationTemplate, { AmpereUnits } from '../types/ChargingStationTemplate'; +import ChargingStationTemplate, { + AmpereUnits, + CurrentType, + Voltage, +} from '../types/ChargingStationTemplate'; +import { MeterValueMeasurand, MeterValuePhase } from '../types/ocpp/MeterValues'; import { BootNotificationRequest } from '../types/ocpp/Requests'; +import ChargingStation from './ChargingStation'; +import { ChargingStationConfigurationUtils } from './ChargingStationConfigurationUtils'; import ChargingStationInfo from '../types/ChargingStationInfo'; import Configuration from '../utils/Configuration'; import Constants from '../utils/Constants'; +import { SampledValueTemplate } from '../types/MeasurandPerPhaseSampledValueTemplates'; +import { StandardParametersKey } from '../types/ocpp/Configuration'; import Utils from '../utils/Utils'; import { WebSocketCloseEventStatusString } from '../types/WebSocket'; import { WorkerProcessType } from '../types/Worker'; @@ -325,6 +334,108 @@ export class ChargingStationUtils { return null; } + public static getDefaultVoltageOut( + currentType: CurrentType, + templateFile: string, + logPrefix: string + ): Voltage { + const errMsg = `${logPrefix} Unknown ${currentType} currentOutType in template file ${templateFile}, cannot define default voltage out`; + let defaultVoltageOut: number; + switch (currentType) { + case CurrentType.AC: + defaultVoltageOut = Voltage.VOLTAGE_230; + break; + case CurrentType.DC: + defaultVoltageOut = Voltage.VOLTAGE_400; + break; + default: + logger.error(errMsg); + throw new Error(errMsg); + } + return defaultVoltageOut; + } + + public static getSampledValueTemplate( + chargingStation: ChargingStation, + connectorId: number, + measurand: MeterValueMeasurand = MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER, + phase?: MeterValuePhase + ): SampledValueTemplate | undefined { + const onPhaseStr = phase ? `on phase ${phase} ` : ''; + if (!Constants.SUPPORTED_MEASURANDS.includes(measurand)) { + logger.warn( + `${chargingStation.logPrefix()} Trying to get unsupported MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}` + ); + return; + } + if ( + measurand !== MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER && + !ChargingStationConfigurationUtils.getConfigurationKey( + chargingStation, + StandardParametersKey.MeterValuesSampledData + )?.value.includes(measurand) + ) { + logger.debug( + `${chargingStation.logPrefix()} Trying to get MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId} not found in '${ + StandardParametersKey.MeterValuesSampledData + }' OCPP parameter` + ); + return; + } + const sampledValueTemplates: SampledValueTemplate[] = + chargingStation.getConnectorStatus(connectorId).MeterValues; + for ( + let index = 0; + !Utils.isEmptyArray(sampledValueTemplates) && index < sampledValueTemplates.length; + index++ + ) { + if ( + !Constants.SUPPORTED_MEASURANDS.includes( + sampledValueTemplates[index]?.measurand ?? + MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER + ) + ) { + logger.warn( + `${chargingStation.logPrefix()} Unsupported MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}` + ); + } else if ( + phase && + sampledValueTemplates[index]?.phase === phase && + sampledValueTemplates[index]?.measurand === measurand && + ChargingStationConfigurationUtils.getConfigurationKey( + chargingStation, + StandardParametersKey.MeterValuesSampledData + )?.value.includes(measurand) + ) { + return sampledValueTemplates[index]; + } else if ( + !phase && + !sampledValueTemplates[index].phase && + sampledValueTemplates[index]?.measurand === measurand && + ChargingStationConfigurationUtils.getConfigurationKey( + chargingStation, + StandardParametersKey.MeterValuesSampledData + )?.value.includes(measurand) + ) { + return sampledValueTemplates[index]; + } else if ( + measurand === MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER && + (!sampledValueTemplates[index].measurand || + sampledValueTemplates[index].measurand === measurand) + ) { + return sampledValueTemplates[index]; + } + } + if (measurand === MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER) { + const errorMsg = `${chargingStation.logPrefix()} Missing MeterValues for default measurand '${measurand}' in template on connectorId ${connectorId}`; + logger.error(errorMsg); + throw new Error(errorMsg); + } + logger.debug( + `${chargingStation.logPrefix()} No MeterValues for measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}` + ); + } + private static getRandomSerialNumberSuffix(params?: { randomBytesLength?: number; upperCase?: boolean; diff --git a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts index 7592713b..1c8e1369 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts @@ -24,6 +24,7 @@ import { } from '../../../types/ocpp/1.6/Configuration'; import type ChargingStation from '../../ChargingStation'; +import { ChargingStationUtils } from '../../ChargingStationUtils'; import Constants from '../../../utils/Constants'; import { ErrorType } from '../../../types/ocpp/ErrorType'; import MeasurandValues from '../../../types/MeasurandValues'; @@ -62,7 +63,8 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { }; const connector = chargingStation.getConnectorStatus(connectorId); // SoC measurand - const socSampledValueTemplate = chargingStation.getSampledValueTemplate( + const socSampledValueTemplate = ChargingStationUtils.getSampledValueTemplate( + chargingStation, connectorId, OCPP16MeterValueMeasurand.STATE_OF_CHARGE ); @@ -89,7 +91,8 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { } } // Voltage measurand - const voltageSampledValueTemplate = chargingStation.getSampledValueTemplate( + const voltageSampledValueTemplate = ChargingStationUtils.getSampledValueTemplate( + chargingStation, connectorId, OCPP16MeterValueMeasurand.VOLTAGE ); @@ -118,7 +121,8 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { ) { const phaseLineToNeutralValue = `L${phase}-N`; const voltagePhaseLineToNeutralSampledValueTemplate = - chargingStation.getSampledValueTemplate( + ChargingStationUtils.getSampledValueTemplate( + chargingStation, connectorId, OCPP16MeterValueMeasurand.VOLTAGE, phaseLineToNeutralValue as OCPP16MeterValuePhase @@ -152,7 +156,8 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { : chargingStation.getNumberOfPhases() }`; const voltagePhaseLineToLineSampledValueTemplate = - chargingStation.getSampledValueTemplate( + ChargingStationUtils.getSampledValueTemplate( + chargingStation, connectorId, OCPP16MeterValueMeasurand.VOLTAGE, phaseLineToLineValue as OCPP16MeterValuePhase @@ -187,24 +192,28 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { } } // Power.Active.Import measurand - const powerSampledValueTemplate = chargingStation.getSampledValueTemplate( + const powerSampledValueTemplate = ChargingStationUtils.getSampledValueTemplate( + chargingStation, connectorId, OCPP16MeterValueMeasurand.POWER_ACTIVE_IMPORT ); let powerPerPhaseSampledValueTemplates: MeasurandPerPhaseSampledValueTemplates = {}; if (chargingStation.getNumberOfPhases() === 3) { powerPerPhaseSampledValueTemplates = { - L1: chargingStation.getSampledValueTemplate( + L1: ChargingStationUtils.getSampledValueTemplate( + chargingStation, connectorId, OCPP16MeterValueMeasurand.POWER_ACTIVE_IMPORT, OCPP16MeterValuePhase.L1_N ), - L2: chargingStation.getSampledValueTemplate( + L2: ChargingStationUtils.getSampledValueTemplate( + chargingStation, connectorId, OCPP16MeterValueMeasurand.POWER_ACTIVE_IMPORT, OCPP16MeterValuePhase.L2_N ), - L3: chargingStation.getSampledValueTemplate( + L3: ChargingStationUtils.getSampledValueTemplate( + chargingStation, connectorId, OCPP16MeterValueMeasurand.POWER_ACTIVE_IMPORT, OCPP16MeterValuePhase.L3_N @@ -390,24 +399,28 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { } } // Current.Import measurand - const currentSampledValueTemplate = chargingStation.getSampledValueTemplate( + const currentSampledValueTemplate = ChargingStationUtils.getSampledValueTemplate( + chargingStation, connectorId, OCPP16MeterValueMeasurand.CURRENT_IMPORT ); let currentPerPhaseSampledValueTemplates: MeasurandPerPhaseSampledValueTemplates = {}; if (chargingStation.getNumberOfPhases() === 3) { currentPerPhaseSampledValueTemplates = { - L1: chargingStation.getSampledValueTemplate( + L1: ChargingStationUtils.getSampledValueTemplate( + chargingStation, connectorId, OCPP16MeterValueMeasurand.CURRENT_IMPORT, OCPP16MeterValuePhase.L1 ), - L2: chargingStation.getSampledValueTemplate( + L2: ChargingStationUtils.getSampledValueTemplate( + chargingStation, connectorId, OCPP16MeterValueMeasurand.CURRENT_IMPORT, OCPP16MeterValuePhase.L2 ), - L3: chargingStation.getSampledValueTemplate( + L3: ChargingStationUtils.getSampledValueTemplate( + chargingStation, connectorId, OCPP16MeterValueMeasurand.CURRENT_IMPORT, OCPP16MeterValuePhase.L3 @@ -594,7 +607,10 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { } } // Energy.Active.Import.Register measurand (default) - const energySampledValueTemplate = chargingStation.getSampledValueTemplate(connectorId); + const energySampledValueTemplate = ChargingStationUtils.getSampledValueTemplate( + chargingStation, + connectorId + ); if (energySampledValueTemplate) { OCPP16ServiceUtils.checkMeasurandPowerDivider( chargingStation, @@ -674,7 +690,10 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { sampledValue: [], }; // Energy.Active.Import.Register measurand (default) - const sampledValueTemplate = chargingStation.getSampledValueTemplate(connectorId); + const sampledValueTemplate = ChargingStationUtils.getSampledValueTemplate( + chargingStation, + connectorId + ); const unitDivider = sampledValueTemplate?.unit === MeterValueUnit.KILO_WATT_HOUR ? 1000 : 1; meterValue.sampledValue.push( OCPP16ServiceUtils.buildSampledValue( @@ -696,7 +715,10 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { sampledValue: [], }; // Energy.Active.Import.Register measurand (default) - const sampledValueTemplate = chargingStation.getSampledValueTemplate(connectorId); + const sampledValueTemplate = ChargingStationUtils.getSampledValueTemplate( + chargingStation, + connectorId + ); const unitDivider = sampledValueTemplate?.unit === MeterValueUnit.KILO_WATT_HOUR ? 1000 : 1; meterValue.sampledValue.push( OCPP16ServiceUtils.buildSampledValue( -- 2.34.1