X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCPP16ServiceUtils.ts;h=932d4515f6d0fcc7d0c259f3fe8ff000e11423ed;hb=4cf856f0ff988b709da7b9185913266146e82add;hp=089ecc76e065d4eb01d3ca36da3a71a571e7147c;hpb=edd134392e237a3242dc2093341df70244c51472;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts index 089ecc76..932d4515 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts @@ -1,37 +1,36 @@ // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. -import OCPPError from '../../../exception/OCPPError'; -import { CurrentType, Voltage } from '../../../types/ChargingStationTemplate'; -import type { - MeasurandPerPhaseSampledValueTemplates, - SampledValueTemplate, -} from '../../../types/MeasurandPerPhaseSampledValueTemplates'; -import type { MeasurandValues } from '../../../types/MeasurandValues'; -import type { OCPP16ChargingProfile } from '../../../types/ocpp/1.6/ChargingProfile'; -import { - OCPP16StandardParametersKey, - OCPP16SupportedFeatureProfiles, -} from '../../../types/ocpp/1.6/Configuration'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +import type { JSONSchemaType } from 'ajv'; + +import type { ChargingStation } from '../../../charging-station'; +import { OCPPError } from '../../../exception'; import { + CurrentType, + ErrorType, + type JsonType, + type MeasurandPerPhaseSampledValueTemplates, + type MeasurandValues, MeterValueContext, MeterValueLocation, MeterValueUnit, + type OCPP16ChargingProfile, + type OCPP16IncomingRequestCommand, type OCPP16MeterValue, OCPP16MeterValueMeasurand, OCPP16MeterValuePhase, - type OCPP16SampledValue, -} from '../../../types/ocpp/1.6/MeterValues'; -import { - type OCPP16IncomingRequestCommand, OCPP16RequestCommand, -} from '../../../types/ocpp/1.6/Requests'; -import { ErrorType } from '../../../types/ocpp/ErrorType'; -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 { OCPPServiceUtils } from '../OCPPServiceUtils'; + type OCPP16SampledValue, + OCPP16StandardParametersKey, + type OCPP16SupportedFeatureProfiles, + OCPPVersion, + type SampledValueTemplate, + Voltage, +} from '../../../types'; +import { ACElectricUtils, Constants, DCElectricUtils, Utils, logger } from '../../../utils'; +import { OCPPServiceUtils } from '../internal'; export class OCPP16ServiceUtils extends OCPPServiceUtils { public static checkFeatureProfile( @@ -58,7 +57,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { debug = false ): OCPP16MeterValue { const meterValue: OCPP16MeterValue = { - timestamp: new Date().toISOString(), + timestamp: new Date(), sampledValue: [], }; const connector = chargingStation.getConnectorStatus(connectorId); @@ -69,24 +68,32 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { OCPP16MeterValueMeasurand.STATE_OF_CHARGE ); if (socSampledValueTemplate) { + const socMaximumValue = 100; + const socMinimumValue = socSampledValueTemplate.minimumValue ?? 0; const socSampledValueTemplateValue = socSampledValueTemplate.value ? Utils.getRandomFloatFluctuatedRounded( parseInt(socSampledValueTemplate.value), socSampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT ) - : Utils.getRandomInteger(100); + : Utils.getRandomInteger(socMaximumValue, socMinimumValue); meterValue.sampledValue.push( OCPP16ServiceUtils.buildSampledValue(socSampledValueTemplate, socSampledValueTemplateValue) ); const sampledValuesIndex = meterValue.sampledValue.length - 1; - if (Utils.convertToInt(meterValue.sampledValue[sampledValuesIndex].value) > 100 || debug) { + if ( + Utils.convertToInt(meterValue.sampledValue[sampledValuesIndex].value) > socMaximumValue || + Utils.convertToInt(meterValue.sampledValue[sampledValuesIndex].value) < socMinimumValue || + debug + ) { logger.error( `${chargingStation.logPrefix()} MeterValues measurand ${ meterValue.sampledValue[sampledValuesIndex].measurand ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER - }: connectorId ${connectorId}, transaction ${connector.transactionId}, value: ${ + }: connectorId ${connectorId}, transaction ${ + connector?.transactionId + }, value: ${socMinimumValue}/${ meterValue.sampledValue[sampledValuesIndex].value - }/100` + }/${socMaximumValue}}` ); } } @@ -145,7 +152,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { OCPP16ServiceUtils.buildSampledValue( voltagePhaseLineToNeutralSampledValueTemplate ?? voltageSampledValueTemplate, voltagePhaseLineToNeutralMeasurandValue ?? voltageMeasurandValue, - null, + undefined, phaseLineToNeutralValue as OCPP16MeterValuePhase ) ); @@ -184,7 +191,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { OCPP16ServiceUtils.buildSampledValue( voltagePhaseLineToLineSampledValueTemplate ?? voltageSampledValueTemplate, voltagePhaseLineToLineMeasurandValue ?? defaultVoltagePhaseLineToLineMeasurandValue, - null, + undefined, phaseLineToLineValue as OCPP16MeterValuePhase ) ); @@ -242,6 +249,10 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { const connectorMaximumPowerPerPhase = Math.round( connectorMaximumAvailablePower / chargingStation.getNumberOfPhases() ); + const connectorMinimumPower = Math.round(powerSampledValueTemplate.minimumValue) ?? 0; + const connectorMinimumPowerPerPhase = Math.round( + connectorMinimumPower / chargingStation.getNumberOfPhases() + ); switch (chargingStation.getCurrentOutType()) { case CurrentType.AC: if (chargingStation.getNumberOfPhases() === 3) { @@ -292,15 +303,24 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { powerMeasurandValues.L1 = phase1FluctuatedValue ?? defaultFluctuatedPowerPerPhase ?? - Utils.getRandomFloatRounded(connectorMaximumPowerPerPhase / unitDivider); + Utils.getRandomFloatRounded( + connectorMaximumPowerPerPhase / unitDivider, + connectorMinimumPowerPerPhase / unitDivider + ); powerMeasurandValues.L2 = phase2FluctuatedValue ?? defaultFluctuatedPowerPerPhase ?? - Utils.getRandomFloatRounded(connectorMaximumPowerPerPhase / unitDivider); + Utils.getRandomFloatRounded( + connectorMaximumPowerPerPhase / unitDivider, + connectorMinimumPowerPerPhase / unitDivider + ); powerMeasurandValues.L3 = phase3FluctuatedValue ?? defaultFluctuatedPowerPerPhase ?? - Utils.getRandomFloatRounded(connectorMaximumPowerPerPhase / unitDivider); + Utils.getRandomFloatRounded( + connectorMaximumPowerPerPhase / unitDivider, + connectorMinimumPowerPerPhase / unitDivider + ); } else { powerMeasurandValues.L1 = powerSampledValueTemplate.value ? Utils.getRandomFloatFluctuatedRounded( @@ -312,7 +332,10 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { powerSampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT ) - : Utils.getRandomFloatRounded(connectorMaximumPower / unitDivider); + : Utils.getRandomFloatRounded( + connectorMaximumPower / unitDivider, + connectorMinimumPower / unitDivider + ); powerMeasurandValues.L2 = 0; powerMeasurandValues.L3 = 0; } @@ -332,7 +355,10 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { powerSampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT ) - : Utils.getRandomFloatRounded(connectorMaximumPower / unitDivider); + : Utils.getRandomFloatRounded( + connectorMaximumPower / unitDivider, + connectorMinimumPower / unitDivider + ); break; default: logger.error(`${chargingStation.logPrefix()} ${errMsg}`); @@ -346,16 +372,21 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { ); const sampledValuesIndex = meterValue.sampledValue.length - 1; const connectorMaximumPowerRounded = Utils.roundTo(connectorMaximumPower / unitDivider, 2); + const connectorMinimumPowerRounded = Utils.roundTo(connectorMinimumPower / unitDivider, 2); if ( Utils.convertToFloat(meterValue.sampledValue[sampledValuesIndex].value) > connectorMaximumPowerRounded || + Utils.convertToFloat(meterValue.sampledValue[sampledValuesIndex].value) < + connectorMinimumPowerRounded || debug ) { logger.error( `${chargingStation.logPrefix()} MeterValues measurand ${ meterValue.sampledValue[sampledValuesIndex].measurand ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER - }: connectorId ${connectorId}, transaction ${connector.transactionId}, value: ${ + }: connectorId ${connectorId}, transaction ${ + connector?.transactionId + }, value: ${connectorMinimumPowerRounded}/${ meterValue.sampledValue[sampledValuesIndex].value }/${connectorMaximumPowerRounded}` ); @@ -371,7 +402,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { (powerPerPhaseSampledValueTemplates[`L${phase}`] as SampledValueTemplate) ?? powerSampledValueTemplate, powerMeasurandValues[`L${phase}`] as number, - null, + undefined, phaseValue as OCPP16MeterValuePhase ) ); @@ -380,9 +411,15 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { connectorMaximumPowerPerPhase / unitDivider, 2 ); + const connectorMinimumPowerPerPhaseRounded = Utils.roundTo( + connectorMinimumPowerPerPhase / unitDivider, + 2 + ); if ( Utils.convertToFloat(meterValue.sampledValue[sampledValuesPerPhaseIndex].value) > connectorMaximumPowerPerPhaseRounded || + Utils.convertToFloat(meterValue.sampledValue[sampledValuesPerPhaseIndex].value) < + connectorMinimumPowerPerPhaseRounded || debug ) { logger.error( @@ -391,7 +428,9 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER }: phase ${ meterValue.sampledValue[sampledValuesPerPhaseIndex].phase - }, connectorId ${connectorId}, transaction ${connector.transactionId}, value: ${ + }, connectorId ${connectorId}, transaction ${ + connector?.transactionId + }, value: ${connectorMinimumPowerPerPhaseRounded}/${ meterValue.sampledValue[sampledValuesPerPhaseIndex].value }/${connectorMaximumPowerPerPhaseRounded}` ); @@ -444,6 +483,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { const currentMeasurandValues: MeasurandValues = {} as MeasurandValues; const connectorMaximumAvailablePower = chargingStation.getConnectorMaximumAvailablePower(connectorId); + const connectorMinimumAmperage = currentSampledValueTemplate.minimumValue ?? 0; let connectorMaximumAmperage: number; switch (chargingStation.getCurrentOutType()) { case CurrentType.AC: @@ -500,15 +540,15 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { currentMeasurandValues.L1 = phase1FluctuatedValue ?? defaultFluctuatedAmperagePerPhase ?? - Utils.getRandomFloatRounded(connectorMaximumAmperage); + Utils.getRandomFloatRounded(connectorMaximumAmperage, connectorMinimumAmperage); currentMeasurandValues.L2 = phase2FluctuatedValue ?? defaultFluctuatedAmperagePerPhase ?? - Utils.getRandomFloatRounded(connectorMaximumAmperage); + Utils.getRandomFloatRounded(connectorMaximumAmperage, connectorMinimumAmperage); currentMeasurandValues.L3 = phase3FluctuatedValue ?? defaultFluctuatedAmperagePerPhase ?? - Utils.getRandomFloatRounded(connectorMaximumAmperage); + Utils.getRandomFloatRounded(connectorMaximumAmperage, connectorMinimumAmperage); } else { currentMeasurandValues.L1 = currentSampledValueTemplate.value ? Utils.getRandomFloatFluctuatedRounded( @@ -520,7 +560,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { currentSampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT ) - : Utils.getRandomFloatRounded(connectorMaximumAmperage); + : Utils.getRandomFloatRounded(connectorMaximumAmperage, connectorMinimumAmperage); currentMeasurandValues.L2 = 0; currentMeasurandValues.L3 = 0; } @@ -545,7 +585,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { currentSampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT ) - : Utils.getRandomFloatRounded(connectorMaximumAmperage); + : Utils.getRandomFloatRounded(connectorMaximumAmperage, connectorMinimumAmperage); break; default: logger.error(`${chargingStation.logPrefix()} ${errMsg}`); @@ -561,13 +601,17 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { if ( Utils.convertToFloat(meterValue.sampledValue[sampledValuesIndex].value) > connectorMaximumAmperage || + Utils.convertToFloat(meterValue.sampledValue[sampledValuesIndex].value) < + connectorMinimumAmperage || debug ) { logger.error( `${chargingStation.logPrefix()} MeterValues measurand ${ meterValue.sampledValue[sampledValuesIndex].measurand ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER - }: connectorId ${connectorId}, transaction ${connector.transactionId}, value: ${ + }: connectorId ${connectorId}, transaction ${ + connector?.transactionId + }, value: ${connectorMinimumAmperage}/${ meterValue.sampledValue[sampledValuesIndex].value }/${connectorMaximumAmperage}` ); @@ -583,7 +627,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { (currentPerPhaseSampledValueTemplates[phaseValue] as SampledValueTemplate) ?? currentSampledValueTemplate, currentMeasurandValues[phaseValue] as number, - null, + undefined, phaseValue as OCPP16MeterValuePhase ) ); @@ -591,6 +635,8 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { if ( Utils.convertToFloat(meterValue.sampledValue[sampledValuesPerPhaseIndex].value) > connectorMaximumAmperage || + Utils.convertToFloat(meterValue.sampledValue[sampledValuesPerPhaseIndex].value) < + connectorMinimumAmperage || debug ) { logger.error( @@ -599,7 +645,9 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER }: phase ${ meterValue.sampledValue[sampledValuesPerPhaseIndex].phase - }, connectorId ${connectorId}, transaction ${connector.transactionId}, value: ${ + }, connectorId ${connectorId}, transaction ${ + connector?.transactionId + }, value: ${connectorMinimumAmperage}/${ meterValue.sampledValue[sampledValuesPerPhaseIndex].value }/${connectorMaximumAmperage}` ); @@ -669,7 +717,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { meterValue.sampledValue[sampledValuesIndex].measurand ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER }: connectorId ${connectorId}, transaction ${ - connector.transactionId + connector?.transactionId }, value: ${energyValueRounded}/${connectorMaximumEnergyRounded}, duration: ${Utils.roundTo( interval / (3600 * 1000), 4 @@ -686,7 +734,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { meterStart: number ): OCPP16MeterValue { const meterValue: OCPP16MeterValue = { - timestamp: new Date().toISOString(), + timestamp: new Date(), sampledValue: [], }; // Energy.Active.Import.Register measurand (default) @@ -711,7 +759,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { meterStop: number ): OCPP16MeterValue { const meterValue: OCPP16MeterValue = { - timestamp: new Date().toISOString(), + timestamp: new Date(), sampledValue: [], }; // Energy.Active.Import.Register measurand (default) @@ -745,23 +793,27 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { connectorId: number, cp: OCPP16ChargingProfile ): void { - if (Utils.isNullOrUndefined(chargingStation.getConnectorStatus(connectorId).chargingProfiles)) { + if ( + Utils.isNullOrUndefined(chargingStation.getConnectorStatus(connectorId)?.chargingProfiles) + ) { logger.error( `${chargingStation.logPrefix()} Trying to set a charging profile on connectorId ${connectorId} with an uninitialized charging profiles array attribute, applying deferred initialization` ); chargingStation.getConnectorStatus(connectorId).chargingProfiles = []; } - if (Array.isArray(chargingStation.getConnectorStatus(connectorId).chargingProfiles) === false) { + if ( + Array.isArray(chargingStation.getConnectorStatus(connectorId)?.chargingProfiles) === false + ) { logger.error( `${chargingStation.logPrefix()} Trying to set a charging profile on connectorId ${connectorId} with an improper attribute type for the charging profiles array, applying proper type initialization` ); chargingStation.getConnectorStatus(connectorId).chargingProfiles = []; } let cpReplaced = false; - if (!Utils.isEmptyArray(chargingStation.getConnectorStatus(connectorId).chargingProfiles)) { + if (Utils.isNotEmptyArray(chargingStation.getConnectorStatus(connectorId)?.chargingProfiles)) { chargingStation .getConnectorStatus(connectorId) - .chargingProfiles?.forEach((chargingProfile: OCPP16ChargingProfile, index: number) => { + ?.chargingProfiles?.forEach((chargingProfile: OCPP16ChargingProfile, index: number) => { if ( chargingProfile.chargingProfileId === cp.chargingProfileId || (chargingProfile.stackLevel === cp.stackLevel && @@ -772,7 +824,20 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { } }); } - !cpReplaced && chargingStation.getConnectorStatus(connectorId).chargingProfiles?.push(cp); + !cpReplaced && chargingStation.getConnectorStatus(connectorId)?.chargingProfiles?.push(cp); + } + + public static parseJsonSchemaFile( + relativePath: string, + moduleName?: string, + methodName?: string + ): JSONSchemaType { + return super.parseJsonSchemaFile( + path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath), + OCPPVersion.VERSION_16, + moduleName, + methodName + ); } private static buildSampledValue(