refactor: cleanup worker events handling in main thread
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 5 Feb 2024 21:48:30 +0000 (22:48 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 5 Feb 2024 21:48:30 +0000 (22:48 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/Bootstrap.ts

index 2c1b0f43b3443f8f239888b7f4e507ad6eefd584..75accffc9f72b723583fdd8f1681845083e85668 100644 (file)
@@ -150,6 +150,15 @@ export class Bootstrap extends EventEmitter {
           ChargingStationWorkerMessageEvents.performanceStatistics,
           this.workerEventPerformanceStatistics
         )
+        this.on(
+          ChargingStationWorkerMessageEvents.workerElementError,
+          (msg: ChargingStationWorkerMessage<ChargingStationWorkerEventError>) => {
+            logger.error(
+              `${this.logPrefix()} ${moduleName}.messageHandler: Error occurred while handling '${msg.data.event}' event on worker:`,
+              msg.data
+            )
+          }
+        )
         this.initializeCounters()
         const workerConfiguration = Configuration.getConfigurationSection<WorkerConfiguration>(
           ConfigurationSection.worker
@@ -338,30 +347,23 @@ export class Bootstrap extends EventEmitter {
     try {
       switch (msg.event) {
         case ChargingStationWorkerMessageEvents.added:
-          this.emit(ChargingStationWorkerMessageEvents.added, msg.data as ChargingStationData)
+          this.emit(ChargingStationWorkerMessageEvents.added, msg.data)
           break
         case ChargingStationWorkerMessageEvents.started:
-          this.emit(ChargingStationWorkerMessageEvents.started, msg.data as ChargingStationData)
+          this.emit(ChargingStationWorkerMessageEvents.started, msg.data)
           break
         case ChargingStationWorkerMessageEvents.stopped:
-          this.emit(ChargingStationWorkerMessageEvents.stopped, msg.data as ChargingStationData)
+          this.emit(ChargingStationWorkerMessageEvents.stopped, msg.data)
           break
         case ChargingStationWorkerMessageEvents.updated:
-          this.emit(ChargingStationWorkerMessageEvents.updated, msg.data as ChargingStationData)
+          this.emit(ChargingStationWorkerMessageEvents.updated, msg.data)
           break
         case ChargingStationWorkerMessageEvents.performanceStatistics:
-          this.emit(
-            ChargingStationWorkerMessageEvents.performanceStatistics,
-            msg.data as Statistics
-          )
+          this.emit(ChargingStationWorkerMessageEvents.performanceStatistics, msg.data)
           break
         case ChargingStationWorkerMessageEvents.addedWorkerElement:
           break
         case ChargingStationWorkerMessageEvents.workerElementError:
-          logger.error(
-            `${this.logPrefix()} ${moduleName}.messageHandler: Error occurred while handling '${(msg.data as ChargingStationWorkerEventError).event}' event on worker:`,
-            msg.data
-          )
           this.emit(ChargingStationWorkerMessageEvents.workerElementError, msg.data)
           break
         default: