fix: fix recursion loop getTemplateFromFile -> logPrefix -> getTemplateFromFile
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 26 Nov 2023 22:49:48 +0000 (23:49 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 26 Nov 2023 22:49:48 +0000 (23:49 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/Helpers.ts

index f0fa08208c79241bf23d0c7cdd05b4c1d7e14991..ba430878d4947641da61c72548fc58e437e75914 100644 (file)
@@ -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 {
index f0f848d38c96cfdddc9a5fce3ae7d68d5622ba2c..ce0ba3e373cc02cc588615e29f02ebee3eea615a 100644 (file)
@@ -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}`;
 };