fix: find the first free indexes at adding charging stations
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 11 Feb 2024 21:29:37 +0000 (22:29 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 11 Feb 2024 21:29:37 +0000 (22:29 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/Bootstrap.ts
src/charging-station/ChargingStation.ts
src/types/ChargingStationInfo.ts

index 5356a15569fbce4db81a738dc7e24274d93f2118..9e24c8b845e258351e1087cd36fab590a572fa98 100644 (file)
@@ -41,8 +41,7 @@ import {
   isAsyncFunction,
   isNotEmptyArray,
   logPrefix,
-  logger,
-  max
+  logger
 } from '../utils/index.js'
 import { type WorkerAbstract, WorkerFactory } from '../worker/index.js'
 
@@ -60,7 +59,7 @@ interface TemplateChargingStations {
   configured: number
   added: number
   started: number
-  lastIndex: number
+  indexes: Set<number>
 }
 
 export class Bootstrap extends EventEmitter {
@@ -118,7 +117,16 @@ export class Bootstrap extends EventEmitter {
   }
 
   public getLastIndex (templateName: string): number {
-    return this.chargingStationsByTemplate.get(templateName)?.lastIndex ?? 0
+    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+    const indexes = [...this.chargingStationsByTemplate.get(templateName)!.indexes]
+      .concat(0)
+      .sort((a, b) => a - b)
+    for (let i = 0; i < indexes.length - 1; i++) {
+      if (indexes[i + 1] - indexes[i] !== 1) {
+        return indexes[i]
+      }
+    }
+    return indexes[indexes.length - 1]
   }
 
   public getPerformanceStatistics (): IterableIterator<Statistics> | undefined {
@@ -392,8 +400,6 @@ 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
@@ -407,6 +413,9 @@ export class Bootstrap extends EventEmitter {
     this.uiServer?.chargingStations.delete(data.stationInfo.hashId)
     // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
     --this.chargingStationsByTemplate.get(data.stationInfo.templateName)!.added
+    this.chargingStationsByTemplate
+      .get(data.stationInfo.templateName)
+      ?.indexes.delete(data.stationInfo.templateIndex)
     logger.info(
       `${this.logPrefix()} ${moduleName}.workerEventDeleted: Charging station ${
         data.stationInfo.chargingStationId
@@ -472,7 +481,7 @@ export class Bootstrap extends EventEmitter {
             configured: stationTemplateUrl.numberOfStations,
             added: 0,
             started: 0,
-            lastIndex: 0
+            indexes: new Set<number>()
           })
           this.uiServer?.chargingStationTemplates.add(templateName)
         }
@@ -521,11 +530,10 @@ export class Bootstrap extends EventEmitter {
       ),
       options
     })
+    const templateName = parse(stationTemplateFile).name
     // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-    this.chargingStationsByTemplate.get(parse(stationTemplateFile).name)!.lastIndex = max(
-      index,
-      this.chargingStationsByTemplate.get(parse(stationTemplateFile).name)?.lastIndex ?? -Infinity
-    )
+    ++this.chargingStationsByTemplate.get(templateName)!.added
+    this.chargingStationsByTemplate.get(templateName)?.indexes.add(index)
   }
 
   private gracefulShutdown (): void {
index b6e381e1e400f96796bf53068871a69a46349304..38d40dc1323b313280010ade231f7cfbc5eee8cf 100644 (file)
@@ -1163,6 +1163,7 @@ export class ChargingStation extends EventEmitter {
     }
     const stationInfo = stationTemplateToStationInfo(stationTemplate)
     stationInfo.hashId = getHashId(this.index, stationTemplate)
+    stationInfo.templateIndex = this.index
     stationInfo.templateName = parse(this.templateFile).name
     stationInfo.chargingStationId = getChargingStationId(this.index, stationTemplate)
     createSerialNumber(stationTemplate, stationInfo)
@@ -1217,6 +1218,10 @@ export class ChargingStation extends EventEmitter {
       if (stationInfo != null) {
         delete stationInfo.infoHash
         // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
+        if (stationInfo.templateIndex == null) {
+          stationInfo.templateIndex = this.index
+        }
+        // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
         if (stationInfo.templateName == null) {
           stationInfo.templateName = parse(this.templateFile).name
         }
index d4e4f8db675c606a613b0488d2b5dede109ae8ce..f4447e527ff79de2a70d13ca2a4a2e536b5f08cb 100644 (file)
@@ -14,6 +14,7 @@ ChargingStationTemplate,
 | 'meterSerialNumberPrefix'
 > & {
   hashId: string
+  templateIndex: number
   templateName: string
   /** @deprecated Use hashId instead */
   infoHash?: string