refactor: rename a template key to a more sensible name
[e-mobility-charging-stations-simulator.git] / src / charging-station / IdTagsCache.ts
1 import fs from 'node:fs';
2
3 import { type ChargingStation, ChargingStationUtils } from './internal';
4 import { FileType, IdTagDistribution } from '../types';
5 import { FileUtils, Utils, logger } from '../utils';
6
7 type IdTagsCacheValueType = {
8 idTags: string[];
9 idTagsFileWatcher: fs.FSWatcher | undefined;
10 };
11
12 export class IdTagsCache {
13 private static instance: IdTagsCache | null = null;
14 private readonly idTagsCaches: Map<string, IdTagsCacheValueType>;
15 private readonly idTagsCachesAddressableIndexes: Map<string, number>;
16
17 private constructor() {
18 this.idTagsCaches = new Map<string, IdTagsCacheValueType>();
19 this.idTagsCachesAddressableIndexes = new Map<string, number>();
20 }
21
22 public static getInstance(): IdTagsCache {
23 if (IdTagsCache.instance === null) {
24 IdTagsCache.instance = new IdTagsCache();
25 }
26 return IdTagsCache.instance;
27 }
28
29 public getIdTag(
30 distribution: IdTagDistribution,
31 chargingStation: ChargingStation,
32 connectorId: number
33 ): string {
34 const hashId = chargingStation.stationInfo.hashId;
35 const idTagsFile = ChargingStationUtils.getIdTagsFile(chargingStation.stationInfo);
36 switch (distribution) {
37 case IdTagDistribution.RANDOM:
38 return this.getRandomIdTag(hashId, idTagsFile);
39 case IdTagDistribution.ROUND_ROBIN:
40 return this.getRoundRobinIdTag(hashId, idTagsFile);
41 case IdTagDistribution.CONNECTOR_AFFINITY:
42 return this.getConnectorAffinityIdTag(chargingStation, connectorId);
43 default:
44 return this.getRoundRobinIdTag(hashId, idTagsFile);
45 }
46 }
47
48 public getIdTags(file: string): string[] | undefined {
49 if (this.hasIdTagsCache(file) === false) {
50 this.setIdTagsCache(file, this.getIdTagsFromFile(file));
51 }
52 return this.getIdTagsCache(file);
53 }
54
55 public deleteIdTags(file: string): boolean {
56 return this.deleteIdTagsCache(file);
57 }
58
59 private getRandomIdTag(hashId: string, file: string): string {
60 const idTags = this.getIdTags(file);
61 const addressableKey = file + hashId;
62 this.idTagsCachesAddressableIndexes.set(
63 addressableKey,
64 Math.floor(Utils.secureRandom() * idTags.length)
65 );
66 return idTags[this.idTagsCachesAddressableIndexes.get(addressableKey)];
67 }
68
69 private getRoundRobinIdTag(hashId: string, file: string): string {
70 const idTags = this.getIdTags(file);
71 const addressableKey = file + hashId;
72 const idTagIndex = this.idTagsCachesAddressableIndexes.get(addressableKey) ?? 0;
73 const idTag = idTags[idTagIndex];
74 this.idTagsCachesAddressableIndexes.set(
75 addressableKey,
76 idTagIndex === idTags.length - 1 ? 0 : idTagIndex + 1
77 );
78 return idTag;
79 }
80
81 private getConnectorAffinityIdTag(chargingStation: ChargingStation, connectorId: number): string {
82 const file = ChargingStationUtils.getIdTagsFile(chargingStation.stationInfo);
83 const idTags = this.getIdTags(file);
84 const hashId = chargingStation.stationInfo.hashId;
85 const addressableKey = file + hashId;
86 this.idTagsCachesAddressableIndexes.set(
87 addressableKey,
88 (chargingStation.index - 1 + (connectorId - 1)) % idTags.length
89 );
90 return idTags[this.idTagsCachesAddressableIndexes.get(addressableKey)];
91 }
92
93 private hasIdTagsCache(file: string): boolean {
94 return this.idTagsCaches.has(file);
95 }
96
97 private setIdTagsCache(file: string, idTags: string[]) {
98 return this.idTagsCaches.set(file, {
99 idTags,
100 idTagsFileWatcher: FileUtils.watchJsonFile(
101 file,
102 FileType.Authorization,
103 this.logPrefix(file),
104 undefined,
105 (event, filename) => {
106 if (Utils.isNotEmptyString(filename) && event === 'change') {
107 try {
108 logger.debug(
109 `${this.logPrefix(file)} ${FileType.Authorization} file have changed, reload`
110 );
111 this.deleteIdTagsCache(file);
112 this.deleteIdTagsCacheIndexes(file);
113 } catch (error) {
114 FileUtils.handleFileException(
115 file,
116 FileType.Authorization,
117 error as NodeJS.ErrnoException,
118 this.logPrefix(file),
119 {
120 throwError: false,
121 }
122 );
123 }
124 }
125 }
126 ),
127 });
128 }
129
130 private getIdTagsCache(file: string): string[] | undefined {
131 return this.idTagsCaches.get(file)?.idTags;
132 }
133
134 private deleteIdTagsCache(file: string): boolean {
135 this.idTagsCaches.get(file)?.idTagsFileWatcher?.close();
136 return this.idTagsCaches.delete(file);
137 }
138
139 private deleteIdTagsCacheIndexes(file: string): void {
140 for (const [key] of this.idTagsCachesAddressableIndexes) {
141 if (key.startsWith(file)) {
142 this.idTagsCachesAddressableIndexes.delete(key);
143 }
144 }
145 }
146
147 private getIdTagsFromFile(file: string): string[] {
148 let idTags: string[] = [];
149 if (file) {
150 try {
151 // Load id tags file
152 idTags = JSON.parse(fs.readFileSync(file, 'utf8')) as string[];
153 } catch (error) {
154 FileUtils.handleFileException(
155 file,
156 FileType.Authorization,
157 error as NodeJS.ErrnoException,
158 this.logPrefix(file)
159 );
160 }
161 } else {
162 logger.info(`${this.logPrefix(file)} No id tags file given`);
163 }
164 return idTags;
165 }
166
167 private logPrefix = (file: string): string => {
168 return Utils.logPrefix(` Id tags cache for id tags file '${file}' |`);
169 };
170 }