Delete supervision url configuration key if the feature is disabled from
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStation.ts
index 75094ceb104f538d92d1882c5145b3cf18fd664e..01829fdf47273797aefa980824db13282cd72862 100644 (file)
@@ -105,9 +105,7 @@ export default class ChargingStation {
   get wsConnectionUrl(): URL {
     return this.getSupervisionUrlOcppConfiguration()
       ? new URL(
-          this.getConfigurationKey(
-            this.stationInfo.supervisionUrlOcppKey ?? VendorDefaultParametersKey.ConnectionUrl
-          ).value +
+          this.getConfigurationKey(this.getSupervisionUrlOcppKey()).value +
             '/' +
             this.stationInfo.chargingStationId
         )
@@ -538,12 +536,15 @@ export default class ChargingStation {
         }
       }
     );
-    FileUtils.watchJsonFile<ChargingStationConfiguration>(
-      this.logPrefix(),
-      FileType.ChargingStationConfiguration,
-      this.configurationFile,
-      this.configuration
-    );
+    // FIXME: triggered by saveConfiguration()
+    // if (this.getOcppPersistentConfiguration()) {
+    //   FileUtils.watchJsonFile<ChargingStationConfiguration>(
+    //     this.logPrefix(),
+    //     FileType.ChargingStationConfiguration,
+    //     this.configurationFile,
+    //     this.configuration
+    //   );
+    // }
     // Handle WebSocket message
     this.wsConnection.on(
       'message',
@@ -632,10 +633,7 @@ export default class ChargingStation {
     const reboot = options.reboot;
     let keyFound = this.getConfigurationKey(key);
     if (keyFound && params?.overwrite) {
-      this.configuration.configurationKey.splice(
-        this.configuration.configurationKey.indexOf(keyFound),
-        1
-      );
+      this.deleteConfigurationKey(keyFound.key, { save: false });
       keyFound = undefined;
     }
     if (!keyFound) {
@@ -673,6 +671,21 @@ export default class ChargingStation {
     }
   }
 
+  public deleteConfigurationKey(
+    key: string | StandardParametersKey,
+    params: { save?: boolean; caseInsensitive?: boolean } = { save: true, caseInsensitive: false }
+  ): ConfigurationKey[] {
+    const keyFound = this.getConfigurationKey(key, params?.caseInsensitive);
+    if (keyFound) {
+      const deletedConfigurationKey = this.configuration.configurationKey.splice(
+        this.configuration.configurationKey.indexOf(keyFound),
+        1
+      );
+      params?.save && this.saveConfiguration();
+      return deletedConfigurationKey;
+    }
+  }
+
   public setChargingProfile(connectorId: number, cp: ChargingProfile): void {
     let cpReplaced = false;
     if (!Utils.isEmptyArray(this.getConnectorStatus(connectorId).chargingProfiles)) {
@@ -724,6 +737,10 @@ export default class ChargingStation {
     return this.stationInfo.supervisionUrlOcppConfiguration ?? false;
   }
 
+  private getSupervisionUrlOcppKey(): string {
+    return this.stationInfo.supervisionUrlOcppKey ?? VendorDefaultParametersKey.ConnectionUrl;
+  }
+
   private getChargingStationId(stationTemplate: ChargingStationTemplate): string {
     // In case of multiple instances: add instance index to charging station id
     const instanceIndex = process.env.CF_INSTANCE_INDEX ?? 0;
@@ -793,6 +810,10 @@ export default class ChargingStation {
     return this.stationInfo.ocppVersion ? this.stationInfo.ocppVersion : OCPPVersion.VERSION_16;
   }
 
+  private getOcppPersistentConfiguration(): boolean {
+    return this.stationInfo.ocppPersistentConfiguration ?? true;
+  }
+
   private handleUnsupportedVersion(version: OCPPVersion) {
     const errMsg = `${this.logPrefix()} Unsupported protocol version '${version}' configured in template file ${
       this.stationTemplateFile
@@ -805,8 +826,7 @@ export default class ChargingStation {
     this.stationInfo = this.buildStationInfo();
     this.configurationFile = path.join(
       path.resolve(__dirname, '../'),
-      'assets',
-      'configurations',
+      'assets/configurations',
       this.stationInfo.chargingStationId + '.json'
     );
     this.configuration = this.getConfiguration();
@@ -951,15 +971,18 @@ export default class ChargingStation {
   private initOcppParameters(): void {
     if (
       this.getSupervisionUrlOcppConfiguration() &&
-      !this.getConfigurationKey(
-        this.stationInfo.supervisionUrlOcppKey ?? VendorDefaultParametersKey.ConnectionUrl
-      )
+      !this.getConfigurationKey(this.getSupervisionUrlOcppKey())
     ) {
       this.addConfigurationKey(
-        this.stationInfo.supervisionUrlOcppKey ?? VendorDefaultParametersKey.ConnectionUrl,
+        this.getSupervisionUrlOcppKey(),
         this.getConfiguredSupervisionUrl().href,
         { reboot: true }
       );
+    } else if (
+      !this.getSupervisionUrlOcppConfiguration() &&
+      this.getConfigurationKey(this.getSupervisionUrlOcppKey())
+    ) {
+      this.deleteConfigurationKey(this.getSupervisionUrlOcppKey(), { save: false });
     }
     if (!this.getConfigurationKey(StandardParametersKey.SupportedFeatureProfiles)) {
       this.addConfigurationKey(
@@ -1025,7 +1048,11 @@ export default class ChargingStation {
 
   private getConfigurationFromFile(): ChargingStationConfiguration | null {
     let configuration: ChargingStationConfiguration = null;
-    if (this.configurationFile && fs.existsSync(this.configurationFile)) {
+    if (
+      this.getOcppPersistentConfiguration() &&
+      this.configurationFile &&
+      fs.existsSync(this.configurationFile)
+    ) {
       try {
         configuration = JSON.parse(
           fs.readFileSync(this.configurationFile, 'utf8')
@@ -1043,26 +1070,28 @@ export default class ChargingStation {
   }
 
   private saveConfiguration(): void {
-    if (this.configurationFile) {
-      try {
-        if (!fs.existsSync(path.dirname(this.configurationFile))) {
-          fs.mkdirSync(path.dirname(this.configurationFile), { recursive: true });
+    if (this.getOcppPersistentConfiguration()) {
+      if (this.configurationFile) {
+        try {
+          if (!fs.existsSync(path.dirname(this.configurationFile))) {
+            fs.mkdirSync(path.dirname(this.configurationFile), { recursive: true });
+          }
+          const fileDescriptor = fs.openSync(this.configurationFile, 'w');
+          fs.writeFileSync(fileDescriptor, JSON.stringify(this.configuration, null, 2));
+          fs.closeSync(fileDescriptor);
+        } catch (error) {
+          FileUtils.handleFileException(
+            this.logPrefix(),
+            FileType.ChargingStationConfiguration,
+            this.configurationFile,
+            error as NodeJS.ErrnoException
+          );
         }
-        const fileDescriptor = fs.openSync(this.configurationFile, 'w');
-        fs.writeFileSync(fileDescriptor, JSON.stringify(this.configuration, null, 2));
-        fs.closeSync(fileDescriptor);
-      } catch (error) {
-        FileUtils.handleFileException(
-          this.logPrefix(),
-          FileType.ChargingStationConfiguration,
-          this.configurationFile,
-          error as NodeJS.ErrnoException
+      } else {
+        logger.error(
+          `${this.logPrefix()} Trying to save charging station configuration to undefined file`
         );
       }
-    } else {
-      logger.error(
-        `${this.logPrefix()} Trying to save charging station configuration to undefined file`
-      );
     }
   }