From: Jérôme Benoit Date: Sat, 27 Jan 2024 14:14:23 +0000 (+0100) Subject: fix: ensure the message sequence is started after reconnection X-Git-Tag: v1.2.33~22 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=7c9741559557afc041160e600189b30a5dbeed6d;p=e-mobility-charging-stations-simulator.git fix: ensure the message sequence is started after reconnection Signed-off-by: Jérôme Benoit --- 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)