X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FIdTagsCache.ts;h=5e0ab0b8f0778c2fbf02333c579657387a5c1d5f;hb=ec54600d41e798a66e61e6311ee07cccfb1aea2b;hp=26ef71318af3bf9cb4834c9b4e07e29faa70e1ba;hpb=5edd8ba0f8978cfb3ca9d80f299d9748c6c5970e;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/IdTagsCache.ts b/src/charging-station/IdTagsCache.ts index 26ef7131..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 { getIdTagsFile } 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; @@ -49,7 +49,7 @@ export class IdTagsCache { connectorId: number, ): string { const hashId = chargingStation.stationInfo.hashId; - const idTagsFile = getIdTagsFile(chargingStation.stationInfo); + const idTagsFile = getIdTagsFile(chargingStation.stationInfo)!; switch (distribution) { case IdTagDistribution.RANDOM: return this.getRandomIdTag(hashId, idTagsFile); @@ -81,17 +81,17 @@ 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), ); - 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]; @@ -103,8 +103,8 @@ export class IdTagsCache { } private getConnectorAffinityIdTag(chargingStation: ChargingStation, connectorId: number): string { - const file = 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, @@ -113,7 +113,7 @@ export class IdTagsCache { addressableKey, (chargingStation.index - 1 + (connectorId - 1)) % idTags.length, ); - return idTags[this.idTagsCachesAddressableIndexes.get(addressableKey)]; + return idTags[this.idTagsCachesAddressableIndexes.get(addressableKey)!]; } private hasIdTagsCache(file: string): boolean { @@ -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));