X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FIdTagsCache.ts;h=ff5c49d9a6a2dba2fc2810b0e6a36fa54fbbb125;hb=86b46b496311fcb6ee26c0d639b6f3c642042c2b;hp=48ecb552a90b7c23f0f474952cdb33b87cd49dbb;hpb=e302df1d4573ef07df7c44a210a34e429f571814;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/IdTagsCache.ts b/src/charging-station/IdTagsCache.ts index 48ecb552..ff5c49d9 100644 --- a/src/charging-station/IdTagsCache.ts +++ b/src/charging-station/IdTagsCache.ts @@ -1,8 +1,9 @@ import fs from 'node:fs'; -import { type ChargingStation, ChargingStationUtils } from './internal'; +import type { ChargingStation } from './ChargingStation'; +import { ChargingStationUtils } from './ChargingStationUtils'; import { FileType, IdTagDistribution } from '../types'; -import { FileUtils, Utils, logger } from '../utils'; +import { ErrorUtils, FileUtils, Utils, logger } from '../utils'; type IdTagsCacheValueType = { idTags: string[]; @@ -26,6 +27,15 @@ export class IdTagsCache { return IdTagsCache.instance; } + /** + * Get 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, @@ -45,6 +55,13 @@ export class IdTagsCache { } } + /** + * Get 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)); @@ -58,7 +75,7 @@ export class IdTagsCache { private getRandomIdTag(hashId: string, file: string): string { const idTags = this.getIdTags(file); - const addressableKey = file + hashId; + const addressableKey = this.getIdTagsCacheIndexesAddressableKey(file, hashId); this.idTagsCachesAddressableIndexes.set( addressableKey, Math.floor(Utils.secureRandom() * idTags.length) @@ -68,7 +85,7 @@ export class IdTagsCache { private getRoundRobinIdTag(hashId: string, file: string): string { const idTags = this.getIdTags(file); - const addressableKey = file + hashId; + const addressableKey = this.getIdTagsCacheIndexesAddressableKey(file, hashId); const idTagIndex = this.idTagsCachesAddressableIndexes.get(addressableKey) ?? 0; const idTag = idTags[idTagIndex]; this.idTagsCachesAddressableIndexes.set( @@ -81,8 +98,10 @@ export class IdTagsCache { private getConnectorAffinityIdTag(chargingStation: ChargingStation, connectorId: number): string { const file = ChargingStationUtils.getIdTagsFile(chargingStation.stationInfo); const idTags = this.getIdTags(file); - const hashId = chargingStation.stationInfo.hashId; - const addressableKey = file + hashId; + const addressableKey = this.getIdTagsCacheIndexesAddressableKey( + file, + chargingStation.stationInfo.hashId + ); this.idTagsCachesAddressableIndexes.set( addressableKey, (chargingStation.index - 1 + (connectorId - 1)) % idTags.length @@ -111,7 +130,7 @@ export class IdTagsCache { this.deleteIdTagsCache(file); this.deleteIdTagsCacheIndexes(file); } catch (error) { - FileUtils.handleFileException( + ErrorUtils.handleFileException( file, FileType.Authorization, error as NodeJS.ErrnoException, @@ -144,24 +163,24 @@ export class IdTagsCache { } } + private getIdTagsCacheIndexesAddressableKey(prefix: string, uid: string): string { + return `${prefix}${uid}`; + } + private getIdTagsFromFile(file: string): string[] { - let idTags: string[] = []; - if (file) { + if (Utils.isNotEmptyString(file)) { try { - // Load id tags file - idTags = JSON.parse(fs.readFileSync(file, 'utf8')) as string[]; + return JSON.parse(fs.readFileSync(file, 'utf8')) as string[]; } catch (error) { - FileUtils.handleFileException( + ErrorUtils.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 => {