import { type FSWatcher, readFileSync } from 'node:fs';
import type { ChargingStation } from './ChargingStation';
-import { getIdTagsFile } from './ChargingStationUtils';
+import { getIdTagsFile } from './Helpers';
import { FileType, IdTagDistribution } from '../types';
import {
handleFileException,
watchJsonFile,
} from '../utils';
-type IdTagsCacheValueType = {
+interface IdTagsCacheValueType {
idTags: string[];
idTagsFileWatcher: FSWatcher | undefined;
-};
+}
export class IdTagsCache {
private static instance: IdTagsCache | null = null;
connectorId: number,
): string {
const hashId = chargingStation.stationInfo.hashId;
- const idTagsFile = getIdTagsFile(chargingStation.stationInfo);
+ const idTagsFile = getIdTagsFile(chargingStation.stationInfo)!;
switch (distribution) {
case IdTagDistribution.RANDOM:
return this.getRandomIdTag(hashId, idTagsFile);
}
private getRandomIdTag(hashId: string, file: string): string {
- const idTags = this.getIdTags(file);
+ const idTags = this.getIdTags(file)!;
const addressableKey = this.getIdTagsCacheIndexesAddressableKey(file, hashId);
this.idTagsCachesAddressableIndexes.set(
addressableKey,
Math.floor(secureRandom() * idTags.length),
);
- return idTags[this.idTagsCachesAddressableIndexes.get(addressableKey)];
+ return idTags[this.idTagsCachesAddressableIndexes.get(addressableKey)!];
}
private getRoundRobinIdTag(hashId: string, file: string): string {
- const idTags = this.getIdTags(file);
+ const idTags = this.getIdTags(file)!;
const addressableKey = this.getIdTagsCacheIndexesAddressableKey(file, hashId);
const idTagIndex = this.idTagsCachesAddressableIndexes.get(addressableKey) ?? 0;
const idTag = idTags[idTagIndex];
}
private getConnectorAffinityIdTag(chargingStation: ChargingStation, connectorId: number): string {
- const file = getIdTagsFile(chargingStation.stationInfo);
- const idTags = this.getIdTags(file);
+ const file = getIdTagsFile(chargingStation.stationInfo)!;
+ const idTags = this.getIdTags(file)!;
const addressableKey = this.getIdTagsCacheIndexesAddressableKey(
file,
chargingStation.stationInfo.hashId,
addressableKey,
(chargingStation.index - 1 + (connectorId - 1)) % idTags.length,
);
- return idTags[this.idTagsCachesAddressableIndexes.get(addressableKey)];
+ return idTags[this.idTagsCachesAddressableIndexes.get(addressableKey)!];
}
private hasIdTagsCache(file: string): boolean {
}
private deleteIdTagsCacheIndexes(file: string): boolean {
- let deleted: boolean[];
+ const deleted: boolean[] = [];
for (const [key] of this.idTagsCachesAddressableIndexes) {
if (key.startsWith(file)) {
deleted.push(this.idTagsCachesAddressableIndexes.delete(key));