From c1f16afd333f0fc8a6a02b9baa0aff23dbd18580 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 26 Nov 2023 23:49:48 +0100 Subject: [PATCH] fix: fix recursion loop getTemplateFromFile -> logPrefix -> getTemplateFromFile MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 20 ++++++++++++-------- src/charging-station/Helpers.ts | 7 +++++-- 2 files changed, 17 insertions(+), 10 deletions(-) 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}`; }; -- 2.34.1