feat: reenable configuration files change watchers
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 10 Nov 2023 13:00:40 +0000 (14:00 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 10 Nov 2023 13:00:40 +0000 (14:00 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/IdTagsCache.ts
src/utils/Configuration.ts

index 575293e8aa4ee6992e2fcac6bc2b087ec19ac04c..3ecf33d23f5e100c5948323c32402d0f04f5e268 100644 (file)
@@ -151,7 +151,7 @@ import {
   roundTo,
   secureRandom,
   sleep,
-  // watchJsonFile,
+  watchJsonFile,
 } from '../utils';
 
 export class ChargingStation extends EventEmitter {
@@ -677,55 +677,44 @@ export class ChargingStation extends EventEmitter {
         }
         this.openWSConnection();
         // Monitor charging station template file
-        // FIXME: Disabled until the spurious configuration file change detection is identified
-        // this.templateFileWatcher = watchJsonFile(
-        //   this.templateFile,
-        //   FileType.ChargingStationTemplate,
-        //   this.logPrefix(),
-        //   undefined,
-        //   (event, filename): void => {
-        //     if (isNotEmptyString(filename) && event === 'change') {
-        //       try {
-        //         logger.debug(
-        //           `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
-        //             this.templateFile
-        //           } file have changed, reload`,
-        //         );
-        //         this.sharedLRUCache.deleteChargingStationTemplate(this.templateFileHash);
-        //         // Initialize
-        //         this.initialize();
-        //         this.idTagsCache.deleteIdTags(getIdTagsFile(this.stationInfo)!);
-        //         // Restart the ATG
-        //         this.stopAutomaticTransactionGenerator()
-        //           .then(() => {
-        //             delete this.automaticTransactionGeneratorConfiguration;
-        //             if (this.getAutomaticTransactionGeneratorConfiguration()?.enable === true) {
-        //               this.startAutomaticTransactionGenerator();
-        //             }
-        //           })
-        //           .catch((err) =>
-        //             logger.error(
-        //               `${this.logPrefix()} failed to stop ATG at ${
-        //                 FileType.ChargingStationTemplate
-        //               } reload`,
-        //               err,
-        //             ),
-        //           );
-        //         if (this.getEnableStatistics() === true) {
-        //           this.performanceStatistics?.restart();
-        //         } else {
-        //           this.performanceStatistics?.stop();
-        //         }
-        //         // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
-        //       } catch (error) {
-        //         logger.error(
-        //           `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
-        //           error,
-        //         );
-        //       }
-        //     }
-        //   },
-        // );
+        this.templateFileWatcher = watchJsonFile(
+          this.templateFile,
+          FileType.ChargingStationTemplate,
+          this.logPrefix(),
+          undefined,
+          (event, filename): void => {
+            if (isNotEmptyString(filename) && event === 'change') {
+              try {
+                logger.debug(
+                  `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
+                    this.templateFile
+                  } file have changed, reload`,
+                );
+                this.sharedLRUCache.deleteChargingStationTemplate(this.templateFileHash);
+                // Initialize
+                this.initialize();
+                this.idTagsCache.deleteIdTags(getIdTagsFile(this.stationInfo)!);
+                // Restart the ATG
+                this.stopAutomaticTransactionGenerator();
+                delete this.automaticTransactionGeneratorConfiguration;
+                if (this.getAutomaticTransactionGeneratorConfiguration()?.enable === true) {
+                  this.startAutomaticTransactionGenerator();
+                }
+                if (this.getEnableStatistics() === true) {
+                  this.performanceStatistics?.restart();
+                } else {
+                  this.performanceStatistics?.stop();
+                }
+                // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
+              } catch (error) {
+                logger.error(
+                  `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
+                  error,
+                );
+              }
+            }
+          },
+        );
         this.started = true;
         this.emit(ChargingStationEvents.started);
         this.starting = false;
index 5c0a454a05d6615389de331d19b72832bd4724a4..5e0ab0b8f0778c2fbf02333c579657387a5c1d5f 100644 (file)
@@ -7,14 +7,14 @@ import {
   handleFileException,
   isNotEmptyString,
   logPrefix,
-  // logger,
+  logger,
   secureRandom,
-  // watchJsonFile,
+  watchJsonFile,
 } from '../utils';
 
 interface IdTagsCacheValueType {
   idTags: string[];
-  idTagsFileWatcher?: FSWatcher | undefined;
+  idTagsFileWatcher: FSWatcher | undefined;
 }
 
 export class IdTagsCache {
@@ -123,34 +123,33 @@ export class IdTagsCache {
   private setIdTagsCache(file: string, idTags: string[]) {
     return this.idTagsCaches.set(file, {
       idTags,
-      // FIXME: Disabled until the spurious configuration file change detection is identified
-      // idTagsFileWatcher: watchJsonFile(
-      //   file,
-      //   FileType.Authorization,
-      //   this.logPrefix(file),
-      //   undefined,
-      //   (event, filename) => {
-      //     if (isNotEmptyString(filename) && event === 'change') {
-      //       try {
-      //         logger.debug(
-      //           `${this.logPrefix(file)} ${FileType.Authorization} file have changed, reload`,
-      //         );
-      //         this.deleteIdTagsCache(file);
-      //         this.deleteIdTagsCacheIndexes(file);
-      //       } catch (error) {
-      //         handleFileException(
-      //           file,
-      //           FileType.Authorization,
-      //           error as NodeJS.ErrnoException,
-      //           this.logPrefix(file),
-      //           {
-      //             throwError: false,
-      //           },
-      //         );
-      //       }
-      //     }
-      //   },
-      // ),
+      idTagsFileWatcher: watchJsonFile(
+        file,
+        FileType.Authorization,
+        this.logPrefix(file),
+        undefined,
+        (event, filename) => {
+          if (isNotEmptyString(filename) && event === 'change') {
+            try {
+              logger.debug(
+                `${this.logPrefix(file)} ${FileType.Authorization} file have changed, reload`,
+              );
+              this.deleteIdTagsCache(file);
+              this.deleteIdTagsCacheIndexes(file);
+            } catch (error) {
+              handleFileException(
+                file,
+                FileType.Authorization,
+                error as NodeJS.ErrnoException,
+                this.logPrefix(file),
+                {
+                  throwError: false,
+                },
+              );
+            }
+          }
+        },
+      ),
     });
   }
 
index ebf99c55fbe793b3c69a02cf08c84bf7abf0989f..1f370480d4d1b6fc47828792ccbf8271cfb81724 100644 (file)
@@ -529,10 +529,9 @@ export class Configuration {
         Configuration.configurationData = JSON.parse(
           readFileSync(Configuration.configurationFile, 'utf8'),
         ) as ConfigurationData;
-        // FIXME: Disabled until the spurious configuration file change detection is identified
-        // if (!Configuration.configurationFileWatcher) {
-        //   Configuration.configurationFileWatcher = Configuration.getConfigurationFileWatcher();
-        // }
+        if (!Configuration.configurationFileWatcher) {
+          Configuration.configurationFileWatcher = Configuration.getConfigurationFileWatcher();
+        }
       } catch (error) {
         Configuration.handleFileException(
           Configuration.configurationFile,