Add helpers to close and terminate WS connection
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 27 May 2022 10:11:55 +0000 (12:11 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 27 May 2022 10:11:55 +0000 (12:11 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts

index 7d853547883eb45031c3bbdc407e9149a167a4f9..14bb05a10637468dca4f3c0d09a02cc6750215c7 100644 (file)
@@ -554,9 +554,7 @@ export default class ChargingStation {
         this.getConnectorStatus(connectorId).status = ChargePointStatus.UNAVAILABLE;
       }
     }
-    if (this.isWebSocketConnectionOpened()) {
-      this.wsConnection.close();
-    }
+    this.closeWSConnection();
     if (this.getEnableStatistics()) {
       this.performanceStatistics.stop();
     }
@@ -1893,11 +1891,11 @@ export default class ChargingStation {
     ) {
       options.auth = `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
     }
-    if (this.isWebSocketConnectionOpened() && params?.closeOpened) {
-      this.wsConnection.close();
+    if (params?.closeOpened) {
+      this.closeWSConnection();
     }
-    if (this.isWebSocketConnectionOpened() && params?.terminateOpened) {
-      this.wsConnection.terminate();
+    if (params?.terminateOpened) {
+      this.terminateWSConnection();
     }
     let protocol: string;
     switch (this.getOcppVersion()) {
@@ -1914,6 +1912,20 @@ export default class ChargingStation {
     );
   }
 
+  private closeWSConnection(): void {
+    if (this.isWebSocketConnectionOpened()) {
+      this.wsConnection.close();
+      this.wsConnection = null;
+    }
+  }
+
+  private terminateWSConnection(): void {
+    if (this.isWebSocketConnectionOpened()) {
+      this.wsConnection.terminate();
+      this.wsConnection = null;
+    }
+  }
+
   private stopMeterValues(connectorId: number) {
     if (this.getConnectorStatus(connectorId)?.transactionSetInterval) {
       clearInterval(this.getConnectorStatus(connectorId).transactionSetInterval);