Consistently name charging stations run on CF on multiple instances
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStation.ts
index e63e067de3d0f3549a0d0ef35007091573d43cb3..67bf3b627042e9c63bfc1e83a7df5ba07369558b 100644 (file)
@@ -435,8 +435,7 @@ export default class ChargingStation {
 
   private getChargingStationId(stationTemplate: ChargingStationTemplate): string {
     // In case of multiple instances: add instance index to charging station id
-    let instanceIndex = process.env.CF_INSTANCE_INDEX ?? 0;
-    instanceIndex = instanceIndex > 0 ? instanceIndex : '';
+    const instanceIndex = process.env.CF_INSTANCE_INDEX ?? 0;
     const idSuffix = stationTemplate.nameSuffix ?? '';
     return stationTemplate.fixedName ? stationTemplate.baseName : stationTemplate.baseName + '-' + instanceIndex.toString() + ('000000000' + this.index.toString()).substr(('000000000' + this.index.toString()).length - 4) + idSuffix;
   }
@@ -631,16 +630,18 @@ export default class ChargingStation {
     this.hasSocketRestarted = false;
   }
 
-  private async onClose(closeEvent: any): Promise<void> {
-    switch (closeEvent) {
-      case WebSocketCloseEventStatusCode.CLOSE_NORMAL: // Normal close
+  private async onClose(code: number, reason: string): Promise<void> {
+    switch (code) {
+      // Normal close
+      case WebSocketCloseEventStatusCode.CLOSE_NORMAL:
       case WebSocketCloseEventStatusCode.CLOSE_NO_STATUS:
-        logger.info(`${this.logPrefix()} Socket normally closed with status '${Utils.getWebSocketCloseEventStatusString(closeEvent)}'`);
+        logger.info(`${this.logPrefix()} Socket normally closed with status '${Utils.getWebSocketCloseEventStatusString(code)}' and reason '${reason}'`);
         this.autoReconnectRetryCount = 0;
         break;
-      default: // Abnormal close
-        logger.error(`${this.logPrefix()} Socket abnormally closed with status '${Utils.getWebSocketCloseEventStatusString(closeEvent)}'`);
-        await this.reconnect(closeEvent);
+      // Abnormal close
+      default:
+        logger.error(`${this.logPrefix()} Socket abnormally closed with status '${Utils.getWebSocketCloseEventStatusString(code)}' and reason '${reason}'`);
+        await this.reconnect(code);
         break;
     }
   }
@@ -1035,7 +1036,7 @@ export default class ChargingStation {
     return !Utils.isUndefined(this.stationInfo.reconnectExponentialDelay) ? this.stationInfo.reconnectExponentialDelay : false;
   }
 
-  private async reconnect(error: Error): Promise<void> {
+  private async reconnect(code: number): Promise<void> {
     // Stop WebSocket ping
     this.stopWebSocketPing();
     // Stop heartbeat