fix: add charging station at instantiation
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStation.ts
index 36cd0958fffe778f3cb68c0402be8b3285f58f4f..0f4bce43184793c9d3f7c221cb152f26dac5d9eb 100644 (file)
@@ -123,6 +123,7 @@ import {
   Configuration,
   Constants,
   DCElectricUtils,
+  buildAddedMessage,
   buildChargingStationAutomaticTransactionGeneratorConfiguration,
   buildConnectorsStatus,
   buildEvsesStatus,
@@ -206,6 +207,9 @@ export class ChargingStation extends EventEmitter {
     this.idTagsCache = IdTagsCache.getInstance()
     this.chargingStationWorkerBroadcastChannel = new ChargingStationWorkerBroadcastChannel(this)
 
+    this.on(ChargingStationEvents.added, () => {
+      parentPort?.postMessage(buildAddedMessage(this))
+    })
     this.on(ChargingStationEvents.started, () => {
       parentPort?.postMessage(buildStartedMessage(this))
     })
@@ -240,6 +244,10 @@ export class ChargingStation extends EventEmitter {
     })
 
     this.initialize()
+
+    this.add()
+
+    this.stationInfo?.autoStart === true && this.start()
   }
 
   public get hasEvses (): boolean {
@@ -646,6 +654,10 @@ export class ChargingStation extends EventEmitter {
     }
   }
 
+  private add (): void {
+    this.emit(ChargingStationEvents.added)
+  }
+
   public start (): void {
     if (!this.started) {
       if (!this.starting) {
@@ -1129,6 +1141,7 @@ export class ChargingStation extends EventEmitter {
     }
     const stationInfo = stationTemplateToStationInfo(stationTemplate)
     stationInfo.hashId = getHashId(this.index, stationTemplate)
+    stationInfo.autoStart = stationTemplate.autoStart ?? true
     stationInfo.templateName = parse(this.templateFile).name
     stationInfo.chargingStationId = getChargingStationId(this.index, stationTemplate)
     stationInfo.ocppVersion = stationTemplate.ocppVersion ?? OCPPVersion.VERSION_16
@@ -1188,6 +1201,9 @@ export class ChargingStation extends EventEmitter {
         if (stationInfo.templateName == null) {
           stationInfo.templateName = parse(this.templateFile).name
         }
+        if (stationInfo.autoStart == null) {
+          stationInfo.autoStart = true
+        }
       }
     }
     return stationInfo