X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FHelpers.ts;h=22ed5041e0515cc6b0769b3acb85df2d54525287;hb=d990f4bc8ad5366a200fd3d89be76c084fd71cea;hp=d8ef4a328e1c5a4fdef6d09acf1da05294f9cfc8;hpb=0c9f0f67eb80d625536f4bc8a1cea58e00f35c2f;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/Helpers.ts b/src/charging-station/Helpers.ts index d8ef4a32..22ed5041 100644 --- a/src/charging-station/Helpers.ts +++ b/src/charging-station/Helpers.ts @@ -36,6 +36,7 @@ import { type ChargingSchedulePeriod, type ChargingStationConfiguration, type ChargingStationInfo, + type ChargingStationOptions, type ChargingStationTemplate, type ChargingStationWorkerMessageEvents, ConnectorPhaseRotation, @@ -57,7 +58,7 @@ import { ACElectricUtils, Constants, DCElectricUtils, - cloneObject, + clone, convertToDate, convertToInt, isArraySorted, @@ -65,7 +66,7 @@ import { isEmptyString, isNotEmptyArray, isNotEmptyString, - isValidTime, + isValidDate, logger, secureRandom } from '../utils/index.js' @@ -145,16 +146,16 @@ export const getHashId = (index: number, stationTemplate: ChargingStationTemplat const chargingStationInfo = { chargePointModel: stationTemplate.chargePointModel, chargePointVendor: stationTemplate.chargePointVendor, - ...(stationTemplate.chargeBoxSerialNumberPrefix !== undefined && { + ...(stationTemplate.chargeBoxSerialNumberPrefix != null && { chargeBoxSerialNumber: stationTemplate.chargeBoxSerialNumberPrefix }), - ...(stationTemplate.chargePointSerialNumberPrefix !== undefined && { + ...(stationTemplate.chargePointSerialNumberPrefix != null && { chargePointSerialNumber: stationTemplate.chargePointSerialNumberPrefix }), - ...(stationTemplate.meterSerialNumberPrefix !== undefined && { + ...(stationTemplate.meterSerialNumberPrefix != null && { meterSerialNumber: stationTemplate.meterSerialNumberPrefix }), - ...(stationTemplate.meterType !== undefined && { + ...(stationTemplate.meterType != null && { meterType: stationTemplate.meterType }) } @@ -247,14 +248,6 @@ export const checkTemplate = ( logger.error(`${logPrefix} ${errorMsg}`) throw new BaseError(errorMsg) } - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - if (isEmptyObject(stationTemplate.AutomaticTransactionGenerator!)) { - stationTemplate.AutomaticTransactionGenerator = Constants.DEFAULT_ATG_CONFIGURATION - logger.warn( - `${logPrefix} Empty automatic transaction generator configuration from template file ${templateFile}, set to default: %j`, - Constants.DEFAULT_ATG_CONFIGURATION - ) - } if (stationTemplate.idTagsFile == null || isEmptyString(stationTemplate.idTagsFile)) { logger.warn( `${logPrefix} Missing id tags file in template file ${templateFile}. That can lead to issues with the Automatic Transaction Generator` @@ -331,7 +324,7 @@ export const buildConnectorsMap = ( const connectorStatus = connectors[connector] const connectorId = convertToInt(connector) checkStationInfoConnectorStatus(connectorId, connectorStatus, logPrefix, templateFile) - connectorsMap.set(connectorId, cloneObject(connectorStatus)) + connectorsMap.set(connectorId, clone(connectorStatus)) } } else { logger.warn( @@ -341,6 +334,37 @@ export const buildConnectorsMap = ( return connectorsMap } +export const setChargingStationOptions = ( + stationInfo: ChargingStationInfo, + options?: ChargingStationOptions +): ChargingStationInfo => { + if (options?.supervisionUrls != null) { + stationInfo.supervisionUrls = options.supervisionUrls + } + if (options?.persistentConfiguration != null) { + stationInfo.stationInfoPersistentConfiguration = options.persistentConfiguration + stationInfo.ocppPersistentConfiguration = options.persistentConfiguration + stationInfo.automaticTransactionGeneratorPersistentConfiguration = + options.persistentConfiguration + } + if (options?.autoStart != null) { + stationInfo.autoStart = options.autoStart + } + if (options?.autoRegister != null) { + stationInfo.autoRegister = options.autoRegister + } + if (options?.enableStatistics != null) { + stationInfo.enableStatistics = options.enableStatistics + } + if (options?.ocppStrictCompliance != null) { + stationInfo.ocppStrictCompliance = options.ocppStrictCompliance + } + if (options?.stopTransactionsOnStopped != null) { + stationInfo.stopTransactionsOnStopped = options.stopTransactionsOnStopped + } + return stationInfo +} + export const initializeConnectorsMapStatus = ( connectors: Map, logPrefix: string @@ -348,9 +372,9 @@ export const initializeConnectorsMapStatus = ( for (const connectorId of connectors.keys()) { if (connectorId > 0 && connectors.get(connectorId)?.transactionStarted === true) { logger.warn( - `${logPrefix} Connector id ${connectorId} at initialization has a transaction started with id ${connectors.get( - connectorId - )?.transactionId}` + `${logPrefix} Connector id ${connectorId} at initialization has a transaction started with id ${ + connectors.get(connectorId)?.transactionId + }` ) } if (connectorId === 0) { @@ -367,11 +391,14 @@ export const initializeConnectorsMapStatus = ( } } -export const resetConnectorStatus = (connectorStatus: ConnectorStatus): void => { +export const resetConnectorStatus = (connectorStatus: ConnectorStatus | undefined): void => { + if (connectorStatus == null) { + return + } connectorStatus.chargingProfiles = connectorStatus.transactionId != null && isNotEmptyArray(connectorStatus.chargingProfiles) - ? connectorStatus.chargingProfiles?.filter( - (chargingProfile) => chargingProfile.transactionId !== connectorStatus.transactionId + ? connectorStatus.chargingProfiles.filter( + chargingProfile => chargingProfile.transactionId !== connectorStatus.transactionId ) : [] connectorStatus.idTagLocalAuthorized = false @@ -397,21 +424,21 @@ export const createBootNotificationRequest = ( return { chargePointModel: stationInfo.chargePointModel, chargePointVendor: stationInfo.chargePointVendor, - ...(stationInfo.chargeBoxSerialNumber !== undefined && { + ...(stationInfo.chargeBoxSerialNumber != null && { chargeBoxSerialNumber: stationInfo.chargeBoxSerialNumber }), - ...(stationInfo.chargePointSerialNumber !== undefined && { + ...(stationInfo.chargePointSerialNumber != null && { chargePointSerialNumber: stationInfo.chargePointSerialNumber }), - ...(stationInfo.firmwareVersion !== undefined && { + ...(stationInfo.firmwareVersion != null && { firmwareVersion: stationInfo.firmwareVersion }), - ...(stationInfo.iccid !== undefined && { iccid: stationInfo.iccid }), - ...(stationInfo.imsi !== undefined && { imsi: stationInfo.imsi }), - ...(stationInfo.meterSerialNumber !== undefined && { + ...(stationInfo.iccid != null && { iccid: stationInfo.iccid }), + ...(stationInfo.imsi != null && { imsi: stationInfo.imsi }), + ...(stationInfo.meterSerialNumber != null && { meterSerialNumber: stationInfo.meterSerialNumber }), - ...(stationInfo.meterType !== undefined && { + ...(stationInfo.meterType != null && { meterType: stationInfo.meterType }) } satisfies OCPP16BootNotificationRequest @@ -422,16 +449,16 @@ export const createBootNotificationRequest = ( chargingStation: { model: stationInfo.chargePointModel, vendorName: stationInfo.chargePointVendor, - ...(stationInfo.firmwareVersion !== undefined && { + ...(stationInfo.firmwareVersion != null && { firmwareVersion: stationInfo.firmwareVersion }), - ...(stationInfo.chargeBoxSerialNumber !== undefined && { + ...(stationInfo.chargeBoxSerialNumber != null && { serialNumber: stationInfo.chargeBoxSerialNumber }), - ...((stationInfo.iccid !== undefined || stationInfo.imsi !== undefined) && { + ...((stationInfo.iccid != null || stationInfo.imsi != null) && { modem: { - ...(stationInfo.iccid !== undefined && { iccid: stationInfo.iccid }), - ...(stationInfo.imsi !== undefined && { imsi: stationInfo.imsi }) + ...(stationInfo.iccid != null && { iccid: stationInfo.iccid }), + ...(stationInfo.imsi != null && { imsi: stationInfo.imsi }) } }) } @@ -456,7 +483,7 @@ export const warnTemplateKeysDeprecation = ( templateKey.deprecatedKey, logPrefix, templateFile, - templateKey.key !== undefined ? `Use '${templateKey.key}' instead` : undefined + templateKey.key != null ? `Use '${templateKey.key}' instead` : undefined ) convertDeprecatedTemplateKey(stationTemplate, templateKey.deprecatedKey, templateKey.key) } @@ -465,13 +492,14 @@ export const warnTemplateKeysDeprecation = ( export const stationTemplateToStationInfo = ( stationTemplate: ChargingStationTemplate ): ChargingStationInfo => { - stationTemplate = cloneObject(stationTemplate) + stationTemplate = clone(stationTemplate) delete stationTemplate.power delete stationTemplate.powerUnit delete stationTemplate.Connectors delete stationTemplate.Evses delete stationTemplate.Configuration delete stationTemplate.AutomaticTransactionGenerator + delete stationTemplate.numberOfConnectors delete stationTemplate.chargeBoxSerialNumberPrefix delete stationTemplate.chargePointSerialNumberPrefix delete stationTemplate.meterSerialNumberPrefix @@ -563,7 +591,7 @@ export const getConnectorChargingProfiles = ( chargingStation: ChargingStation, connectorId: number ): ChargingProfile[] => { - return cloneObject( + return clone( (chargingStation.getConnectorStatus(connectorId)?.chargingProfiles ?? []) .sort((a, b) => b.stackLevel - a.stackLevel) .concat( @@ -659,7 +687,7 @@ export const waitChargingStationEvents = async ( event: ChargingStationWorkerMessageEvents, eventsToWait: number ): Promise => { - return await new Promise((resolve) => { + return await new Promise(resolve => { let events = 0 if (eventsToWait === 0) { resolve(events) @@ -677,11 +705,11 @@ export const waitChargingStationEvents = async ( const getConfiguredMaxNumberOfConnectors = (stationTemplate: ChargingStationTemplate): number => { let configuredMaxNumberOfConnectors = 0 if (isNotEmptyArray(stationTemplate.numberOfConnectors)) { - const numberOfConnectors = stationTemplate.numberOfConnectors as number[] + const numberOfConnectors = stationTemplate.numberOfConnectors configuredMaxNumberOfConnectors = numberOfConnectors[Math.floor(secureRandom() * numberOfConnectors.length)] } else if (stationTemplate.numberOfConnectors != null) { - configuredMaxNumberOfConnectors = stationTemplate.numberOfConnectors as number + configuredMaxNumberOfConnectors = stationTemplate.numberOfConnectors } else if (stationTemplate.Connectors != null && stationTemplate.Evses == null) { configuredMaxNumberOfConnectors = // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition @@ -749,7 +777,7 @@ const warnDeprecatedTemplateKey = ( templateFile: string, logMsgToAppend = '' ): void => { - if (template[key as keyof ChargingStationTemplate] !== undefined) { + if (template[key as keyof ChargingStationTemplate] != null) { const logMsg = `Deprecated template key '${key}' usage in file '${templateFile}'${ isNotEmptyString(logMsgToAppend) ? `. ${logMsgToAppend}` : '' }` @@ -763,8 +791,8 @@ const convertDeprecatedTemplateKey = ( deprecatedKey: string, key?: string ): void => { - if (template[deprecatedKey as keyof ChargingStationTemplate] !== undefined) { - if (key !== undefined) { + if (template[deprecatedKey as keyof ChargingStationTemplate] != null) { + if (key != null) { (template as unknown as Record)[key] = template[deprecatedKey as keyof ChargingStationTemplate] } @@ -795,8 +823,7 @@ const getLimitFromChargingProfiles = ( ): ChargingProfilesLimit | undefined => { const debugLogMsg = `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Matching charging profile found for power limitation: %j` const currentDate = new Date() - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const connectorStatus = chargingStation.getConnectorStatus(connectorId)! + const connectorStatus = chargingStation.getConnectorStatus(connectorId) for (const chargingProfile of chargingProfiles) { const chargingSchedule = chargingProfile.chargingSchedule if (chargingSchedule.startSchedule == null) { @@ -804,7 +831,7 @@ const getLimitFromChargingProfiles = ( `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${chargingProfile.chargingProfileId} has no startSchedule defined. Trying to set it to the connector current transaction start date` ) // OCPP specifies that if startSchedule is not defined, it should be relative to start of the connector transaction - chargingSchedule.startSchedule = connectorStatus.transactionStart + chargingSchedule.startSchedule = connectorStatus?.transactionStart } if (!isDate(chargingSchedule.startSchedule)) { logger.warn( @@ -915,7 +942,7 @@ const getLimitFromChargingProfiles = ( } export const prepareChargingProfileKind = ( - connectorStatus: ConnectorStatus, + connectorStatus: ConnectorStatus | undefined, chargingProfile: ChargingProfile, currentDate: string | number | Date, logPrefix: string @@ -934,7 +961,7 @@ export const prepareChargingProfileKind = ( ) delete chargingProfile.chargingSchedule.startSchedule } - if (connectorStatus.transactionStarted === true) { + if (connectorStatus?.transactionStarted === true) { chargingProfile.chargingSchedule.startSchedule = connectorStatus.transactionStart } // FIXME: Handle relative charging profile duration @@ -949,16 +976,14 @@ export const canProceedChargingProfile = ( logPrefix: string ): boolean => { if ( - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - (isValidTime(chargingProfile.validFrom) && isBefore(currentDate, chargingProfile.validFrom!)) || - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - (isValidTime(chargingProfile.validTo) && isAfter(currentDate, chargingProfile.validTo!)) + (isValidDate(chargingProfile.validFrom) && isBefore(currentDate, chargingProfile.validFrom)) || + (isValidDate(chargingProfile.validTo) && isAfter(currentDate, chargingProfile.validTo)) ) { logger.debug( `${logPrefix} ${moduleName}.canProceedChargingProfile: Charging profile id ${ chargingProfile.chargingProfileId } is not valid for the current date ${ - currentDate instanceof Date ? currentDate.toISOString() : currentDate + isDate(currentDate) ? currentDate.toISOString() : currentDate }` ) return false @@ -972,7 +997,7 @@ export const canProceedChargingProfile = ( ) return false } - if (!isValidTime(chargingProfile.chargingSchedule.startSchedule)) { + if (!isValidDate(chargingProfile.chargingSchedule.startSchedule)) { logger.error( `${logPrefix} ${moduleName}.canProceedChargingProfile: Charging profile id ${chargingProfile.chargingProfileId} has an invalid startSchedule date defined` ) @@ -1026,7 +1051,7 @@ const prepareRecurringChargingProfile = ( ): boolean => { const chargingSchedule = chargingProfile.chargingSchedule let recurringIntervalTranslated = false - let recurringInterval: Interval + let recurringInterval: Interval | undefined switch (chargingProfile.recurrencyKind) { case RecurrencyKindType.DAILY: recurringInterval = { @@ -1085,13 +1110,11 @@ const prepareRecurringChargingProfile = ( `${logPrefix} ${moduleName}.prepareRecurringChargingProfile: Recurring ${ chargingProfile.recurrencyKind } charging profile id ${chargingProfile.chargingProfileId} recurrency time interval [${toDate( - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - recurringInterval!.start + recurringInterval?.start as Date ).toISOString()}, ${toDate( - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - recurringInterval!.end + recurringInterval?.end as Date ).toISOString()}] has not been properly translated to current date ${ - currentDate instanceof Date ? currentDate.toISOString() : currentDate + isDate(currentDate) ? currentDate.toISOString() : currentDate } ` ) }