X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2FOCPPServiceUtils.ts;h=256544fa50090a5a88319f08fdcbe47c2c845f05;hb=cffc32b7bc5a6569525e92b562af8a93760bc339;hp=6a2e28ac50e80272fd24f225239f61c6d9196a9a;hpb=a974c8e4b8a98c9450be49546a77be0d03e9f512;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index 6a2e28ac..256544fa 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -1,3 +1,4 @@ +import { randomInt } from 'node:crypto' import { readFileSync } from 'node:fs' import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' @@ -5,9 +6,6 @@ import { fileURLToPath } from 'node:url' import type { DefinedError, ErrorObject, JSONSchemaType } from 'ajv' import { isDate } from 'date-fns' -import { OCPP16Constants } from './1.6/OCPP16Constants.js' -import { OCPP20Constants } from './2.0/OCPP20Constants.js' -import { OCPPConstants } from './OCPPConstants.js' import { type ChargingStation, getConfigurationKey, @@ -20,7 +18,8 @@ import { type AuthorizeResponse, ChargePointErrorCode, ChargingStationEvents, - type ConnectorStatusEnum, + type ConnectorStatus, + ConnectorStatusEnum, CurrentType, ErrorType, FileType, @@ -51,23 +50,25 @@ import { import { ACElectricUtils, Constants, - DCElectricUtils, convertToFloat, convertToInt, + DCElectricUtils, getRandomFloatFluctuatedRounded, getRandomFloatRounded, - getRandomInteger, handleFileException, isNotEmptyArray, isNotEmptyString, - logPrefix, logger, + logPrefix, max, min, roundTo } from '../../utils/index.js' +import { OCPP16Constants } from './1.6/OCPP16Constants.js' +import { OCPP20Constants } from './2.0/OCPP20Constants.js' +import { OCPPConstants } from './OCPPConstants.js' -export const getMessageTypeString = (messageType: MessageType): string => { +export const getMessageTypeString = (messageType: MessageType | undefined): string => { switch (messageType) { case MessageType.CALL_MESSAGE: return 'request' @@ -80,7 +81,7 @@ export const getMessageTypeString = (messageType: MessageType): string => { } } -export const buildStatusNotificationRequest = ( +const buildStatusNotificationRequest = ( chargingStation: ChargingStation, connectorId: number, status: ConnectorStatusEnum, @@ -194,6 +195,21 @@ export const sendAndSetConnectorStatus = async ( }) } +export const restoreConnectorStatus = async ( + chargingStation: ChargingStation, + connectorId: number, + connectorStatus: ConnectorStatus | undefined +): Promise => { + if ( + connectorStatus?.reservation != null && + connectorStatus.status !== ConnectorStatusEnum.Reserved + ) { + await sendAndSetConnectorStatus(chargingStation, connectorId, ConnectorStatusEnum.Reserved) + } else if (connectorStatus?.status !== ConnectorStatusEnum.Available) { + await sendAndSetConnectorStatus(chargingStation, connectorId, ConnectorStatusEnum.Available) + } +} + const checkConnectorStatusTransition = ( chargingStation: ChargingStation, connectorId: number, @@ -284,7 +300,7 @@ export const buildMeterValue = ( parseInt(socSampledValueTemplate.value), socSampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT ) - : getRandomInteger(socMaximumValue, socMinimumValue) + : randomInt(socMinimumValue, socMaximumValue) meterValue.sampledValue.push( buildSampledValue(socSampledValueTemplate, socSampledValueTemplateValue) ) @@ -443,7 +459,6 @@ export const buildMeterValue = ( } } if (powerSampledValueTemplate != null) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion checkMeasurandPowerDivider(chargingStation, powerSampledValueTemplate.measurand) const errMsg = `MeterValues measurand ${ powerSampledValueTemplate.measurand ?? MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER @@ -491,7 +506,7 @@ export const buildMeterValue = ( ) ? getRandomFloatFluctuatedRounded( getLimitFromSampledValueTemplateCustomValue( - powerPerPhaseSampledValueTemplates.L1?.value, + powerPerPhaseSampledValueTemplates.L1.value, connectorMaximumPowerPerPhase / unitDivider, connectorMinimumPowerPerPhase / unitDivider, { @@ -500,7 +515,7 @@ export const buildMeterValue = ( fallbackValue: connectorMinimumPowerPerPhase / unitDivider } ), - powerPerPhaseSampledValueTemplates.L1?.fluctuationPercent ?? + powerPerPhaseSampledValueTemplates.L1.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT ) : undefined @@ -509,7 +524,7 @@ export const buildMeterValue = ( ) ? getRandomFloatFluctuatedRounded( getLimitFromSampledValueTemplateCustomValue( - powerPerPhaseSampledValueTemplates.L2?.value, + powerPerPhaseSampledValueTemplates.L2.value, connectorMaximumPowerPerPhase / unitDivider, connectorMinimumPowerPerPhase / unitDivider, { @@ -518,7 +533,7 @@ export const buildMeterValue = ( fallbackValue: connectorMinimumPowerPerPhase / unitDivider } ), - powerPerPhaseSampledValueTemplates.L2?.fluctuationPercent ?? + powerPerPhaseSampledValueTemplates.L2.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT ) : undefined @@ -527,7 +542,7 @@ export const buildMeterValue = ( ) ? getRandomFloatFluctuatedRounded( getLimitFromSampledValueTemplateCustomValue( - powerPerPhaseSampledValueTemplates.L3?.value, + powerPerPhaseSampledValueTemplates.L3.value, connectorMaximumPowerPerPhase / unitDivider, connectorMinimumPowerPerPhase / unitDivider, { @@ -536,7 +551,7 @@ export const buildMeterValue = ( fallbackValue: connectorMinimumPowerPerPhase / unitDivider } ), - powerPerPhaseSampledValueTemplates.L3?.fluctuationPercent ?? + powerPerPhaseSampledValueTemplates.L3.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT ) : undefined @@ -757,7 +772,7 @@ export const buildMeterValue = ( ) ? getRandomFloatFluctuatedRounded( getLimitFromSampledValueTemplateCustomValue( - currentPerPhaseSampledValueTemplates.L1?.value, + currentPerPhaseSampledValueTemplates.L1.value, connectorMaximumAmperage, connectorMinimumAmperage, { @@ -766,7 +781,7 @@ export const buildMeterValue = ( fallbackValue: connectorMinimumAmperage } ), - currentPerPhaseSampledValueTemplates.L1?.fluctuationPercent ?? + currentPerPhaseSampledValueTemplates.L1.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT ) : undefined @@ -775,7 +790,7 @@ export const buildMeterValue = ( ) ? getRandomFloatFluctuatedRounded( getLimitFromSampledValueTemplateCustomValue( - currentPerPhaseSampledValueTemplates.L2?.value, + currentPerPhaseSampledValueTemplates.L2.value, connectorMaximumAmperage, connectorMinimumAmperage, { @@ -784,7 +799,7 @@ export const buildMeterValue = ( fallbackValue: connectorMinimumAmperage } ), - currentPerPhaseSampledValueTemplates.L2?.fluctuationPercent ?? + currentPerPhaseSampledValueTemplates.L2.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT ) : undefined @@ -793,7 +808,7 @@ export const buildMeterValue = ( ) ? getRandomFloatFluctuatedRounded( getLimitFromSampledValueTemplateCustomValue( - currentPerPhaseSampledValueTemplates.L3?.value, + currentPerPhaseSampledValueTemplates.L3.value, connectorMaximumAmperage, connectorMinimumAmperage, { @@ -802,7 +817,7 @@ export const buildMeterValue = ( fallbackValue: connectorMinimumAmperage } ), - currentPerPhaseSampledValueTemplates.L3?.fluctuationPercent ?? + currentPerPhaseSampledValueTemplates.L3.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT ) : undefined @@ -931,7 +946,6 @@ export const buildMeterValue = ( // Energy.Active.Import.Register measurand (default) energySampledValueTemplate = getSampledValueTemplate(chargingStation, connectorId) if (energySampledValueTemplate != null) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion checkMeasurandPowerDivider(chargingStation, energySampledValueTemplate.measurand) const unitDivider = energySampledValueTemplate.unit === MeterValueUnit.KILO_WATT_HOUR ? 1000 : 1 @@ -1228,10 +1242,11 @@ const getMeasurandDefaultLocation = ( // eslint-disable-next-line @typescript-eslint/no-extraneous-class export class OCPPServiceUtils { - public static getMessageTypeString = getMessageTypeString - public static sendAndSetConnectorStatus = sendAndSetConnectorStatus - public static isIdTagAuthorized = isIdTagAuthorized - public static buildTransactionEndMeterValue = buildTransactionEndMeterValue + public static readonly getMessageTypeString = getMessageTypeString + public static readonly sendAndSetConnectorStatus = sendAndSetConnectorStatus + public static readonly restoreConnectorStatus = restoreConnectorStatus + public static readonly isIdTagAuthorized = isIdTagAuthorized + public static readonly buildTransactionEndMeterValue = buildTransactionEndMeterValue protected static getSampledValueTemplate = getSampledValueTemplate protected static buildSampledValue = buildSampledValue @@ -1239,7 +1254,7 @@ export class OCPPServiceUtils { // This is intentional } - public static ajvErrorsToErrorType (errors: ErrorObject[] | null | undefined): ErrorType { + public static ajvErrorsToErrorType (errors: ErrorObject[] | undefined | null): ErrorType { if (isNotEmptyArray(errors)) { for (const error of errors as DefinedError[]) { switch (error.keyword) { @@ -1331,16 +1346,16 @@ export class OCPPServiceUtils { return true } - public static convertDateToISOString(obj: T): void { - for (const key in obj) { + public static convertDateToISOString(object: T): void { + for (const key in object) { // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion, @typescript-eslint/no-non-null-assertion - if (isDate(obj![key])) { + if (isDate(object![key])) { // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion, @typescript-eslint/no-non-null-assertion - (obj![key] as string) = (obj![key] as Date).toISOString() + (object![key] as string) = (object![key] as Date).toISOString() // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion, @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-unnecessary-condition - } else if (typeof obj![key] === 'object' && obj![key] !== null) { + } else if (typeof object![key] === 'object' && object![key] !== null) { // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion, @typescript-eslint/no-non-null-assertion - OCPPServiceUtils.convertDateToISOString(obj![key] as T) + OCPPServiceUtils.convertDateToISOString(object![key] as T) } } }