X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FIdTagsCache.ts;h=5e0ab0b8f0778c2fbf02333c579657387a5c1d5f;hb=be1e907c6de583ad192c9566943252bbde19aa51;hp=ff5c49d9a6a2dba2fc2810b0e6a36fa54fbbb125;hpb=111aaf897c1c7b5c2f48678923a458f62406e3e2;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/IdTagsCache.ts b/src/charging-station/IdTagsCache.ts index ff5c49d9..5e0ab0b8 100644 --- a/src/charging-station/IdTagsCache.ts +++ b/src/charging-station/IdTagsCache.ts @@ -1,14 +1,21 @@ -import fs from 'node:fs'; +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 { ErrorUtils, FileUtils, Utils, logger } from '../utils'; - -type IdTagsCacheValueType = { +import { + handleFileException, + isNotEmptyString, + logPrefix, + logger, + secureRandom, + watchJsonFile, +} from '../utils'; + +interface IdTagsCacheValueType { idTags: string[]; - idTagsFileWatcher: fs.FSWatcher | undefined; -}; + idTagsFileWatcher: FSWatcher | undefined; +} export class IdTagsCache { private static instance: IdTagsCache | null = null; @@ -28,21 +35,21 @@ export class IdTagsCache { } /** - * Get one idtag from the cache given the distribution + * Gets one idtag from the cache given the distribution * Must be called after checking the cache is not an empty array * - * @param distribution - * @param chargingStation - * @param connectorId + * @param distribution - + * @param chargingStation - + * @param connectorId - * @returns */ 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); @@ -56,10 +63,10 @@ export class IdTagsCache { } /** - * Get all idtags from the cache + * Gets all idtags from the cache * Must be called after checking the cache is not an empty array * - * @param file + * @param file - * @returns */ public getIdTags(file: string): string[] | undefined { @@ -70,43 +77,43 @@ export class IdTagsCache { } public deleteIdTags(file: string): boolean { - return this.deleteIdTagsCache(file); + return this.deleteIdTagsCache(file) && this.deleteIdTagsCacheIndexes(file); } 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(Utils.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 { @@ -116,32 +123,32 @@ export class IdTagsCache { private setIdTagsCache(file: string, idTags: string[]) { return this.idTagsCaches.set(file, { idTags, - idTagsFileWatcher: FileUtils.watchJsonFile( + idTagsFileWatcher: watchJsonFile( file, FileType.Authorization, this.logPrefix(file), undefined, (event, filename) => { - if (Utils.isNotEmptyString(filename) && event === 'change') { + 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); } catch (error) { - ErrorUtils.handleFileException( + handleFileException( file, FileType.Authorization, error as NodeJS.ErrnoException, this.logPrefix(file), { throwError: false, - } + }, ); } } - } + }, ), }); } @@ -155,12 +162,14 @@ export class IdTagsCache { return this.idTagsCaches.delete(file); } - private deleteIdTagsCacheIndexes(file: string): void { + private deleteIdTagsCacheIndexes(file: string): boolean { + const deleted: boolean[] = []; for (const [key] of this.idTagsCachesAddressableIndexes) { if (key.startsWith(file)) { - this.idTagsCachesAddressableIndexes.delete(key); + deleted.push(this.idTagsCachesAddressableIndexes.delete(key)); } } + return !deleted.some((value) => value === false); } private getIdTagsCacheIndexesAddressableKey(prefix: string, uid: string): string { @@ -168,15 +177,15 @@ export class IdTagsCache { } private getIdTagsFromFile(file: string): string[] { - if (Utils.isNotEmptyString(file)) { + if (isNotEmptyString(file)) { try { - return JSON.parse(fs.readFileSync(file, 'utf8')) as string[]; + return JSON.parse(readFileSync(file, 'utf8')) as string[]; } catch (error) { - ErrorUtils.handleFileException( + handleFileException( file, FileType.Authorization, error as NodeJS.ErrnoException, - this.logPrefix(file) + this.logPrefix(file), ); } } @@ -184,6 +193,6 @@ export class IdTagsCache { } private logPrefix = (file: string): string => { - return Utils.logPrefix(` Id tags cache for id tags file '${file}' |`); + return logPrefix(` Id tags cache for id tags file '${file}' |`); }; }