X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FAuthorizedTagsCache.ts;h=edb695386c8609b8927460e628d03a3c57144faa;hb=ba62a535da8d31400787113da1f77282967abb65;hp=48c72fd4bb2399d8bdd199bc016c0085c7a32711;hpb=d6a62ad9d0be1d1f29530dc9bae59272d40efee7;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/AuthorizedTagsCache.ts b/src/charging-station/AuthorizedTagsCache.ts index 48c72fd4..edb69538 100644 --- a/src/charging-station/AuthorizedTagsCache.ts +++ b/src/charging-station/AuthorizedTagsCache.ts @@ -1,6 +1,7 @@ import fs from 'node:fs'; -import { FileType } from '../types'; +import { type ChargingStation, ChargingStationUtils } from './internal'; +import { FileType, IdTagDistribution } from '../types'; import { FileUtils, Utils, logger } from '../utils'; type TagsCacheValueType = { @@ -11,9 +12,11 @@ type TagsCacheValueType = { export class AuthorizedTagsCache { private static instance: AuthorizedTagsCache | null = null; private readonly tagsCaches: Map; + private readonly tagsCachesAddressableIndexes: Map; private constructor() { this.tagsCaches = new Map(); + this.tagsCachesAddressableIndexes = new Map(); } public static getInstance(): AuthorizedTagsCache { @@ -23,6 +26,27 @@ export class AuthorizedTagsCache { return AuthorizedTagsCache.instance; } + public getIdTag( + distribution: IdTagDistribution, + chargingStation: ChargingStation, + connectorId: number + ): string { + const hashId = chargingStation.stationInfo.hashId; + const authorizationFile = ChargingStationUtils.getAuthorizationFile( + chargingStation.stationInfo + ); + switch (distribution) { + case IdTagDistribution.RANDOM: + return this.getRandomIdTag(hashId, authorizationFile); + case IdTagDistribution.ROUND_ROBIN: + return this.getRoundRobinIdTag(hashId, authorizationFile); + case IdTagDistribution.CONNECTOR_AFFINITY: + return this.getConnectorAffinityIdTag(chargingStation, connectorId); + default: + return this.getRoundRobinIdTag(hashId, authorizationFile); + } + } + public getAuthorizedTags(file: string): string[] | undefined { if (this.hasTags(file) === false) { this.setTags(file, this.getAuthorizedTagsFromFile(file)); @@ -34,6 +58,40 @@ export class AuthorizedTagsCache { return this.deleteTags(file); } + private getRandomIdTag(hashId: string, file: string): string { + const tags = this.getAuthorizedTags(file); + const addressableKey = file + hashId; + this.tagsCachesAddressableIndexes.set( + addressableKey, + Math.floor(Utils.secureRandom() * tags.length) + ); + return tags[this.tagsCachesAddressableIndexes.get(addressableKey)]; + } + + private getRoundRobinIdTag(hashId: string, file: string): string { + const tags = this.getAuthorizedTags(file); + const addressableKey = file + hashId; + const idTagIndex = this.tagsCachesAddressableIndexes.get(addressableKey) ?? 0; + const idTag = tags[idTagIndex]; + this.tagsCachesAddressableIndexes.set( + addressableKey, + idTagIndex === tags.length - 1 ? 0 : idTagIndex + 1 + ); + return idTag; + } + + private getConnectorAffinityIdTag(chargingStation: ChargingStation, connectorId: number): string { + const file = ChargingStationUtils.getAuthorizationFile(chargingStation.stationInfo); + const tags = this.getAuthorizedTags(file); + const hashId = chargingStation.stationInfo.hashId; + const addressableKey = file + hashId; + this.tagsCachesAddressableIndexes.set( + addressableKey, + (chargingStation.index - 1 + (connectorId - 1)) % tags.length + ); + return tags[this.tagsCachesAddressableIndexes.get(addressableKey)]; + } + private hasTags(file: string): boolean { return this.tagsCaches.has(file); } @@ -53,6 +111,7 @@ export class AuthorizedTagsCache { `${this.logPrefix(file)} ${FileType.Authorization} file have changed, reload` ); this.deleteTags(file); + this.deleteTagsIndexes(file); } catch (error) { FileUtils.handleFileException( file, @@ -79,6 +138,14 @@ export class AuthorizedTagsCache { return this.tagsCaches.delete(file); } + private deleteTagsIndexes(file: string): void { + for (const [key] of this.tagsCachesAddressableIndexes) { + if (key.startsWith(file)) { + this.tagsCachesAddressableIndexes.delete(key); + } + } + } + private getAuthorizedTagsFromFile(file: string): string[] { let authorizedTags: string[] = []; if (file) {