refactor: cleanup charging station events handling
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 27 Jan 2024 14:26:45 +0000 (15:26 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 27 Jan 2024 14:26:45 +0000 (15:26 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts

index 44957e1e4b15eb015b8539b1566f834b94dd5087..31b8cf794b4e5fd0b041eb20a468294d91715ba0 100644 (file)
@@ -180,7 +180,6 @@ export class ChargingStation extends EventEmitter {
   private evsesConfigurationHash!: string
   private automaticTransactionGeneratorConfiguration?: AutomaticTransactionGeneratorConfiguration
   private ocppIncomingRequestService!: OCPPIncomingRequestService
-  private acceptedEventListenerRegistered: boolean
   private readonly messageBuffer: Set<string>
   private configuredSupervisionUrl!: URL
   private autoReconnectRetryCount: number
@@ -196,7 +195,6 @@ export class ChargingStation extends EventEmitter {
     this.started = false
     this.starting = false
     this.stopping = false
-    this.acceptedEventListenerRegistered = false
     this.wsConnection = null
     this.autoReconnectRetryCount = 0
     this.index = index
@@ -218,6 +216,11 @@ export class ChargingStation extends EventEmitter {
     this.on(ChargingStationEvents.updated, () => {
       parentPort?.postMessage(buildUpdatedMessage(this))
     })
+    this.on(ChargingStationEvents.accepted, () => {
+      this.startMessageSequence().catch(error => {
+        logger.error(`${this.logPrefix()} Error while starting the message sequence:`, error)
+      })
+    })
 
     this.initialize()
   }
@@ -1740,16 +1743,6 @@ export class ChargingStation extends EventEmitter {
     return ocppConfiguration
   }
 
-  private registerAcceptedEventListener (): void {
-    this.once(ChargingStationEvents.accepted, () => {
-      this.startMessageSequence().catch(error => {
-        logger.error(`${this.logPrefix()} Error while starting the message sequence:`, error)
-      })
-      this.acceptedEventListenerRegistered = false
-    })
-    this.acceptedEventListenerRegistered = true
-  }
-
   private async onOpen (): Promise<void> {
     if (this.isWebSocketConnectionOpened()) {
       logger.info(
@@ -1788,9 +1781,6 @@ export class ChargingStation extends EventEmitter {
             this.stationInfo?.registrationMaxRetries === -1)
         )
       }
-      if (!this.acceptedEventListenerRegistered) {
-        this.registerAcceptedEventListener()
-      }
       if (this.isRegistered()) {
         this.emit(ChargingStationEvents.registered)
         if (this.inAcceptedState()) {