refactor(simulator): remove unneeded intermediate variable
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStation.ts
index 282b6aefb18ea80a9d265926cdc66d4409092ed5..3f3248c99e8c95f0bde688a1c6f9e92dced424d7 100644 (file)
@@ -228,19 +228,18 @@ export class ChargingStation extends EventEmitter {
 
   public get stationInfo(): ChargingStationInfo {
     return {
-      ...this.internalStationInfo,
       ...{
         enableStatistics: false,
         remoteAuthorization: true,
         currentOutType: CurrentType.AC,
+        mainVoltageMeterValues: true,
+        phaseLineToLineVoltageMeterValues: false,
+        customValueLimitationMeterValues: true,
         ocppStrictCompliance: true,
         outOfOrderEndMeterValues: false,
         beginEndMeterValues: false,
         meteringPerTransaction: true,
         transactionDataMeterValues: false,
-        mainVoltageMeterValues: true,
-        phaseLineToLineVoltageMeterValues: false,
-        customValueLimitationMeterValues: true,
         supervisionUrlOcppConfiguration: false,
         supervisionUrlOcppKey: VendorParametersKey.ConnectionUrl,
         ocppVersion: OCPPVersion.VERSION_16,
@@ -252,6 +251,7 @@ export class ChargingStation extends EventEmitter {
         reconnectExponentialDelay: false,
         stopTransactionsOnStopped: true,
       },
+      ...this.internalStationInfo,
     };
   }
 
@@ -513,7 +513,7 @@ export class ChargingStation extends EventEmitter {
 
   public setSupervisionUrl(url: string): void {
     if (
-      this.stationInfo?.supervisionUrlOcppConfiguration &&
+      this.stationInfo?.supervisionUrlOcppConfiguration === true &&
       isNotEmptyString(this.stationInfo?.supervisionUrlOcppKey)
     ) {
       setConfigurationKeyValue(this, this.stationInfo.supervisionUrlOcppKey!, url);
@@ -1115,6 +1115,7 @@ export class ChargingStation extends EventEmitter {
     stationInfo.chargingStationId = getChargingStationId(this.index, stationTemplate);
     stationInfo.ocppVersion = stationTemplate?.ocppVersion ?? OCPPVersion.VERSION_16;
     createSerialNumber(stationTemplate, stationInfo);
+    stationInfo.voltageOut = this.getVoltageOut(stationInfo);
     if (isNotEmptyArray(stationTemplate?.power)) {
       stationTemplate.power = stationTemplate.power as number[];
       const powerArrayRandomIndex = Math.floor(secureRandom() * stationTemplate.power.length);
@@ -1129,6 +1130,7 @@ export class ChargingStation extends EventEmitter {
           ? stationTemplate.power * 1000
           : stationTemplate.power;
     }
+    stationInfo.maximumAmperage = this.getMaximumAmperage(stationInfo);
     stationInfo.firmwareVersionPattern =
       stationTemplate?.firmwareVersionPattern ?? Constants.SEMVER_PATTERN;
     if (
@@ -1153,7 +1155,6 @@ export class ChargingStation extends EventEmitter {
     stationInfo.resetTime = !isNullOrUndefined(stationTemplate?.resetTime)
       ? secondsToMilliseconds(stationTemplate.resetTime!)
       : Constants.CHARGING_STATION_DEFAULT_RESET_TIME;
-    stationInfo.maximumAmperage = this.getMaximumAmperage(stationInfo);
     return stationInfo;
   }
 
@@ -1192,7 +1193,7 @@ export class ChargingStation extends EventEmitter {
     }
   }
 
-  private handleUnsupportedVersion(version: OCPPVersion) {
+  private handleUnsupportedVersion(version: OCPPVersion | undefined) {
     const errorMsg = `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`;
     logger.error(`${this.logPrefix()} ${errorMsg}`);
     throw new BaseError(errorMsg);
@@ -1249,6 +1250,11 @@ export class ChargingStation extends EventEmitter {
     this.ocppConfiguration = this.getOcppConfiguration();
     this.initializeOcppConfiguration();
     this.initializeOcppServices();
+    this.once(ChargingStationEvents.accepted, () => {
+      this.startMessageSequence().catch((error) => {
+        logger.error(`${this.logPrefix()} Error while starting the message sequence:`, error);
+      });
+    });
     if (this.stationInfo?.autoRegister === true) {
       this.bootNotificationResponse = {
         currentTime: new Date(),
@@ -1735,9 +1741,9 @@ export class ChargingStation extends EventEmitter {
       logger.info(
         `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`,
       );
+      let registrationRetryCount = 0;
       if (this.isRegistered() === false) {
         // Send BootNotification
-        let registrationRetryCount = 0;
         do {
           this.bootNotificationResponse = await this.ocppRequestService.requestHandler<
             BootNotificationRequest,
@@ -1763,11 +1769,10 @@ export class ChargingStation extends EventEmitter {
         this.emit(ChargingStationEvents.registered);
         if (this.inAcceptedState() === true) {
           this.emit(ChargingStationEvents.accepted);
-          await this.startMessageSequence();
         }
       } else {
         logger.error(
-          `${this.logPrefix()} Registration failure: max retries reached or retry disabled (${this
+          `${this.logPrefix()} Registration failure: maximum retries reached (${registrationRetryCount}) or retry disabled (${this
             .stationInfo?.registrationMaxRetries})`,
         );
       }
@@ -2037,7 +2042,7 @@ export class ChargingStation extends EventEmitter {
 
   private getPowerDivider(): number {
     let powerDivider = this.hasEvses ? this.getNumberOfEvses() : this.getNumberOfConnectors();
-    if (this.stationInfo?.powerSharedByConnectors) {
+    if (this.stationInfo?.powerSharedByConnectors === true) {
       powerDivider = this.getNumberOfRunningTransactions();
     }
     return powerDivider;
@@ -2062,16 +2067,14 @@ export class ChargingStation extends EventEmitter {
   }
 
   private getCurrentOutType(stationInfo?: ChargingStationInfo): CurrentType {
-    return (stationInfo ?? this.stationInfo).currentOutType!;
+    return (stationInfo ?? this.stationInfo).currentOutType ?? CurrentType.AC;
   }
 
   private getVoltageOut(stationInfo?: ChargingStationInfo): number {
-    const defaultVoltageOut = getDefaultVoltageOut(
-      this.getCurrentOutType(stationInfo),
-      this.logPrefix(),
-      this.templateFile,
+    return (
+      (stationInfo ?? this.stationInfo).voltageOut ??
+      getDefaultVoltageOut(this.getCurrentOutType(stationInfo), this.logPrefix(), this.templateFile)
     );
-    return (stationInfo ?? this.stationInfo).voltageOut ?? defaultVoltageOut;
   }
 
   private getAmperageLimitation(): number | undefined {
@@ -2152,12 +2155,12 @@ export class ChargingStation extends EventEmitter {
     this.stopWebSocketPing();
     // Stop heartbeat
     this.stopHeartbeat();
-    // Stop ongoing transactions
-    stopTransactions && (await this.stopRunningTransactions(reason));
     // Stop the ATG
     if (this.automaticTransactionGenerator?.started === true) {
       this.stopAutomaticTransactionGenerator();
     }
+    // Stop ongoing transactions
+    stopTransactions && (await this.stopRunningTransactions(reason));
     if (this.hasEvses) {
       for (const [evseId, evseStatus] of this.evses) {
         if (evseId > 0) {