Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
Reduce a bit more the charging station instance memory footprint
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
  • Loading branch information
Jérôme Benoit committed May 18, 2022
1 parent 8d0e34c commit 492cf6a
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 114 deletions.
104 changes: 6 additions & 98 deletions src/charging-station/ChargingStation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import ChargingStationConfiguration, { Section } from '../types/ChargingStationC
import ChargingStationTemplate, {
CurrentType,
PowerUnits,
Voltage,
WsOptions,
} from '../types/ChargingStationTemplate';
import {
Expand All @@ -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,
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
113 changes: 112 additions & 1 deletion src/charging-station/ChargingStationUtils.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 492cf6a

Please sign in to comment.