From 7c9741559557afc041160e600189b30a5dbeed6d Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 27 Jan 2024 15:14:23 +0100 Subject: [PATCH] fix: ensure the message sequence is started after reconnection MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index e9d94651..44957e1e 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -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 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 { 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) -- 2.34.1