X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FIdTagsCache.ts;h=bf0838e91890614f9e0fdbf48af0b541aba8dcdd;hb=b2b606263e2676354259164d532ff9aa91ccdf87;hp=f19f66f31948b270de58a115fee550a4a6fcbdcb;hpb=7af183e79b63c60d72449a4eecb7e996927ec156;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/IdTagsCache.ts b/src/charging-station/IdTagsCache.ts index f19f66f3..bf0838e9 100644 --- a/src/charging-station/IdTagsCache.ts +++ b/src/charging-station/IdTagsCache.ts @@ -1,12 +1,20 @@ -import fs from 'node:fs'; +import { type FSWatcher, readFileSync } from 'node:fs'; -import { type ChargingStation, ChargingStationUtils } from './internal'; +import type { ChargingStation } from './ChargingStation'; +import { getIdTagsFile } from './ChargingStationUtils'; import { FileType, IdTagDistribution } from '../types'; -import { FileUtils, Utils, logger } 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 { @@ -26,13 +34,22 @@ export class IdTagsCache { return IdTagsCache.instance; } + /** + * 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 - + * @returns + */ public getIdTag( distribution: IdTagDistribution, chargingStation: ChargingStation, 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); @@ -45,6 +62,13 @@ export class IdTagsCache { } } + /** + * Gets all idtags from the cache + * Must be called after checking the cache is not an empty array + * + * @param file - + * @returns + */ public getIdTags(file: string): string[] | undefined { if (this.hasIdTagsCache(file) === false) { this.setIdTagsCache(file, this.getIdTagsFromFile(file)); @@ -53,22 +77,22 @@ 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 addressableKey = this.getIdTagsCacheAddressableKey(file, hashId); + 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)]; } private getRoundRobinIdTag(hashId: string, file: string): string { const idTags = this.getIdTags(file); - const addressableKey = this.getIdTagsCacheAddressableKey(file, hashId); + const addressableKey = this.getIdTagsCacheIndexesAddressableKey(file, hashId); const idTagIndex = this.idTagsCachesAddressableIndexes.get(addressableKey) ?? 0; const idTag = idTags[idTagIndex]; this.idTagsCachesAddressableIndexes.set( @@ -79,10 +103,12 @@ 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 hashId = chargingStation.stationInfo.hashId; - const addressableKey = this.getIdTagsCacheAddressableKey(file, hashId); + const addressableKey = this.getIdTagsCacheIndexesAddressableKey( + file, + chargingStation.stationInfo.hashId + ); this.idTagsCachesAddressableIndexes.set( addressableKey, (chargingStation.index - 1 + (connectorId - 1)) % idTags.length @@ -97,13 +123,13 @@ 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` @@ -111,7 +137,7 @@ export class IdTagsCache { this.deleteIdTagsCache(file); this.deleteIdTagsCacheIndexes(file); } catch (error) { - FileUtils.handleFileException( + handleFileException( file, FileType.Authorization, error as NodeJS.ErrnoException, @@ -136,39 +162,37 @@ export class IdTagsCache { return this.idTagsCaches.delete(file); } - private deleteIdTagsCacheIndexes(file: string): void { + private deleteIdTagsCacheIndexes(file: string): boolean { + let 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 getIdTagsCacheAddressableKey(prefix: string, uid: string): string { + private getIdTagsCacheIndexesAddressableKey(prefix: string, uid: string): string { return `${prefix}${uid}`; } private getIdTagsFromFile(file: string): string[] { - let idTags: string[] = []; - if (file) { + if (isNotEmptyString(file)) { try { - // Load id tags file - idTags = JSON.parse(fs.readFileSync(file, 'utf8')) as string[]; + return JSON.parse(readFileSync(file, 'utf8')) as string[]; } catch (error) { - FileUtils.handleFileException( + handleFileException( file, FileType.Authorization, error as NodeJS.ErrnoException, this.logPrefix(file) ); } - } else { - logger.info(`${this.logPrefix(file)} No id tags file given`); } - return idTags; + return []; } 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}' |`); }; }