From: Jérôme Benoit Date: Wed, 6 Mar 2024 12:35:44 +0000 (+0100) Subject: fix: fix template name consistency X-Git-Tag: v1.3.0~44 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=a33026fe3cd6ce141efa55d27d35b4eb5c1a54b9;p=e-mobility-charging-stations-simulator.git fix: fix template name consistency Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 05fffa5b..bd7fda2d 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -1,7 +1,7 @@ // Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved. import { EventEmitter } from 'node:events' -import { dirname, extname, join, parse } from 'node:path' +import { dirname, extname, join } from 'node:path' import process, { exit } from 'node:process' import { fileURLToPath } from 'node:url' import { isMainThread } from 'node:worker_threads' @@ -36,6 +36,7 @@ import { import { Configuration, Constants, + buildTemplateName, buildTemplateStatisticsPayload, formatDurationMilliSeconds, generateUUID, @@ -208,9 +209,7 @@ export class Bootstrap extends EventEmitter { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion for (const stationTemplateUrl of Configuration.getStationTemplateUrls()!) { try { - const nbStations = - this.templateStatistics.get(parse(stationTemplateUrl.file).name)?.configured ?? - stationTemplateUrl.numberOfStations + const nbStations = stationTemplateUrl.numberOfStations for (let index = 1; index <= nbStations; index++) { await this.addChargingStation(index, stationTemplateUrl.file) } @@ -495,10 +494,7 @@ export class Bootstrap extends EventEmitter { const stationTemplateUrls = Configuration.getStationTemplateUrls()! if (isNotEmptyArray(stationTemplateUrls)) { for (const stationTemplateUrl of stationTemplateUrls) { - const templateName = join( - parse(stationTemplateUrl.file).dir, - parse(stationTemplateUrl.file).name - ) + const templateName = buildTemplateName(stationTemplateUrl.file) this.templateStatistics.set(templateName, { configured: stationTemplateUrl.numberOfStations, added: 0, @@ -539,7 +535,7 @@ export class Bootstrap extends EventEmitter { public async addChargingStation ( index: number, - stationTemplateFile: string, + templateFile: string, options?: ChargingStationOptions ): Promise { await this.workerImplementation?.addElement({ @@ -548,12 +544,12 @@ export class Bootstrap extends EventEmitter { dirname(fileURLToPath(import.meta.url)), 'assets', 'station-templates', - stationTemplateFile + templateFile ), options }) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const templateStatistics = this.templateStatistics.get(parse(stationTemplateFile).name)! + const templateStatistics = this.templateStatistics.get(buildTemplateName(templateFile))! ++templateStatistics.added templateStatistics.indexes.add(index) } diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 82fba5ad..f79a59ce 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -3,7 +3,7 @@ import { createHash } from 'node:crypto' import { EventEmitter } from 'node:events' import { type FSWatcher, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs' -import { dirname, join, parse } from 'node:path' +import { dirname, join } from 'node:path' import { URL } from 'node:url' import { parentPort } from 'node:worker_threads' @@ -131,6 +131,7 @@ import { buildEvsesStatus, buildStartedMessage, buildStoppedMessage, + buildTemplateName, buildUpdatedMessage, clone, convertToBoolean, @@ -1163,7 +1164,7 @@ export class ChargingStation extends EventEmitter { const stationInfo = stationTemplateToStationInfo(stationTemplate) stationInfo.hashId = getHashId(this.index, stationTemplate) stationInfo.templateIndex = this.index - stationInfo.templateName = parse(this.templateFile).name + stationInfo.templateName = buildTemplateName(this.templateFile) stationInfo.chargingStationId = getChargingStationId(this.index, stationTemplate) createSerialNumber(stationTemplate, stationInfo) stationInfo.voltageOut = this.getVoltageOut(stationInfo) @@ -1223,7 +1224,7 @@ export class ChargingStation extends EventEmitter { } // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (stationInfo.templateName == null) { - stationInfo.templateName = parse(this.templateFile).name + stationInfo.templateName = buildTemplateName(this.templateFile) } } } diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index abfac5e4..c39c95e6 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -1,5 +1,7 @@ import { getRandomValues, randomBytes, randomInt, randomUUID } from 'node:crypto' +import { dirname, isAbsolute, join, parse, relative, resolve } from 'node:path' import { env, nextTick } from 'node:process' +import { fileURLToPath } from 'node:url' import { formatDuration, @@ -207,6 +209,17 @@ export const getRandomFloatFluctuatedRounded = ( ) } +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 extractTimeSeriesValues = (timeSeries: TimestampedData[]): number[] => { return timeSeries.map(timeSeriesItem => timeSeriesItem.value) } diff --git a/src/utils/index.ts b/src/utils/index.ts index 1bc4fe7e..fdcb6fa2 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -29,6 +29,7 @@ export { } from './MessageChannelUtils.js' export { JSONStringifyWithMapSupport, + buildTemplateName, clone, convertToBoolean, convertToDate,