X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FIdTagsCache.ts;h=5e0ab0b8f0778c2fbf02333c579657387a5c1d5f;hb=be1e907c6de583ad192c9566943252bbde19aa51;hp=0b3ba285dd080777ca60de657fe2060555553536;hpb=9bf0ef23c51160abc6866ad8d07eea85e308edb8;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/IdTagsCache.ts b/src/charging-station/IdTagsCache.ts index 0b3ba285..5e0ab0b8 100644 --- a/src/charging-station/IdTagsCache.ts +++ b/src/charging-station/IdTagsCache.ts @@ -1,7 +1,7 @@ import { type FSWatcher, readFileSync } from 'node:fs'; import type { ChargingStation } from './ChargingStation'; -import { ChargingStationUtils } from './ChargingStationUtils'; +import { getIdTagsFile } from './Helpers'; import { FileType, IdTagDistribution } from '../types'; import { handleFileException, @@ -12,10 +12,10 @@ import { watchJsonFile, } from '../utils'; -type IdTagsCacheValueType = { +interface IdTagsCacheValueType { idTags: string[]; idTagsFileWatcher: FSWatcher | undefined; -}; +} export class IdTagsCache { private static instance: IdTagsCache | null = null; @@ -46,10 +46,10 @@ export class IdTagsCache { public getIdTag( distribution: IdTagDistribution, chargingStation: ChargingStation, - connectorId: number + connectorId: number, ): string { const hashId = chargingStation.stationInfo.hashId; - const idTagsFile = ChargingStationUtils.getIdTagsFile(chargingStation.stationInfo); + const idTagsFile = getIdTagsFile(chargingStation.stationInfo)!; switch (distribution) { case IdTagDistribution.RANDOM: return this.getRandomIdTag(hashId, idTagsFile); @@ -81,39 +81,39 @@ export class IdTagsCache { } private getRandomIdTag(hashId: string, file: string): string { - const idTags = this.getIdTags(file); + const idTags = this.getIdTags(file)!; const addressableKey = this.getIdTagsCacheIndexesAddressableKey(file, hashId); this.idTagsCachesAddressableIndexes.set( addressableKey, - Math.floor(secureRandom() * idTags.length) + Math.floor(secureRandom() * idTags.length), ); - return idTags[this.idTagsCachesAddressableIndexes.get(addressableKey)]; + return idTags[this.idTagsCachesAddressableIndexes.get(addressableKey)!]; } private getRoundRobinIdTag(hashId: string, file: string): string { - const idTags = this.getIdTags(file); + const idTags = this.getIdTags(file)!; const addressableKey = this.getIdTagsCacheIndexesAddressableKey(file, hashId); const idTagIndex = this.idTagsCachesAddressableIndexes.get(addressableKey) ?? 0; const idTag = idTags[idTagIndex]; this.idTagsCachesAddressableIndexes.set( addressableKey, - idTagIndex === idTags.length - 1 ? 0 : idTagIndex + 1 + idTagIndex === idTags.length - 1 ? 0 : idTagIndex + 1, ); return idTag; } private getConnectorAffinityIdTag(chargingStation: ChargingStation, connectorId: number): string { - const file = ChargingStationUtils.getIdTagsFile(chargingStation.stationInfo); - const idTags = this.getIdTags(file); + const file = getIdTagsFile(chargingStation.stationInfo)!; + const idTags = this.getIdTags(file)!; const addressableKey = this.getIdTagsCacheIndexesAddressableKey( file, - chargingStation.stationInfo.hashId + chargingStation.stationInfo.hashId, ); this.idTagsCachesAddressableIndexes.set( addressableKey, - (chargingStation.index - 1 + (connectorId - 1)) % idTags.length + (chargingStation.index - 1 + (connectorId - 1)) % idTags.length, ); - return idTags[this.idTagsCachesAddressableIndexes.get(addressableKey)]; + return idTags[this.idTagsCachesAddressableIndexes.get(addressableKey)!]; } private hasIdTagsCache(file: string): boolean { @@ -132,7 +132,7 @@ export class IdTagsCache { if (isNotEmptyString(filename) && event === 'change') { try { logger.debug( - `${this.logPrefix(file)} ${FileType.Authorization} file have changed, reload` + `${this.logPrefix(file)} ${FileType.Authorization} file have changed, reload`, ); this.deleteIdTagsCache(file); this.deleteIdTagsCacheIndexes(file); @@ -144,11 +144,11 @@ export class IdTagsCache { this.logPrefix(file), { throwError: false, - } + }, ); } } - } + }, ), }); } @@ -163,7 +163,7 @@ export class IdTagsCache { } private deleteIdTagsCacheIndexes(file: string): boolean { - let deleted: boolean[]; + const deleted: boolean[] = []; for (const [key] of this.idTagsCachesAddressableIndexes) { if (key.startsWith(file)) { deleted.push(this.idTagsCachesAddressableIndexes.delete(key)); @@ -185,7 +185,7 @@ export class IdTagsCache { file, FileType.Authorization, error as NodeJS.ErrnoException, - this.logPrefix(file) + this.logPrefix(file), ); } }