feat: reenable configuration files change watchers
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStation.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;