From: Jérôme Benoit Date: Sun, 26 Nov 2023 22:49:48 +0000 (+0100) Subject: fix: fix recursion loop getTemplateFromFile -> logPrefix -> getTemplateFromFile X-Git-Tag: v1.2.27~17 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=c1f16afd333f0fc8a6a02b9baa0aff23dbd18580;p=e-mobility-charging-stations-simulator.git fix: fix recursion loop getTemplateFromFile -> logPrefix -> getTemplateFromFile Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index f0fa0820..ba430878 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -236,14 +236,18 @@ export class ChargingStation extends EventEmitter { } public logPrefix = (): string => { - return logPrefix( - ` ${ - (isNotEmptyString(this?.stationInfo?.chargingStationId) - ? this?.stationInfo?.chargingStationId - : getChargingStationId(this.index, this.getTemplateFromFile()!)) ?? - 'Error at building log prefix' - } |`, - ); + if (isNotEmptyString(this?.stationInfo?.chargingStationId)) { + return logPrefix(` ${this?.stationInfo?.chargingStationId} |`); + } + let stationTemplate: ChargingStationTemplate | undefined; + try { + stationTemplate = JSON.parse( + readFileSync(this.templateFile, 'utf8'), + ) as ChargingStationTemplate; + } catch { + stationTemplate = undefined; + } + return logPrefix(` ${getChargingStationId(this.index, stationTemplate)} |`); }; public hasIdTags(): boolean { diff --git a/src/charging-station/Helpers.ts b/src/charging-station/Helpers.ts index f0f848d3..ce0ba3e3 100644 --- a/src/charging-station/Helpers.ts +++ b/src/charging-station/Helpers.ts @@ -74,15 +74,18 @@ const moduleName = 'Helpers'; export const getChargingStationId = ( index: number, - stationTemplate: ChargingStationTemplate, + stationTemplate: ChargingStationTemplate | undefined, ): string => { + if (isUndefined(stationTemplate)) { + return "Unknown 'chargingStationId'"; + } // In case of multiple instances: add instance index to charging station id const instanceIndex = env.CF_INSTANCE_INDEX ?? 0; const idSuffix = stationTemplate?.nameSuffix ?? ''; const idStr = `000000000${index.toString()}`; return stationTemplate?.fixedName ? stationTemplate.baseName - : `${stationTemplate.baseName}-${instanceIndex.toString()}${idStr.substring( + : `${stationTemplate?.baseName}-${instanceIndex.toString()}${idStr.substring( idStr.length - 4, )}${idSuffix}`; };