diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 5356a1556..9e24c8b84 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -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 } 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 | 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() }) 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 { diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index b6e381e1e..38d40dc13 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -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 } diff --git a/src/types/ChargingStationInfo.ts b/src/types/ChargingStationInfo.ts index d4e4f8db6..f4447e527 100644 --- a/src/types/ChargingStationInfo.ts +++ b/src/types/ChargingStationInfo.ts @@ -14,6 +14,7 @@ ChargingStationTemplate, | 'meterSerialNumberPrefix' > & { hashId: string + templateIndex: number templateName: string /** @deprecated Use hashId instead */ infoHash?: string