feat: untangle add charging station op from start charging station op
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStation.ts
index 36cd0958fffe778f3cb68c0402be8b3285f58f4f..c8eaf5e44bc70e18844a37ac79390db7a8b845bc 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,8 @@ export class ChargingStation extends EventEmitter {
     })
 
     this.initialize()
+
+    this.stationInfo?.autoStart === true && this.start()
   }
 
   public get hasEvses (): boolean {
@@ -646,6 +652,10 @@ export class ChargingStation extends EventEmitter {
     }
   }
 
+  public add (): void {
+    this.emit(ChargingStationEvents.added)
+  }
+
   public start (): void {
     if (!this.started) {
       if (!this.starting) {
@@ -1129,6 +1139,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 +1199,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