Prepare the code for ESM support
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStation.ts
index 7d853547883eb45031c3bbdc407e9149a167a4f9..e114da41c951aba7977e9a21fbefe1ea12097fe8 100644 (file)
@@ -39,8 +39,9 @@ import {
   StopTransactionRequest,
   StopTransactionResponse,
 } from '../types/ocpp/Transaction';
+import { URL, fileURLToPath } from 'url';
 import { WSError, WebSocketCloseEventStatusCode } from '../types/WebSocket';
-import WebSocket, { Data, OPEN, RawData } from 'ws';
+import WebSocket, { Data, RawData } from 'ws';
 
 import AutomaticTransactionGenerator from './AutomaticTransactionGenerator';
 import { AutomaticTransactionGeneratorConfiguration } from '../types/AutomaticTransactionGenerator';
@@ -72,7 +73,6 @@ import OCPPRequestService from './ocpp/OCPPRequestService';
 import { OCPPVersion } from '../types/ocpp/OCPPVersion';
 import PerformanceStatistics from '../performance/PerformanceStatistics';
 import { SupervisionUrlDistribution } from '../types/ConfigurationData';
-import { URL } from 'url';
 import Utils from '../utils/Utils';
 import crypto from 'crypto';
 import fs from 'fs';
@@ -180,7 +180,7 @@ export default class ChargingStation {
   }
 
   public isWebSocketConnectionOpened(): boolean {
-    return this?.wsConnection?.readyState === OPEN;
+    return this?.wsConnection?.readyState === WebSocket.OPEN;
   }
 
   public getRegistrationStatus(): RegistrationStatus {
@@ -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();
     }
@@ -879,7 +877,7 @@ export default class ChargingStation {
     this.hashId = ChargingStationUtils.getHashId(this.index, this.getTemplateFromFile());
     logger.info(`${this.logPrefix()} Charging station hashId '${this.hashId}'`);
     this.configurationFile = path.join(
-      path.resolve(__dirname, '../'),
+      path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../'),
       'assets',
       'configurations',
       this.hashId + '.json'
@@ -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);