Refine comment at CS configuration file handling
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStation.ts
index b570f74afa2ed8036850773d67545d95ad82f6f3..757cea0f8eb448cd73906fbcf97db6a4a28d2929 100644 (file)
@@ -895,8 +895,14 @@ export default class ChargingStation {
     try {
       const measureId = `${FileType.ChargingStationTemplate} read`;
       const beginId = PerformanceStatistics.beginMeasure(measureId);
-      template = JSON.parse(fs.readFileSync(this.templateFile, 'utf8')) as ChargingStationTemplate;
+      template =
+        (JSON.parse(fs.readFileSync(this.templateFile, 'utf8')) as ChargingStationTemplate) ??
+        ({} as ChargingStationTemplate);
       PerformanceStatistics.endMeasure(measureId, beginId);
+      template.templateHash = crypto
+        .createHash(Constants.DEFAULT_HASH_ALGORITHM)
+        .update(JSON.stringify(template))
+        .digest('hex');
     } catch (error) {
       FileUtils.handleFileException(
         this.logPrefix(),
@@ -943,12 +949,7 @@ export default class ChargingStation {
   }
 
   private getStationInfoFromTemplate(): ChargingStationInfo {
-    const stationInfo: ChargingStationInfo =
-      this.getTemplateFromFile() ?? ({} as ChargingStationInfo);
-    stationInfo.hash = crypto
-      .createHash(Constants.DEFAULT_HASH_ALGORITHM)
-      .update(JSON.stringify(stationInfo))
-      .digest('hex');
+    const stationInfo: ChargingStationInfo = this.getTemplateFromFile();
     const chargingStationId = this.getChargingStationId(stationInfo);
     // Deprecation template keys section
     this.warnDeprecatedTemplateKey(
@@ -982,8 +983,13 @@ export default class ChargingStation {
     return stationInfo;
   }
 
-  private getStationInfoFromFile(): ChargingStationInfo | null {
-    return this.getConfigurationFromFile()?.stationInfo ?? null;
+  private getStationInfoFromFile(): ChargingStationInfo {
+    const stationInfo = this.getConfigurationFromFile()?.stationInfo ?? ({} as ChargingStationInfo);
+    stationInfo.infoHash = crypto
+      .createHash(Constants.DEFAULT_HASH_ALGORITHM)
+      .update(JSON.stringify(stationInfo))
+      .digest('hex');
+    return stationInfo;
   }
 
   private getStationInfo(): ChargingStationInfo {
@@ -996,11 +1002,17 @@ export default class ChargingStation {
       this.hashId + '.json'
     );
     const stationInfoFromFile: ChargingStationInfo = this.getStationInfoFromFile();
-    if (stationInfoFromFile?.hash === stationInfoFromTemplate.hash) {
+    // Priority: charging station info from template > charging station info from configuration file > charging station info attribute
+    if (stationInfoFromFile?.templateHash === stationInfoFromTemplate.templateHash) {
       return stationInfoFromFile;
+    } else if (stationInfoFromFile?.templateHash !== stationInfoFromTemplate.templateHash) {
+      this.createSerialNumber(stationInfoFromTemplate, stationInfoFromFile);
+      return stationInfoFromTemplate;
+    }
+    if (this.stationInfo?.infoHash === stationInfoFromFile?.infoHash) {
+      return this.stationInfo;
     }
-    this.createSerialNumber(stationInfoFromTemplate, stationInfoFromFile);
-    return stationInfoFromTemplate;
+    return stationInfoFromFile;
   }
 
   private saveStationInfo(): void {
@@ -1517,7 +1529,9 @@ export default class ChargingStation {
             } else {
               throw new OCPPError(
                 ErrorType.PROTOCOL_ERROR,
-                `Cached request for message id ${messageId} response is not iterable`
+                `Cached request for message id ${messageId} response is not iterable`,
+                null,
+                cachedRequest as unknown as JsonType
               );
             }
             logger.debug(
@@ -1529,7 +1543,9 @@ export default class ChargingStation {
               // Error
               throw new OCPPError(
                 ErrorType.INTERNAL_ERROR,
-                `Response for unknown message id ${messageId}`
+                `Response for unknown message id ${messageId}`,
+                null,
+                commandPayload
               );
             }
             responseCallback(commandPayload, requestPayload);
@@ -1543,7 +1559,9 @@ export default class ChargingStation {
             } else {
               throw new OCPPError(
                 ErrorType.PROTOCOL_ERROR,
-                `Cached request for message id ${messageId} error response is not iterable`
+                `Cached request for message id ${messageId} error response is not iterable`,
+                null,
+                cachedRequest as unknown as JsonType
               );
             }
             logger.debug(
@@ -1555,7 +1573,9 @@ export default class ChargingStation {
               // Error
               throw new OCPPError(
                 ErrorType.INTERNAL_ERROR,
-                `Error response for unknown message id ${messageId}`
+                `Error response for unknown message id ${messageId}`,
+                null,
+                { errorType, errorMessage, errorDetails }
               );
             }
             rejectCallback(