From 884a6fdf01f5afe1185c330ded18fc64cc0a3075 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 2 Oct 2022 11:29:10 +0200 Subject: [PATCH] OCPP: separate 1.6 code from global one MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ocpp/1.6/OCPP16ServiceUtils.ts | 91 +------------------ src/charging-station/ocpp/OCPPServiceUtils.ts | 88 ++++++++++++++++++ 2 files changed, 91 insertions(+), 88 deletions(-) diff --git a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts index 2dc498e8..741db1d3 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts @@ -1,6 +1,5 @@ // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. -import BaseError from '../../../exception/BaseError'; import OCPPError from '../../../exception/OCPPError'; import { CurrentType, Voltage } from '../../../types/ChargingStationTemplate'; import type { @@ -8,6 +7,7 @@ import type { SampledValueTemplate, } from '../../../types/MeasurandPerPhaseSampledValueTemplates'; import type { MeasurandValues } from '../../../types/MeasurandValues'; +import type { OCPP16ChargingProfile } from '../../../types/ocpp/1.6/ChargingProfile'; import { OCPP16StandardParametersKey, OCPP16SupportedFeatureProfiles, @@ -25,16 +25,12 @@ import { OCPP16IncomingRequestCommand, OCPP16RequestCommand, } from '../../../types/ocpp/1.6/Requests'; -import type { ChargingProfile } from '../../../types/ocpp/ChargingProfile'; -import { StandardParametersKey } from '../../../types/ocpp/Configuration'; import { ErrorType } from '../../../types/ocpp/ErrorType'; -import { MeterValueMeasurand, type MeterValuePhase } from '../../../types/ocpp/MeterValues'; import Constants from '../../../utils/Constants'; import { ACElectricUtils, DCElectricUtils } from '../../../utils/ElectricUtils'; import logger from '../../../utils/Logger'; import Utils from '../../../utils/Utils'; import type ChargingStation from '../../ChargingStation'; -import { ChargingStationConfigurationUtils } from '../../ChargingStationConfigurationUtils'; import { OCPPServiceUtils } from '../OCPPServiceUtils'; export class OCPP16ServiceUtils extends OCPPServiceUtils { @@ -747,7 +743,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { public static setChargingProfile( chargingStation: ChargingStation, connectorId: number, - cp: ChargingProfile + cp: OCPP16ChargingProfile ): void { if (Utils.isNullOrUndefined(chargingStation.getConnectorStatus(connectorId).chargingProfiles)) { logger.error( @@ -765,7 +761,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { if (!Utils.isEmptyArray(chargingStation.getConnectorStatus(connectorId).chargingProfiles)) { chargingStation .getConnectorStatus(connectorId) - .chargingProfiles?.forEach((chargingProfile: ChargingProfile, index: number) => { + .chargingProfiles?.forEach((chargingProfile: OCPP16ChargingProfile, index: number) => { if ( chargingProfile.chargingProfileId === cp.chargingProfileId || (chargingProfile.stackLevel === cp.stackLevel && @@ -779,87 +775,6 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { !cpReplaced && chargingStation.getConnectorStatus(connectorId).chargingProfiles?.push(cp); } - private 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) === false) { - 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 - ) === false - ) { - 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) === true - ) { - return sampledValueTemplates[index]; - } else if ( - !phase && - !sampledValueTemplates[index].phase && - sampledValueTemplates[index]?.measurand === measurand && - ChargingStationConfigurationUtils.getConfigurationKey( - chargingStation, - StandardParametersKey.MeterValuesSampledData - )?.value.includes(measurand) === true - ) { - 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 = `Missing MeterValues for default measurand '${measurand}' in template on connectorId ${connectorId}`; - logger.error(`${chargingStation.logPrefix()} ${errorMsg}`); - throw new BaseError(errorMsg); - } - logger.debug( - `${chargingStation.logPrefix()} No MeterValues for measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}` - ); - } - private static buildSampledValue( sampledValueTemplate: SampledValueTemplate, value: number, diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index dcf3a999..c524d454 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -1,9 +1,16 @@ import type { DefinedError, ErrorObject } from 'ajv'; +import BaseError from '../../exception/BaseError'; +import type { SampledValueTemplate } from '../../types/MeasurandPerPhaseSampledValueTemplates'; +import { StandardParametersKey } from '../../types/ocpp/Configuration'; import { ErrorType } from '../../types/ocpp/ErrorType'; +import { MeterValueMeasurand, type MeterValuePhase } from '../../types/ocpp/MeterValues'; import { IncomingRequestCommand, RequestCommand } from '../../types/ocpp/Requests'; +import Constants from '../../utils/Constants'; import logger from '../../utils/Logger'; +import Utils from '../../utils/Utils'; import type ChargingStation from '../ChargingStation'; +import { ChargingStationConfigurationUtils } from '../ChargingStationConfigurationUtils'; export class OCPPServiceUtils { protected constructor() { @@ -66,6 +73,87 @@ export class OCPPServiceUtils { return false; } + protected 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) === false) { + 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 + ) === false + ) { + 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) === true + ) { + return sampledValueTemplates[index]; + } else if ( + !phase && + !sampledValueTemplates[index].phase && + sampledValueTemplates[index]?.measurand === measurand && + ChargingStationConfigurationUtils.getConfigurationKey( + chargingStation, + StandardParametersKey.MeterValuesSampledData + )?.value.includes(measurand) === true + ) { + 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 = `Missing MeterValues for default measurand '${measurand}' in template on connectorId ${connectorId}`; + logger.error(`${chargingStation.logPrefix()} ${errorMsg}`); + throw new BaseError(errorMsg); + } + logger.debug( + `${chargingStation.logPrefix()} No MeterValues for measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}` + ); + } + protected static getLimitFromSampledValueTemplateCustomValue( value: string, limit: number, -- 2.34.1