Fix charging station started status detection
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 18 Dec 2022 10:43:54 +0000 (11:43 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 18 Dec 2022 10:43:54 +0000 (11:43 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/AutomaticTransactionGenerator.ts
src/charging-station/ChargingStation.ts
src/charging-station/ui-server/UIWebSocketServer.ts

index 2548836022a7e7381d51002a37a487f52309380e..e29ad4b839ac1286a1bfde585b317313c20d4d43 100644 (file)
@@ -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;
   }
 }
index 58648ed272932e1b064286815699256bf30d2582..5c8ec1e48d9ad7ae112340f4a553abad3cb0f37a 100644 (file)
@@ -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`
       );
index d7b3a811c72303ce77b3b836d784139f974dcbec..27d8ce844b7dc47d5d2039deb2e9c78e3c6f5adb 100644 (file)
@@ -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
+            );
           }
         });
       }