X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FIdTagsCache.ts;h=bf0838e91890614f9e0fdbf48af0b541aba8dcdd;hb=b2b606263e2676354259164d532ff9aa91ccdf87;hp=6e8592cb71aecfeb7f3160212761905adde2b9c8;hpb=26a17d9368bd65430bea2a04d23df8ebe84aeac2;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/IdTagsCache.ts b/src/charging-station/IdTagsCache.ts index 6e8592cb..bf0838e9 100644 --- a/src/charging-station/IdTagsCache.ts +++ b/src/charging-station/IdTagsCache.ts @@ -1,13 +1,20 @@ -import fs from 'node:fs'; +import { type FSWatcher, readFileSync } from 'node:fs'; import type { ChargingStation } from './ChargingStation'; -import { ChargingStationUtils } from './ChargingStationUtils'; +import { getIdTagsFile } from './ChargingStationUtils'; import { FileType, IdTagDistribution } from '../types'; -import { Utils, handleFileException, logger, watchJsonFile } from '../utils'; +import { + handleFileException, + isNotEmptyString, + logPrefix, + logger, + secureRandom, + watchJsonFile, +} from '../utils'; type IdTagsCacheValueType = { idTags: string[]; - idTagsFileWatcher: fs.FSWatcher | undefined; + idTagsFileWatcher: FSWatcher | undefined; }; export class IdTagsCache { @@ -28,7 +35,7 @@ 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 - @@ -42,7 +49,7 @@ export class IdTagsCache { 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,7 +63,7 @@ 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 - @@ -78,7 +85,7 @@ export class IdTagsCache { 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)]; } @@ -96,7 +103,7 @@ export class IdTagsCache { } private getConnectorAffinityIdTag(chargingStation: ChargingStation, connectorId: number): string { - const file = ChargingStationUtils.getIdTagsFile(chargingStation.stationInfo); + const file = getIdTagsFile(chargingStation.stationInfo); const idTags = this.getIdTags(file); const addressableKey = this.getIdTagsCacheIndexesAddressableKey( file, @@ -122,7 +129,7 @@ export class IdTagsCache { 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` @@ -170,9 +177,9 @@ 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) { handleFileException( file, @@ -186,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}' |`); }; }