Ensure that workerSet always properly start a worker at init
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStation.ts
index 4e9abedf3fd9a6b8331079171ceb95958312c028..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();
     }
@@ -1886,18 +1884,18 @@ export default class ChargingStation {
   ): void {
     options.handshakeTimeout = options?.handshakeTimeout ?? this.getConnectionTimeout() * 1000;
     params.closeOpened = params?.closeOpened ?? false;
-    params.terminateOpened = params?.closeOpened ?? false;
+    params.terminateOpened = params?.terminateOpened ?? false;
     if (
       !Utils.isNullOrUndefined(this.stationInfo.supervisionUser) &&
       !Utils.isNullOrUndefined(this.stationInfo.supervisionPassword)
     ) {
       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);