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 = {
export class AuthorizedTagsCache {
private static instance: AuthorizedTagsCache | null = null;
private readonly tagsCaches: Map<string, TagsCacheValueType>;
+ private readonly tagsCachesAddressableIndexes: Map<string, number>;
private constructor() {
this.tagsCaches = new Map<string, TagsCacheValueType>();
+ this.tagsCachesAddressableIndexes = new Map<string, number>();
}
public static getInstance(): 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));
return this.deleteTags(file);
}
+ private getRandomIdTag(hashId: string, file: string): string {
+ const tags = this.getAuthorizedTags(file);
+ this.tagsCachesAddressableIndexes.set(
+ file + hashId,
+ Math.floor(Utils.secureRandom() * tags.length)
+ );
+ return tags[this.tagsCachesAddressableIndexes.get(file + hashId)];
+ }
+
+ private getRoundRobinIdTag(hashId: string, file: string): string {
+ const tags = this.getAuthorizedTags(file);
+ const idTagIndex = this.tagsCachesAddressableIndexes.get(file + hashId) ?? 0;
+ const idTag = tags[idTagIndex];
+ this.tagsCachesAddressableIndexes.set(
+ file + hashId,
+ 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;
+ this.tagsCachesAddressableIndexes.set(
+ file + hashId,
+ (chargingStation.index - 1 + (connectorId - 1)) % tags.length
+ );
+ return tags[this.tagsCachesAddressableIndexes.get(file + hashId)];
+ }
+
private hasTags(file: string): boolean {
return this.tagsCaches.has(file);
}
`${this.logPrefix(file)} ${FileType.Authorization} file have changed, reload`
);
this.deleteTags(file);
+ this.deleteTagsIndexes(file);
} catch (error) {
FileUtils.handleFileException(
file,
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) {