From e375708d902de515fb56e00c0fefd377cbd0469c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 11 Feb 2024 22:29:37 +0100 Subject: [PATCH] fix: find the first free indexes at adding charging stations MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/Bootstrap.ts | 30 ++++++++++++++++--------- src/charging-station/ChargingStation.ts | 5 +++++ src/types/ChargingStationInfo.ts | 1 + 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 5356a155..9e24c8b8 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 b6e381e1..38d40dc1 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 d4e4f8db..f4447e52 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 -- 2.34.1