X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStationUtils.ts;h=78b73647456bdf8568ebf61aaac281e698aad322;hb=704426f31f79f227c2e4163fb8937f89859effb5;hp=d0d99c0b38a89c9ddc100d4407790cbd44ca599f;hpb=b2b606263e2676354259164d532ff9aa91ccdf87;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index d0d99c0b..78b73647 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -51,7 +51,7 @@ const moduleName = 'ChargingStationUtils'; export const getChargingStationId = ( index: number, - stationTemplate: ChargingStationTemplate + stationTemplate: ChargingStationTemplate, ): string => { // In case of multiple instances: add instance index to charging station id const instanceIndex = process.env.CF_INSTANCE_INDEX ?? 0; @@ -60,7 +60,7 @@ export const getChargingStationId = ( return stationTemplate?.fixedName ? stationTemplate.baseName : `${stationTemplate.baseName}-${instanceIndex.toString()}${idStr.substring( - idStr.length - 4 + idStr.length - 4, )}${idSuffix}`; }; @@ -101,7 +101,7 @@ export const getHashId = (index: number, stationTemplate: ChargingStationTemplat export const checkChargingStation = ( chargingStation: ChargingStation, - logPrefix: string + logPrefix: string, ): boolean => { if (chargingStation.started === false && chargingStation.starting === false) { logger.warn(`${logPrefix} charging station is stopped, cannot proceed`); @@ -112,7 +112,7 @@ export const checkChargingStation = ( export const getPhaseRotationValue = ( connectorId: number, - numberOfPhases: number + numberOfPhases: number, ): string | undefined => { // AC/DC if (connectorId === 0 && numberOfPhases === 0) { @@ -144,7 +144,7 @@ const getMaxNumberOfConnectors = (connectors: Record): export const getBootConnectorStatus = ( chargingStation: ChargingStation, connectorId: number, - connectorStatus: ConnectorStatus + connectorStatus: ConnectorStatus, ): ConnectorStatusEnum => { let connectorBootStatus: ConnectorStatusEnum; if ( @@ -169,7 +169,7 @@ export const getBootConnectorStatus = ( export const checkTemplate = ( stationTemplate: ChargingStationTemplate, logPrefix: string, - templateFile: string + templateFile: string, ): void => { if (isNullOrUndefined(stationTemplate)) { const errorMsg = `Failed to read charging station template file ${templateFile}`; @@ -181,16 +181,16 @@ export const checkTemplate = ( logger.error(`${logPrefix} ${errorMsg}`); throw new BaseError(errorMsg); } - if (isEmptyObject(stationTemplate.AutomaticTransactionGenerator)) { + 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 + Constants.DEFAULT_ATG_CONFIGURATION, ); } if (isNullOrUndefined(stationTemplate.idTagsFile) || isEmptyString(stationTemplate.idTagsFile)) { logger.warn( - `${logPrefix} Missing id tags file in template file ${templateFile}. That can lead to issues with the Automatic Transaction Generator` + `${logPrefix} Missing id tags file in template file ${templateFile}. That can lead to issues with the Automatic Transaction Generator`, ); } }; @@ -198,7 +198,7 @@ export const checkTemplate = ( export const checkConnectorsConfiguration = ( stationTemplate: ChargingStationTemplate, logPrefix: string, - templateFile: string + templateFile: string, ): { configuredMaxConnectors: number; templateMaxConnectors: number; @@ -206,9 +206,9 @@ export const checkConnectorsConfiguration = ( } => { const configuredMaxConnectors = getConfiguredNumberOfConnectors(stationTemplate); checkConfiguredMaxConnectors(configuredMaxConnectors, logPrefix, templateFile); - const templateMaxConnectors = getMaxNumberOfConnectors(stationTemplate.Connectors); + const templateMaxConnectors = getMaxNumberOfConnectors(stationTemplate.Connectors!); checkTemplateMaxConnectors(templateMaxConnectors, logPrefix, templateFile); - const templateMaxAvailableConnectors = stationTemplate?.Connectors[0] + const templateMaxAvailableConnectors = stationTemplate.Connectors![0] ? templateMaxConnectors - 1 : templateMaxConnectors; if ( @@ -216,7 +216,7 @@ export const checkConnectorsConfiguration = ( !stationTemplate?.randomConnectors ) { logger.warn( - `${logPrefix} Number of connectors exceeds the number of connector configurations in template ${templateFile}, forcing random connector configurations affectation` + `${logPrefix} Number of connectors exceeds the number of connector configurations in template ${templateFile}, forcing random connector configurations affectation`, ); stationTemplate.randomConnectors = true; } @@ -227,11 +227,11 @@ export const checkStationInfoConnectorStatus = ( connectorId: number, connectorStatus: ConnectorStatus, logPrefix: string, - templateFile: string + templateFile: string, ): void => { if (!isNullOrUndefined(connectorStatus?.status)) { logger.warn( - `${logPrefix} Charging station information from template ${templateFile} with connector id ${connectorId} status configuration defined, undefine it` + `${logPrefix} Charging station information from template ${templateFile} with connector id ${connectorId} status configuration defined, undefine it`, ); delete connectorStatus.status; } @@ -240,7 +240,7 @@ export const checkStationInfoConnectorStatus = ( export const buildConnectorsMap = ( connectors: Record, logPrefix: string, - templateFile: string + templateFile: string, ): Map => { const connectorsMap = new Map(); if (getMaxNumberOfConnectors(connectors) > 0) { @@ -252,7 +252,7 @@ export const buildConnectorsMap = ( } } else { logger.warn( - `${logPrefix} Charging station information from template ${templateFile} with no connectors, cannot build connectors map` + `${logPrefix} Charging station information from template ${templateFile} with no connectors, cannot build connectors map`, ); } return connectorsMap; @@ -260,26 +260,26 @@ export const buildConnectorsMap = ( export const initializeConnectorsMapStatus = ( connectors: Map, - logPrefix: string + logPrefix: string, ): void => { 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) { - connectors.get(connectorId).availability = AvailabilityType.Operative; + connectors.get(connectorId)!.availability = AvailabilityType.Operative; if (isUndefined(connectors.get(connectorId)?.chargingProfiles)) { - connectors.get(connectorId).chargingProfiles = []; + connectors.get(connectorId)!.chargingProfiles = []; } } else if ( connectorId > 0 && isNullOrUndefined(connectors.get(connectorId)?.transactionStarted) ) { - initializeConnectorStatus(connectors.get(connectorId)); + initializeConnectorStatus(connectors.get(connectorId)!); } } }; @@ -299,7 +299,7 @@ export const resetConnectorStatus = (connectorStatus: ConnectorStatus): void => export const createBootNotificationRequest = ( stationInfo: ChargingStationInfo, - bootReason: BootReasonEnumType = BootReasonEnumType.PowerUp + bootReason: BootReasonEnumType = BootReasonEnumType.PowerUp, ): BootNotificationRequest => { const ocppVersion = stationInfo.ocppVersion ?? OCPPVersion.VERSION_16; switch (ocppVersion) { @@ -352,11 +352,12 @@ export const createBootNotificationRequest = ( export const warnTemplateKeysDeprecation = ( stationTemplate: ChargingStationTemplate, logPrefix: string, - templateFile: string + templateFile: string, ) => { - const templateKeys: { key: string; deprecatedKey: string }[] = [ - { key: 'supervisionUrls', deprecatedKey: 'supervisionUrl' }, - { key: 'idTagsFile', deprecatedKey: 'authorizationFile' }, + const templateKeys: { deprecatedKey: string; key?: string }[] = [ + { deprecatedKey: 'supervisionUrl', key: 'supervisionUrls' }, + { deprecatedKey: 'authorizationFile', key: 'idTagsFile' }, + { deprecatedKey: 'payloadSchemaValidation', key: 'ocppStrictCompliance' }, ]; for (const templateKey of templateKeys) { warnDeprecatedTemplateKey( @@ -364,20 +365,20 @@ export const warnTemplateKeysDeprecation = ( templateKey.deprecatedKey, logPrefix, templateFile, - `Use '${templateKey.key}' instead` + !isUndefined(templateKey.key) ? `Use '${templateKey.key}' instead` : undefined, ); convertDeprecatedTemplateKey(stationTemplate, templateKey.deprecatedKey, templateKey.key); } }; export const stationTemplateToStationInfo = ( - stationTemplate: ChargingStationTemplate + stationTemplate: ChargingStationTemplate, ): ChargingStationInfo => { stationTemplate = cloneObject(stationTemplate); delete stationTemplate.power; delete stationTemplate.powerUnit; - delete stationTemplate?.Connectors; - delete stationTemplate?.Evses; + delete stationTemplate.Connectors; + delete stationTemplate.Evses; delete stationTemplate.Configuration; delete stationTemplate.AutomaticTransactionGenerator; delete stationTemplate.chargeBoxSerialNumberPrefix; @@ -395,7 +396,7 @@ export const createSerialNumber = ( } = { randomSerialNumberUpperCase: true, randomSerialNumber: true, - } + }, ): void => { params = { ...{ randomSerialNumberUpperCase: true, randomSerialNumber: true }, ...params }; const serialNumberSuffix = params?.randomSerialNumber @@ -414,11 +415,11 @@ export const createSerialNumber = ( export const propagateSerialNumber = ( stationTemplate: ChargingStationTemplate, stationInfoSrc: ChargingStationInfo, - stationInfoDst: ChargingStationInfo + stationInfoDst: ChargingStationInfo, ) => { if (!stationInfoSrc || !stationTemplate) { throw new BaseError( - 'Missing charging station template or existing configuration to propagate serial number' + 'Missing charging station template or existing configuration to propagate serial number', ); } stationTemplate?.chargePointSerialNumberPrefix && stationInfoSrc?.chargePointSerialNumber @@ -450,20 +451,20 @@ export const getAmperageLimitationUnitDivider = (stationInfo: ChargingStationInf export const getChargingStationConnectorChargingProfilesPowerLimit = ( chargingStation: ChargingStation, - connectorId: number + connectorId: number, ): number | undefined => { - let limit: number, matchingChargingProfile: ChargingProfile; + let limit: number | undefined, matchingChargingProfile: ChargingProfile | undefined; // Get charging profiles for connector and sort by stack level const chargingProfiles = cloneObject( - chargingStation.getConnectorStatus(connectorId)?.chargingProfiles + chargingStation.getConnectorStatus(connectorId)!.chargingProfiles!, )?.sort((a, b) => b.stackLevel - a.stackLevel) ?? []; // Get profiles on connector 0 if (chargingStation.getConnectorStatus(0)?.chargingProfiles) { chargingProfiles.push( ...cloneObject( - chargingStation.getConnectorStatus(0).chargingProfiles - ).sort((a, b) => b.stackLevel - a.stackLevel) + chargingStation.getConnectorStatus(0)!.chargingProfiles!, + ).sort((a, b) => b.stackLevel - a.stackLevel), ); } if (isNotEmptyArray(chargingProfiles)) { @@ -474,28 +475,28 @@ export const getChargingStationConnectorChargingProfilesPowerLimit = ( switch (chargingStation.getCurrentOutType()) { case CurrentType.AC: limit = - matchingChargingProfile.chargingSchedule.chargingRateUnit === ChargingRateUnitType.WATT + matchingChargingProfile?.chargingSchedule?.chargingRateUnit === + ChargingRateUnitType.WATT ? limit : ACElectricUtils.powerTotal( chargingStation.getNumberOfPhases(), chargingStation.getVoltageOut(), - limit + limit!, ); break; case CurrentType.DC: limit = - matchingChargingProfile.chargingSchedule.chargingRateUnit === ChargingRateUnitType.WATT + matchingChargingProfile?.chargingSchedule?.chargingRateUnit === + ChargingRateUnitType.WATT ? limit - : DCElectricUtils.power(chargingStation.getVoltageOut(), limit); + : DCElectricUtils.power(chargingStation.getVoltageOut(), limit!); } const connectorMaximumPower = chargingStation.getMaximumPower() / chargingStation.powerDivider; - if (limit > connectorMaximumPower) { + if (limit! > connectorMaximumPower) { logger.error( - `${chargingStation.logPrefix()} Charging profile id ${ - matchingChargingProfile.chargingProfileId - } limit ${limit} is greater than connector id ${connectorId} maximum ${connectorMaximumPower}: %j`, - result + `${chargingStation.logPrefix()} Charging profile id ${matchingChargingProfile?.chargingProfileId} limit ${limit} is greater than connector id ${connectorId} maximum ${connectorMaximumPower}: %j`, + result, ); limit = connectorMaximumPower; } @@ -507,7 +508,7 @@ export const getChargingStationConnectorChargingProfilesPowerLimit = ( export const getDefaultVoltageOut = ( currentType: CurrentType, logPrefix: string, - templateFile: string + templateFile: string, ): Voltage => { const errorMsg = `Unknown ${currentType} currentOutType in template file ${templateFile}, cannot define default voltage out`; let defaultVoltageOut: number; @@ -535,9 +536,9 @@ export const getIdTagsFile = (stationInfo: ChargingStationInfo): string | undefi export const waitChargingStationEvents = async ( emitter: EventEmitter, event: ChargingStationWorkerMessageEvents, - eventsToWait: number + eventsToWait: number, ): Promise => { - return new Promise((resolve) => { + return new Promise((resolve) => { let events = 0; if (eventsToWait === 0) { resolve(events); @@ -552,7 +553,7 @@ export const waitChargingStationEvents = async ( }; const getConfiguredNumberOfConnectors = (stationTemplate: ChargingStationTemplate): number => { - let configuredMaxConnectors: number; + let configuredMaxConnectors = 0; if (isNotEmptyArray(stationTemplate.numberOfConnectors) === true) { const numberOfConnectors = stationTemplate.numberOfConnectors as number[]; configuredMaxConnectors = @@ -560,11 +561,10 @@ const getConfiguredNumberOfConnectors = (stationTemplate: ChargingStationTemplat } else if (isUndefined(stationTemplate.numberOfConnectors) === false) { configuredMaxConnectors = stationTemplate.numberOfConnectors as number; } else if (stationTemplate.Connectors && !stationTemplate.Evses) { - configuredMaxConnectors = stationTemplate?.Connectors[0] + configuredMaxConnectors = stationTemplate.Connectors[0] ? getMaxNumberOfConnectors(stationTemplate.Connectors) - 1 : getMaxNumberOfConnectors(stationTemplate.Connectors); } else if (stationTemplate.Evses && !stationTemplate.Connectors) { - configuredMaxConnectors = 0; for (const evse in stationTemplate.Evses) { if (evse === '0') { continue; @@ -578,11 +578,11 @@ const getConfiguredNumberOfConnectors = (stationTemplate: ChargingStationTemplat const checkConfiguredMaxConnectors = ( configuredMaxConnectors: number, logPrefix: string, - templateFile: string + templateFile: string, ): void => { if (configuredMaxConnectors <= 0) { logger.warn( - `${logPrefix} Charging station information from template ${templateFile} with ${configuredMaxConnectors} connectors` + `${logPrefix} Charging station information from template ${templateFile} with ${configuredMaxConnectors} connectors`, ); } }; @@ -590,15 +590,15 @@ const checkConfiguredMaxConnectors = ( const checkTemplateMaxConnectors = ( templateMaxConnectors: number, logPrefix: string, - templateFile: string + templateFile: string, ): void => { if (templateMaxConnectors === 0) { logger.warn( - `${logPrefix} Charging station information from template ${templateFile} with empty connectors configuration` + `${logPrefix} Charging station information from template ${templateFile} with empty connectors configuration`, ); } else if (templateMaxConnectors < 0) { logger.error( - `${logPrefix} Charging station information from template ${templateFile} with no connectors configuration defined` + `${logPrefix} Charging station information from template ${templateFile} with no connectors configuration defined`, ); } }; @@ -621,9 +621,9 @@ const warnDeprecatedTemplateKey = ( key: string, logPrefix: string, templateFile: string, - logMsgToAppend = '' + logMsgToAppend = '', ): void => { - if (!isUndefined(template[key])) { + if (!isUndefined(template[key as keyof ChargingStationTemplate])) { const logMsg = `Deprecated template key '${key}' usage in file '${templateFile}'${ isNotEmptyString(logMsgToAppend) ? `. ${logMsgToAppend}` : '' }`; @@ -635,11 +635,14 @@ const warnDeprecatedTemplateKey = ( const convertDeprecatedTemplateKey = ( template: ChargingStationTemplate, deprecatedKey: string, - key: string + key?: string, ): void => { - if (!isUndefined(template[deprecatedKey])) { - template[key] = template[deprecatedKey] as unknown; - delete template[deprecatedKey]; + if (!isUndefined(template[deprecatedKey as keyof ChargingStationTemplate])) { + if (!isUndefined(key)) { + (template as unknown as Record)[key!] = + template[deprecatedKey as keyof ChargingStationTemplate]; + } + delete template[deprecatedKey as keyof ChargingStationTemplate]; } }; @@ -652,7 +655,7 @@ const convertDeprecatedTemplateKey = ( */ const getLimitFromChargingProfiles = ( chargingProfiles: ChargingProfile[], - logPrefix: string + logPrefix: string, ): { limit: number; matchingChargingProfile: ChargingProfile; @@ -665,7 +668,7 @@ const getLimitFromChargingProfiles = ( const chargingSchedule = chargingProfile.chargingSchedule; if (!chargingSchedule?.startSchedule) { logger.warn( - `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: startSchedule is not defined in charging profile id ${chargingProfile.chargingProfileId}` + `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: startSchedule is not defined in charging profile id ${chargingProfile.chargingProfileId}`, ); } // Check type (recurring) and if it is already active @@ -677,14 +680,14 @@ const getLimitFromChargingProfiles = ( ) { if (!(chargingSchedule?.startSchedule instanceof Date)) { logger.warn( - `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: startSchedule is not a Date object in charging profile id ${chargingProfile.chargingProfileId}. Trying to convert it to a Date object` + `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: startSchedule is not a Date object in charging profile id ${chargingProfile.chargingProfileId}. Trying to convert it to a Date object`, ); - chargingSchedule.startSchedule = new Date(chargingSchedule.startSchedule); + chargingSchedule.startSchedule = new Date(chargingSchedule.startSchedule!); } chargingSchedule.startSchedule.setFullYear( currentDate.getFullYear(), currentDate.getMonth(), - currentDate.getDate() + currentDate.getDate(), ); // Check if the start of the schedule is yesterday if (moment(chargingSchedule.startSchedule).isAfter(currentMoment)) { @@ -699,7 +702,7 @@ const getLimitFromChargingProfiles = ( .add(chargingSchedule.duration, 's') .isAfter(currentMoment) ) { - let lastButOneSchedule: ChargingSchedulePeriod; + let lastButOneSchedule: ChargingSchedulePeriod | undefined; // Search the right schedule period for (const schedulePeriod of chargingSchedule.chargingSchedulePeriod) { // Handling of only one period @@ -722,7 +725,7 @@ const getLimitFromChargingProfiles = ( ) { // Found the schedule: last but one is the correct one const result = { - limit: lastButOneSchedule.limit, + limit: lastButOneSchedule!.limit, matchingChargingProfile: chargingProfile, }; logger.debug(debugLogMsg, result);