fix: ensure the message sequence is started after reconnection
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 27 Jan 2024 14:14:23 +0000 (15:14 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 27 Jan 2024 14:14:23 +0000 (15:14 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts

index e9d94651d1d2dd57fccd4440708663206592a4d5..44957e1e4b15eb015b8539b1566f834b94dd5087 100644 (file)
@@ -180,6 +180,7 @@ 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
@@ -195,6 +196,7 @@ 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
@@ -1251,11 +1253,6 @@ export class ChargingStation extends EventEmitter {
     this.ocppConfiguration = this.getOcppConfiguration()
     this.initializeOcppConfiguration()
     this.initializeOcppServices()
-    this.once(ChargingStationEvents.accepted, () => {
-      this.startMessageSequence().catch(error => {
-        logger.error(`${this.logPrefix()} Error while starting the message sequence:`, error)
-      })
-    })
     if (this.stationInfo.autoRegister === true) {
       this.bootNotificationResponse = {
         currentTime: new Date(),
@@ -1743,6 +1740,16 @@ 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(
@@ -1781,6 +1788,9 @@ export class ChargingStation extends EventEmitter {
             this.stationInfo?.registrationMaxRetries === -1)
         )
       }
+      if (!this.acceptedEventListenerRegistered) {
+        this.registerAcceptedEventListener()
+      }
       if (this.isRegistered()) {
         this.emit(ChargingStationEvents.registered)
         if (this.inAcceptedState()) {
@@ -1821,7 +1831,10 @@ export class ChargingStation extends EventEmitter {
             code
           )}' and reason '${reason.toString()}'`
         )
-        this.started && this.reconnect().catch(Constants.EMPTY_FUNCTION)
+        this.started &&
+          this.reconnect().catch(error =>
+            logger.error(`${this.logPrefix()} Error while reconnecting:`, error)
+          )
         break
     }
     this.emit(ChargingStationEvents.updated)