-import fs from 'node:fs';
+import { type FSWatcher, readFileSync } from 'node:fs';
import type { ChargingStation } from './ChargingStation';
-import { ChargingStationUtils } from './ChargingStationUtils';
+import { getIdTagsFile } from './ChargingStationUtils';
import { FileType, IdTagDistribution } from '../types';
-import { ErrorUtils, FileUtils, Utils, logger } from '../utils';
+import {
+ handleFileException,
+ isNotEmptyString,
+ logPrefix,
+ logger,
+ secureRandom,
+ watchJsonFile,
+} from '../utils';
type IdTagsCacheValueType = {
idTags: string[];
- idTagsFileWatcher: fs.FSWatcher | undefined;
+ idTagsFileWatcher: FSWatcher | undefined;
};
export class IdTagsCache {
}
/**
- * Get one idtag from the cache given the distribution
+ * Gets one idtag from the cache given the distribution
* Must be called after checking the cache is not an empty array
*
* @param distribution -
connectorId: number
): string {
const hashId = chargingStation.stationInfo.hashId;
- const idTagsFile = ChargingStationUtils.getIdTagsFile(chargingStation.stationInfo);
+ const idTagsFile = getIdTagsFile(chargingStation.stationInfo);
switch (distribution) {
case IdTagDistribution.RANDOM:
return this.getRandomIdTag(hashId, idTagsFile);
}
/**
- * Get all idtags from the cache
+ * Gets all idtags from the cache
* Must be called after checking the cache is not an empty array
*
* @param file -
}
public deleteIdTags(file: string): boolean {
- return this.deleteIdTagsCache(file);
+ return this.deleteIdTagsCache(file) && this.deleteIdTagsCacheIndexes(file);
}
private getRandomIdTag(hashId: string, file: string): string {
const addressableKey = this.getIdTagsCacheIndexesAddressableKey(file, hashId);
this.idTagsCachesAddressableIndexes.set(
addressableKey,
- Math.floor(Utils.secureRandom() * idTags.length)
+ Math.floor(secureRandom() * idTags.length)
);
return idTags[this.idTagsCachesAddressableIndexes.get(addressableKey)];
}
}
private getConnectorAffinityIdTag(chargingStation: ChargingStation, connectorId: number): string {
- const file = ChargingStationUtils.getIdTagsFile(chargingStation.stationInfo);
+ const file = getIdTagsFile(chargingStation.stationInfo);
const idTags = this.getIdTags(file);
const addressableKey = this.getIdTagsCacheIndexesAddressableKey(
file,
private setIdTagsCache(file: string, idTags: string[]) {
return this.idTagsCaches.set(file, {
idTags,
- idTagsFileWatcher: FileUtils.watchJsonFile(
+ idTagsFileWatcher: watchJsonFile(
file,
FileType.Authorization,
this.logPrefix(file),
undefined,
(event, filename) => {
- if (Utils.isNotEmptyString(filename) && event === 'change') {
+ if (isNotEmptyString(filename) && event === 'change') {
try {
logger.debug(
`${this.logPrefix(file)} ${FileType.Authorization} file have changed, reload`
this.deleteIdTagsCache(file);
this.deleteIdTagsCacheIndexes(file);
} catch (error) {
- ErrorUtils.handleFileException(
+ handleFileException(
file,
FileType.Authorization,
error as NodeJS.ErrnoException,
return this.idTagsCaches.delete(file);
}
- private deleteIdTagsCacheIndexes(file: string): void {
+ private deleteIdTagsCacheIndexes(file: string): boolean {
+ let deleted: boolean[];
for (const [key] of this.idTagsCachesAddressableIndexes) {
if (key.startsWith(file)) {
- this.idTagsCachesAddressableIndexes.delete(key);
+ deleted.push(this.idTagsCachesAddressableIndexes.delete(key));
}
}
+ return !deleted.some((value) => value === false);
}
private getIdTagsCacheIndexesAddressableKey(prefix: string, uid: string): string {
}
private getIdTagsFromFile(file: string): string[] {
- if (Utils.isNotEmptyString(file)) {
+ if (isNotEmptyString(file)) {
try {
- return JSON.parse(fs.readFileSync(file, 'utf8')) as string[];
+ return JSON.parse(readFileSync(file, 'utf8')) as string[];
} catch (error) {
- ErrorUtils.handleFileException(
+ handleFileException(
file,
FileType.Authorization,
error as NodeJS.ErrnoException,
}
private logPrefix = (file: string): string => {
- return Utils.logPrefix(` Id tags cache for id tags file '${file}' |`);
+ return logPrefix(` Id tags cache for id tags file '${file}' |`);
};
}