X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FHelpers.ts;h=735a0b39c9bcf967f69297aa1a66618d2b4f9752;hb=16c46962f43a692c996b1c89a94dc572fea52d63;hp=8870ce86ab3882a89e0c34364026495eaf115d84;hpb=c2be11fddae3fd9c84ab92719e3d479301eb0257;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/Helpers.ts b/src/charging-station/Helpers.ts index 8870ce86..735a0b39 100644 --- a/src/charging-station/Helpers.ts +++ b/src/charging-station/Helpers.ts @@ -1,18 +1,18 @@ import { createHash, randomBytes } from 'node:crypto' import type { EventEmitter } from 'node:events' -import { basename, dirname, join } from 'node:path' +import { basename, dirname, isAbsolute, join, parse, relative, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' import chalk from 'chalk' import { - type Interval, addDays, addSeconds, addWeeks, differenceInDays, differenceInSeconds, differenceInWeeks, + type Interval, isAfter, isBefore, isDate, @@ -21,9 +21,8 @@ import { toDate } from 'date-fns' import { maxTime } from 'date-fns/constants' +import { isEmpty } from 'rambda' -import type { ChargingStation } from './ChargingStation.js' -import { getConfigurationKey } from './ConfigurationKeyUtils.js' import { BaseError } from '../exception/index.js' import { AmpereUnits, @@ -56,23 +55,34 @@ import { } from '../types/index.js' import { ACElectricUtils, - Constants, - DCElectricUtils, clone, + Constants, convertToDate, convertToInt, + DCElectricUtils, isArraySorted, - isEmptyObject, - isEmptyString, isNotEmptyArray, isNotEmptyString, isValidDate, logger, secureRandom } from '../utils/index.js' +import type { ChargingStation } from './ChargingStation.js' +import { getConfigurationKey } from './ConfigurationKeyUtils.js' const moduleName = 'Helpers' +export const buildTemplateName = (templateFile: string): string => { + if (isAbsolute(templateFile)) { + templateFile = relative( + resolve(join(dirname(fileURLToPath(import.meta.url)), 'assets', 'station-templates')), + templateFile + ) + } + const templateFileParsedPath = parse(templateFile) + return join(templateFileParsedPath.dir, templateFileParsedPath.name) +} + export const getChargingStationId = ( index: number, stationTemplate: ChargingStationTemplate | undefined @@ -146,16 +156,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 }) } @@ -243,12 +253,12 @@ export const checkTemplate = ( logger.error(`${logPrefix} ${errorMsg}`) throw new BaseError(errorMsg) } - if (isEmptyObject(stationTemplate)) { + if (isEmpty(stationTemplate)) { const errorMsg = `Empty charging station information from template file ${templateFile}` logger.error(`${logPrefix} ${errorMsg}`) throw new BaseError(errorMsg) } - if (stationTemplate.idTagsFile == null || isEmptyString(stationTemplate.idTagsFile)) { + if (stationTemplate.idTagsFile == null || isEmpty(stationTemplate.idTagsFile)) { logger.warn( `${logPrefix} Missing id tags file in template file ${templateFile}. That can lead to issues with the Automatic Transaction Generator` ) @@ -265,7 +275,7 @@ export const checkConfiguration = ( logger.error(`${logPrefix} ${errorMsg}`) throw new BaseError(errorMsg) } - if (isEmptyObject(stationConfiguration)) { + if (isEmpty(stationConfiguration)) { const errorMsg = `Empty charging station configuration from file ${configurationFile}` logger.error(`${logPrefix} ${errorMsg}`) throw new BaseError(errorMsg) @@ -338,12 +348,18 @@ 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 } @@ -418,21 +434,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 @@ -443,16 +459,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 }) } }) } @@ -477,7 +493,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) } @@ -493,6 +509,7 @@ export const stationTemplateToStationInfo = ( delete stationTemplate.Evses delete stationTemplate.Configuration delete stationTemplate.AutomaticTransactionGenerator + delete stationTemplate.numberOfConnectors delete stationTemplate.chargeBoxSerialNumberPrefix delete stationTemplate.chargePointSerialNumberPrefix delete stationTemplate.meterSerialNumberPrefix @@ -770,7 +787,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}` : '' }` @@ -784,8 +801,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] } @@ -957,7 +974,7 @@ export const prepareChargingProfileKind = ( if (connectorStatus?.transactionStarted === true) { chargingProfile.chargingSchedule.startSchedule = connectorStatus.transactionStart } - // FIXME: Handle relative charging profile duration + // FIXME: handle relative charging profile duration break } return true