Update dependencies.
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStation.ts
index 4f19f2d9581c810fbb1a25a7e2609d32381a08a3..b5ec5209ad8a56a2aa713a08de0a50da529b881e 100644 (file)
@@ -29,6 +29,7 @@ import { WebSocketCloseEventStatusCode } from '../types/WebSocket';
 import crypto from 'crypto';
 import fs from 'fs';
 import logger from '../utils/Logger';
+import path from 'path';
 
 export default class ChargingStation {
   public stationTemplateFile: string;
@@ -223,7 +224,7 @@ export default class ChargingStation {
         }
       }, interval);
     } else {
-      logger.error(`${this.logPrefix()} Charging station ${StandardParametersKey.MeterValueSampleInterval} configuration set to ${Utils.milliSecondsToHHMMSS(interval)}, not sending MeterValues`);
+      logger.error(`${this.logPrefix()} Charging station ${StandardParametersKey.MeterValueSampleInterval} configuration set to ${interval ? Utils.milliSecondsToHHMMSS(interval) : interval}, not sending MeterValues`);
     }
   }
 
@@ -319,22 +320,31 @@ export default class ChargingStation {
     }
   }
 
-  public addMessageToBuffer(message: string): void {
+  public addToMessageQueue(message: string): void {
     let dups = false;
-    // Handle dups in buffer
+    // Handle dups in message queue
     for (const bufferedMessage of this.messageQueue) {
-      // Same message
+      // Message already in the queue
       if (message === bufferedMessage) {
         dups = true;
         break;
       }
     }
     if (!dups) {
-      // Buffer message
+      // Queue message
       this.messageQueue.push(message);
     }
   }
 
+  private flushMessageQueue() {
+    if (!Utils.isEmptyArray(this.messageQueue)) {
+      this.messageQueue.forEach((message, index) => {
+        this.messageQueue.splice(index, 1);
+        this.wsConnection.send(message);
+      });
+    }
+  }
+
   private getChargingStationId(stationTemplate: ChargingStationTemplate): string {
     // In case of multiple instances: add instance index to charging station id
     let instanceIndex = process.env.CF_INSTANCE_INDEX ? process.env.CF_INSTANCE_INDEX : 0;
@@ -470,22 +480,19 @@ export default class ChargingStation {
       // Send BootNotification
       let registrationRetryCount = 0;
       do {
-        this.bootNotificationResponse = await this.ocppRequestService.sendBootNotification(this.bootNotificationRequest.chargePointModel, this.bootNotificationRequest.chargePointVendor, this.bootNotificationRequest.chargeBoxSerialNumber, this.bootNotificationRequest.firmwareVersion);
+        this.bootNotificationResponse = await this.ocppRequestService.sendBootNotification(this.bootNotificationRequest.chargePointModel,
+          this.bootNotificationRequest.chargePointVendor, this.bootNotificationRequest.chargeBoxSerialNumber, this.bootNotificationRequest.firmwareVersion);
         if (!this.isRegistered()) {
           registrationRetryCount++;
           await Utils.sleep(this.bootNotificationResponse?.interval ? this.bootNotificationResponse.interval * 1000 : Constants.OCPP_DEFAULT_BOOT_NOTIFICATION_INTERVAL);
         }
       } while (!this.isRegistered() && (registrationRetryCount <= this.getRegistrationMaxRetries() || this.getRegistrationMaxRetries() === -1));
-    } else if (this.isRegistered()) {
+    }
+    if (this.isRegistered()) {
       await this.startMessageSequence();
       this.hasStopped && (this.hasStopped = false);
       if (this.hasSocketRestarted && this.isWebSocketOpen()) {
-        if (!Utils.isEmptyArray(this.messageQueue)) {
-          this.messageQueue.forEach((message, index) => {
-            this.messageQueue.splice(index, 1);
-            this.wsConnection.send(message);
-          });
-        }
+        this.flushMessageQueue();
       }
     } else {
       logger.error(`${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`);
@@ -593,7 +600,7 @@ export default class ChargingStation {
   }
 
   private getAuthorizationFile(): string {
-    return this.stationInfo.authorizationFile && this.stationInfo.authorizationFile;
+    return this.stationInfo.authorizationFile && path.join(path.resolve(__dirname, '../'), 'assets', path.basename(this.stationInfo.authorizationFile));
   }
 
   private getAuthorizedTags(): string[] {