From cbf9b87853619abe4cd79659b3e56bb8b1d6871b Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 18 Dec 2022 11:43:54 +0100 Subject: [PATCH] Fix charging station started status detection MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../AutomaticTransactionGenerator.ts | 5 ++- src/charging-station/ChargingStation.ts | 4 +- .../ui-server/UIWebSocketServer.ts | 44 ++++++++++--------- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 25488360..e29ad4b8 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -455,9 +455,10 @@ export default class AutomaticTransactionGenerator extends AsyncResource { } private checkChargingStation(connectorId?: number): boolean { - if (this.chargingStation.started === false) { + if (this.chargingStation.started === false && this.chargingStation.starting === false) { logger.warn(`${this.logPrefix(connectorId)} charging station is stopped, cannot proceed`); + return false; } - return this.chargingStation.started; + return true; } } diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 58648ed2..5c8ec1e4 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -91,6 +91,7 @@ export default class ChargingStation { public readonly templateFile: string; public stationInfo!: ChargingStationInfo; public started: boolean; + public starting: boolean; public authorizedTagsCache: AuthorizedTagsCache; public automaticTransactionGenerator!: AutomaticTransactionGenerator; public ocppConfiguration!: ChargingStationOcppConfiguration; @@ -103,7 +104,6 @@ export default class ChargingStation { public bootNotificationRequest!: BootNotificationRequest; public bootNotificationResponse!: BootNotificationResponse | null; public powerDivider!: number; - private starting: boolean; private stopping: boolean; private configurationFile!: string; private configurationFileHash!: string; @@ -608,7 +608,7 @@ export default class ChargingStation { options.handshakeTimeout = options?.handshakeTimeout ?? this.getConnectionTimeout() * 1000; params.closeOpened = params?.closeOpened ?? false; params.terminateOpened = params?.terminateOpened ?? false; - if (this.started === false) { + if (this.started === false && this.starting === false) { logger.warn( `${this.logPrefix()} Cannot open OCPP connection to URL ${this.wsConnectionUrl.toString()} on stopped charging station` ); diff --git a/src/charging-station/ui-server/UIWebSocketServer.ts b/src/charging-station/ui-server/UIWebSocketServer.ts index d7b3a811..27d8ce84 100644 --- a/src/charging-station/ui-server/UIWebSocketServer.ts +++ b/src/charging-station/ui-server/UIWebSocketServer.ts @@ -68,32 +68,34 @@ export default class UIWebSocketServer extends AbstractUIServer { ); }); }); + // eslint-disable-next-line @typescript-eslint/no-unused-vars + this.httpServer.on('connect', (req: IncomingMessage, socket: internal.Duplex, head: Buffer) => { + if (req.headers?.connection !== 'Upgrade' || req.headers?.upgrade !== 'websocket') { + socket.write(`HTTP/1.1 ${StatusCodes.BAD_REQUEST} Bad Request\r\n\r\n`); + socket.destroy(); + } + }); this.httpServer.on( 'upgrade', (req: IncomingMessage, socket: internal.Duplex, head: Buffer): void => { this.authenticate(req, (err) => { - if (req.headers?.connection === 'Upgrade' && req.headers?.upgrade === 'websocket') { - if (err) { - socket.write(`HTTP/1.1 ${StatusCodes.UNAUTHORIZED} Unauthorized\r\n\r\n`); - socket.destroy(); - return; - } - try { - this.webSocketServer.handleUpgrade(req, socket, head, (ws: WebSocket) => { - this.webSocketServer.emit('connection', ws, req); - }); - } catch (error) { - logger.error( - `${this.logPrefix( - moduleName, - 'start.httpServer.on.upgrade' - )} Error at handling connection upgrade:`, - error - ); - } - } else { - socket.write(`HTTP/1.1 ${StatusCodes.BAD_REQUEST} Bad Request\r\n\r\n`); + if (err) { + socket.write(`HTTP/1.1 ${StatusCodes.UNAUTHORIZED} Unauthorized\r\n\r\n`); socket.destroy(); + return; + } + try { + this.webSocketServer.handleUpgrade(req, socket, head, (ws: WebSocket) => { + this.webSocketServer.emit('connection', ws, req); + }); + } catch (error) { + logger.error( + `${this.logPrefix( + moduleName, + 'start.httpServer.on.upgrade' + )} Error at handling connection upgrade:`, + error + ); } }); } -- 2.34.1