docs: update UI protocol requests collections
[e-mobility-charging-stations-simulator.git] / src / charging-station / Bootstrap.ts
index bd740997ff77a41e8a933909630ba45a23d019cc..acd8e73cb23ea1b7cf0b41e5e6cd3436cc4ee0f9 100644 (file)
@@ -18,7 +18,9 @@ import { BaseError } from '../exception/index.js'
 import { type Storage, StorageFactory } from '../performance/index.js'
 import {
   type ChargingStationData,
+  type ChargingStationOptions,
   type ChargingStationWorkerData,
+  type ChargingStationWorkerEventError,
   type ChargingStationWorkerMessage,
   type ChargingStationWorkerMessageData,
   ChargingStationWorkerMessageEvents,
@@ -56,6 +58,7 @@ enum exitCodes {
 
 interface TemplateChargingStations {
   configured: number
+  added: number
   started: number
   lastIndex: number
 }
@@ -118,6 +121,17 @@ export class Bootstrap extends EventEmitter {
     return this.chargingStationsByTemplate.get(templateName)?.lastIndex ?? 0
   }
 
+  public getPerformanceStatistics (): IterableIterator<Statistics> | undefined {
+    return this.storage?.getPerformanceStatistics()
+  }
+
+  private get numberOfAddedChargingStations (): number {
+    return [...this.chargingStationsByTemplate.values()].reduce(
+      (accumulator, value) => accumulator + value.added,
+      0
+    )
+  }
+
   private get numberOfStartedChargingStations (): number {
     return [...this.chargingStationsByTemplate.values()].reduce(
       (accumulator, value) => accumulator + value.started,
@@ -129,6 +143,7 @@ export class Bootstrap extends EventEmitter {
     if (!this.started) {
       if (!this.starting) {
         this.starting = true
+        this.on(ChargingStationWorkerMessageEvents.added, this.workerEventAdded)
         this.on(ChargingStationWorkerMessageEvents.started, this.workerEventStarted)
         this.on(ChargingStationWorkerMessageEvents.stopped, this.workerEventStopped)
         this.on(ChargingStationWorkerMessageEvents.updated, this.workerEventUpdated)
@@ -136,6 +151,15 @@ export class Bootstrap extends EventEmitter {
           ChargingStationWorkerMessageEvents.performanceStatistics,
           this.workerEventPerformanceStatistics
         )
+        this.on(
+          ChargingStationWorkerMessageEvents.workerElementError,
+          (eventError: ChargingStationWorkerEventError) => {
+            logger.error(
+              `${this.logPrefix()} ${moduleName}.start: Error occurred while handling '${eventError.event}' event on worker:`,
+              eventError
+            )
+          }
+        )
         this.initializeCounters()
         const workerConfiguration = Configuration.getConfigurationSection<WorkerConfiguration>(
           ConfigurationSection.worker
@@ -243,7 +267,7 @@ export class Bootstrap extends EventEmitter {
   private async restart (): Promise<void> {
     await this.stop()
     Configuration.getConfigurationSection<UIServerConfiguration>(ConfigurationSection.uiServer)
-      .enabled === false && this.uiServer?.stop()
+      .enabled !== true && this.uiServer?.stop()
     this.initializedCounters = false
     await this.start()
   }
@@ -323,29 +347,26 @@ export class Bootstrap extends EventEmitter {
     // )
     try {
       switch (msg.event) {
+        case ChargingStationWorkerMessageEvents.added:
+          this.emit(ChargingStationWorkerMessageEvents.added, msg.data)
+          break
         case ChargingStationWorkerMessageEvents.started:
-          this.emit(ChargingStationWorkerMessageEvents.started, msg.data as ChargingStationData)
+          this.emit(ChargingStationWorkerMessageEvents.started, msg.data)
           break
         case ChargingStationWorkerMessageEvents.stopped:
-          this.emit(ChargingStationWorkerMessageEvents.stopped, msg.data as ChargingStationData)
+          this.emit(ChargingStationWorkerMessageEvents.stopped, msg.data)
           break
         case ChargingStationWorkerMessageEvents.updated:
-          this.emit(ChargingStationWorkerMessageEvents.updated, msg.data as ChargingStationData)
+          this.emit(ChargingStationWorkerMessageEvents.updated, msg.data)
           break
         case ChargingStationWorkerMessageEvents.performanceStatistics:
-          this.emit(
-            ChargingStationWorkerMessageEvents.performanceStatistics,
-            msg.data as Statistics
-          )
+          this.emit(ChargingStationWorkerMessageEvents.performanceStatistics, msg.data)
           break
-        case ChargingStationWorkerMessageEvents.startWorkerElementError:
-          logger.error(
-            `${this.logPrefix()} ${moduleName}.messageHandler: Error occurred while starting worker element:`,
-            msg.data
-          )
-          this.emit(ChargingStationWorkerMessageEvents.startWorkerElementError, msg.data)
+        case ChargingStationWorkerMessageEvents.addedWorkerElement:
+          this.emit(ChargingStationWorkerMessageEvents.addWorkerElement, msg.data)
           break
-        case ChargingStationWorkerMessageEvents.startedWorkerElement:
+        case ChargingStationWorkerMessageEvents.workerElementError:
+          this.emit(ChargingStationWorkerMessageEvents.workerElementError, msg.data)
           break
         default:
           throw new BaseError(
@@ -364,6 +385,19 @@ export class Bootstrap extends EventEmitter {
     }
   }
 
+  private readonly workerEventAdded = (data: ChargingStationData): void => {
+    this.uiServer?.chargingStations.set(data.stationInfo.hashId, data)
+    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+    ++this.chargingStationsByTemplate.get(data.stationInfo.templateName)!.added
+    logger.info(
+      `${this.logPrefix()} ${moduleName}.workerEventAdded: Charging station ${
+        data.stationInfo.chargingStationId
+      } (hashId: ${data.stationInfo.hashId}) added (${
+        this.numberOfAddedChargingStations
+      } added from ${this.numberOfConfiguredChargingStations} configured charging station(s))`
+    )
+  }
+
   private readonly workerEventStarted = (data: ChargingStationData): void => {
     this.uiServer?.chargingStations.set(data.stationInfo.hashId, data)
     // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -373,7 +407,7 @@ export class Bootstrap extends EventEmitter {
         data.stationInfo.chargingStationId
       } (hashId: ${data.stationInfo.hashId}) started (${
         this.numberOfStartedChargingStations
-      } started from ${this.numberOfConfiguredChargingStations} configured charging station(s))`
+      } started from ${this.numberOfAddedChargingStations} added charging station(s))`
     )
   }
 
@@ -386,7 +420,7 @@ export class Bootstrap extends EventEmitter {
         data.stationInfo.chargingStationId
       } (hashId: ${data.stationInfo.hashId}) stopped (${
         this.numberOfStartedChargingStations
-      } started from ${this.numberOfConfiguredChargingStations} configured charging station(s))`
+      } started from ${this.numberOfAddedChargingStations} added charging station(s))`
     )
   }
 
@@ -418,6 +452,7 @@ export class Bootstrap extends EventEmitter {
           const templateName = parse(stationTemplateUrl.file).name
           this.chargingStationsByTemplate.set(templateName, {
             configured: stationTemplateUrl.numberOfStations,
+            added: 0,
             started: 0,
             lastIndex: 0
           })
@@ -440,7 +475,7 @@ export class Bootstrap extends EventEmitter {
       if (
         this.numberOfConfiguredChargingStations === 0 &&
         Configuration.getConfigurationSection<UIServerConfiguration>(ConfigurationSection.uiServer)
-          .enabled === true
+          .enabled !== true
       ) {
         console.error(
           chalk.red(
@@ -453,7 +488,11 @@ export class Bootstrap extends EventEmitter {
     }
   }
 
-  public async addChargingStation (index: number, stationTemplateFile: string): Promise<void> {
+  public async addChargingStation (
+    index: number,
+    stationTemplateFile: string,
+    options?: ChargingStationOptions
+  ): Promise<void> {
     await this.workerImplementation?.addElement({
       index,
       templateFile: join(
@@ -461,7 +500,8 @@ export class Bootstrap extends EventEmitter {
         'assets',
         'station-templates',
         stationTemplateFile
-      )
+      ),
+      options
     })
     // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
     this.chargingStationsByTemplate.get(parse(stationTemplateFile).name)!.lastIndex = max(
@@ -475,7 +515,6 @@ export class Bootstrap extends EventEmitter {
       .then(() => {
         console.info(chalk.green('Graceful shutdown'))
         this.uiServer?.stop()
-        // stop() asks for charging stations to stop by default
         this.waitChargingStationsStopped()
           .then(() => {
             exit(exitCodes.succeeded)