-import fs from 'node:fs';
+import { type FSWatcher, readFileSync } from 'node:fs';
import type { ChargingStation } from './ChargingStation';
import { ChargingStationUtils } from './ChargingStationUtils';
import { FileType, IdTagDistribution } from '../types';
-import { ErrorUtils, FileUtils, Utils, logger } from '../utils';
+import { Utils, handleFileException, logger, watchJsonFile } from '../utils';
type IdTagsCacheValueType = {
idTags: string[];
- idTagsFileWatcher: fs.FSWatcher | undefined;
+ idTagsFileWatcher: FSWatcher | undefined;
};
export class IdTagsCache {
return IdTagsCache.instance;
}
+ /**
+ * Gets one idtag from the cache given the distribution
+ * Must be called after checking the cache is not an empty array
+ *
+ * @param distribution -
+ * @param chargingStation -
+ * @param connectorId -
+ * @returns
+ */
public getIdTag(
distribution: IdTagDistribution,
chargingStation: ChargingStation,
}
}
+ /**
+ * Gets all idtags from the cache
+ * Must be called after checking the cache is not an empty array
+ *
+ * @param file -
+ * @returns
+ */
public getIdTags(file: string): string[] | undefined {
if (this.hasIdTagsCache(file) === false) {
this.setIdTagsCache(file, this.getIdTagsFromFile(file));
}
public deleteIdTags(file: string): boolean {
- return this.deleteIdTagsCache(file);
+ return this.deleteIdTagsCache(file) && this.deleteIdTagsCacheIndexes(file);
}
private getRandomIdTag(hashId: string, file: string): string {
private setIdTagsCache(file: string, idTags: string[]) {
return this.idTagsCaches.set(file, {
idTags,
- idTagsFileWatcher: FileUtils.watchJsonFile(
+ idTagsFileWatcher: watchJsonFile(
file,
FileType.Authorization,
this.logPrefix(file),
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[] {
- let idTags: string[] = [];
- if (file) {
+ if (Utils.isNotEmptyString(file)) {
try {
- // Load id tags file
- idTags = 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,
this.logPrefix(file)
);
}
- } else {
- logger.info(`${this.logPrefix(file)} No id tags file given`);
}
- return idTags;
+ return [];
}
private logPrefix = (file: string): string => {