X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FIdTagsCache.ts;h=d1ad32d2bbffe63aa4d091fe11fecce1af0db165;hb=5c24bae9ed9d25877c4fa9572bd6e87d9e9547ec;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..d1ad32d2 100644 --- a/src/charging-station/IdTagsCache.ts +++ b/src/charging-station/IdTagsCache.ts @@ -1,34 +1,41 @@ -import fs from 'node:fs'; - -import type { ChargingStation } from './ChargingStation'; -import { ChargingStationUtils } from './ChargingStationUtils'; -import { FileType, IdTagDistribution } from '../types'; -import { Utils, handleFileException, logger, watchJsonFile } from '../utils'; - -type IdTagsCacheValueType = { - idTags: string[]; - idTagsFileWatcher: fs.FSWatcher | undefined; -}; +import { type FSWatcher, readFileSync } from 'node:fs' + +import type { ChargingStation } from './ChargingStation.js' +import { getIdTagsFile } from './Helpers.js' +import { FileType, IdTagDistribution } from '../types/index.js' +import { + handleFileException, + isNotEmptyString, + logPrefix, + logger, + secureRandom, + watchJsonFile +} from '../utils/index.js' + +interface IdTagsCacheValueType { + idTags: string[] + idTagsFileWatcher: FSWatcher | undefined +} export class IdTagsCache { - private static instance: IdTagsCache | null = null; - private readonly idTagsCaches: Map; - private readonly idTagsCachesAddressableIndexes: Map; + private static instance: IdTagsCache | null = null + private readonly idTagsCaches: Map + private readonly idTagsCachesAddressableIndexes: Map - private constructor() { - this.idTagsCaches = new Map(); - this.idTagsCachesAddressableIndexes = new Map(); + private constructor () { + this.idTagsCaches = new Map() + this.idTagsCachesAddressableIndexes = new Map() } - public static getInstance(): IdTagsCache { + public static getInstance (): IdTagsCache { if (IdTagsCache.instance === null) { - IdTagsCache.instance = new IdTagsCache(); + IdTagsCache.instance = new IdTagsCache() } - return IdTagsCache.instance; + return IdTagsCache.instance } /** - * 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 - @@ -36,84 +43,93 @@ export class IdTagsCache { * @param connectorId - * @returns */ - public getIdTag( + public getIdTag ( distribution: IdTagDistribution, chargingStation: ChargingStation, connectorId: number ): string { - const hashId = chargingStation.stationInfo.hashId; - const idTagsFile = ChargingStationUtils.getIdTagsFile(chargingStation.stationInfo); + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const hashId = chargingStation.stationInfo!.hashId + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const idTagsFile = getIdTagsFile(chargingStation.stationInfo!)! switch (distribution) { case IdTagDistribution.RANDOM: - return this.getRandomIdTag(hashId, idTagsFile); + return this.getRandomIdTag(hashId, idTagsFile) case IdTagDistribution.ROUND_ROBIN: - return this.getRoundRobinIdTag(hashId, idTagsFile); + return this.getRoundRobinIdTag(hashId, idTagsFile) case IdTagDistribution.CONNECTOR_AFFINITY: - return this.getConnectorAffinityIdTag(chargingStation, connectorId); + return this.getConnectorAffinityIdTag(chargingStation, connectorId) default: - return this.getRoundRobinIdTag(hashId, idTagsFile); + return this.getRoundRobinIdTag(hashId, idTagsFile) } } /** - * 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 - * @returns */ - public getIdTags(file: string): string[] | undefined { - if (this.hasIdTagsCache(file) === false) { - this.setIdTagsCache(file, this.getIdTagsFromFile(file)); + public getIdTags (file: string): string[] | undefined { + if (!this.hasIdTagsCache(file)) { + this.setIdTagsCache(file, this.getIdTagsFromFile(file)) } - return this.getIdTagsCache(file); + return this.getIdTagsCache(file) } - public deleteIdTags(file: string): boolean { - return this.deleteIdTagsCache(file) && this.deleteIdTagsCacheIndexes(file); + public deleteIdTags (file: string): boolean { + return this.deleteIdTagsCache(file) && this.deleteIdTagsCacheIndexes(file) } - private getRandomIdTag(hashId: string, file: string): string { - const idTags = this.getIdTags(file); - const addressableKey = this.getIdTagsCacheIndexesAddressableKey(file, hashId); + private getRandomIdTag (hashId: string, file: string): string { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const idTags = this.getIdTags(file)! + const addressableKey = this.getIdTagsCacheIndexesAddressableKey(file, hashId) this.idTagsCachesAddressableIndexes.set( addressableKey, - Math.floor(Utils.secureRandom() * idTags.length) - ); - return idTags[this.idTagsCachesAddressableIndexes.get(addressableKey)]; + Math.floor(secureRandom() * idTags.length) + ) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + return idTags[this.idTagsCachesAddressableIndexes.get(addressableKey)!] } - private getRoundRobinIdTag(hashId: string, file: string): string { - const idTags = this.getIdTags(file); - const addressableKey = this.getIdTagsCacheIndexesAddressableKey(file, hashId); - const idTagIndex = this.idTagsCachesAddressableIndexes.get(addressableKey) ?? 0; - const idTag = idTags[idTagIndex]; + private getRoundRobinIdTag (hashId: string, file: string): string { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + 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 - ); - return idTag; + ) + return idTag } - private getConnectorAffinityIdTag(chargingStation: ChargingStation, connectorId: number): string { - const file = ChargingStationUtils.getIdTagsFile(chargingStation.stationInfo); - const idTags = this.getIdTags(file); + private getConnectorAffinityIdTag (chargingStation: ChargingStation, connectorId: number): string { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const file = getIdTagsFile(chargingStation.stationInfo!)! + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const idTags = this.getIdTags(file)! const addressableKey = this.getIdTagsCacheIndexesAddressableKey( file, - chargingStation.stationInfo.hashId - ); + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + chargingStation.stationInfo!.hashId + ) this.idTagsCachesAddressableIndexes.set( addressableKey, (chargingStation.index - 1 + (connectorId - 1)) % idTags.length - ); - return idTags[this.idTagsCachesAddressableIndexes.get(addressableKey)]; + ) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + return idTags[this.idTagsCachesAddressableIndexes.get(addressableKey)!] } - private hasIdTagsCache(file: string): boolean { - return this.idTagsCaches.has(file); + private hasIdTagsCache (file: string): boolean { + return this.idTagsCaches.has(file) } - private setIdTagsCache(file: string, idTags: string[]) { + private setIdTagsCache (file: string, idTags: string[]): Map { return this.idTagsCaches.set(file, { idTags, idTagsFileWatcher: watchJsonFile( @@ -122,13 +138,13 @@ 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` - ); - this.deleteIdTagsCache(file); - this.deleteIdTagsCacheIndexes(file); + ) + this.deleteIdTagsCache(file) + this.deleteIdTagsCacheIndexes(file) } catch (error) { handleFileException( file, @@ -136,56 +152,56 @@ export class IdTagsCache { error as NodeJS.ErrnoException, this.logPrefix(file), { - throwError: false, + throwError: false } - ); + ) } } } - ), - }); + ) + }) } - private getIdTagsCache(file: string): string[] | undefined { - return this.idTagsCaches.get(file)?.idTags; + private getIdTagsCache (file: string): string[] | undefined { + return this.idTagsCaches.get(file)?.idTags } - private deleteIdTagsCache(file: string): boolean { - this.idTagsCaches.get(file)?.idTagsFileWatcher?.close(); - return this.idTagsCaches.delete(file); + private deleteIdTagsCache (file: string): boolean { + this.idTagsCaches.get(file)?.idTagsFileWatcher?.close() + return this.idTagsCaches.delete(file) } - private deleteIdTagsCacheIndexes(file: string): boolean { - let deleted: boolean[]; + private deleteIdTagsCacheIndexes (file: string): boolean { + const deleted: boolean[] = [] for (const [key] of this.idTagsCachesAddressableIndexes) { if (key.startsWith(file)) { - deleted.push(this.idTagsCachesAddressableIndexes.delete(key)); + deleted.push(this.idTagsCachesAddressableIndexes.delete(key)) } } - return !deleted.some((value) => value === false); + return !deleted.some(value => !value) } - private getIdTagsCacheIndexesAddressableKey(prefix: string, uid: string): string { - return `${prefix}${uid}`; + private getIdTagsCacheIndexesAddressableKey (prefix: string, uid: string): string { + return `${prefix}${uid}` } - private getIdTagsFromFile(file: string): string[] { - if (Utils.isNotEmptyString(file)) { + private getIdTagsFromFile (file: string): string[] { + 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, FileType.Authorization, error as NodeJS.ErrnoException, this.logPrefix(file) - ); + ) } } - return []; + return [] } - private logPrefix = (file: string): string => { - return Utils.logPrefix(` Id tags cache for id tags file '${file}' |`); - }; + private readonly logPrefix = (file: string): string => { + return logPrefix(` Id tags cache for id tags file '${file}' |`) + } }