Skip to content

Commit

Permalink
fix: find the first free indexes at adding charging stations
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <[email protected]>
  • Loading branch information
Jérôme Benoit committed Feb 11, 2024
1 parent 36c1166 commit e375708
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/charging-station/Bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ import {
isAsyncFunction,
isNotEmptyArray,
logPrefix,
logger,
max
logger
} from '../utils/index.js'
import { type WorkerAbstract, WorkerFactory } from '../worker/index.js'

Expand All @@ -60,7 +59,7 @@ interface TemplateChargingStations {
configured: number
added: number
started: number
lastIndex: number
indexes: Set<number>
}

export class Bootstrap extends EventEmitter {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions src/charging-station/ChargingStation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions src/types/ChargingStationInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ChargingStationTemplate,
| 'meterSerialNumberPrefix'
> & {
hashId: string
templateIndex: number
templateName: string
/** @deprecated Use hashId instead */
infoHash?: string
Expand Down

0 comments on commit e375708

Please sign in to comment.