X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FIdTagsCache.ts;h=890ac1fbf12892a04c7afcde3e27d04f09845154;hb=a679a1625e2f153a4c882bc8ff7befce50d4119a;hp=ceea0e5bd084c3cada92d63c4c88d7905517d63e;hpb=4c3c0d59f56be4d58e906e938c00390b41e0ca7f;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/IdTagsCache.ts b/src/charging-station/IdTagsCache.ts index ceea0e5b..890ac1fb 100644 --- a/src/charging-station/IdTagsCache.ts +++ b/src/charging-station/IdTagsCache.ts @@ -3,7 +3,7 @@ import fs from 'node:fs'; import type { ChargingStation } from './ChargingStation'; import { ChargingStationUtils } from './ChargingStationUtils'; import { FileType, IdTagDistribution } from '../types'; -import { FileUtils, Utils, logger } from '../utils'; +import { Utils, handleFileException, logger, watchJsonFile } from '../utils'; type IdTagsCacheValueType = { idTags: string[]; @@ -27,6 +27,15 @@ export class IdTagsCache { return IdTagsCache.instance; } + /** + * Get 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, @@ -46,6 +55,13 @@ export class IdTagsCache { } } + /** + * Get 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)); @@ -100,7 +116,7 @@ export class IdTagsCache { private setIdTagsCache(file: string, idTags: string[]) { return this.idTagsCaches.set(file, { idTags, - idTagsFileWatcher: FileUtils.watchJsonFile( + idTagsFileWatcher: watchJsonFile( file, FileType.Authorization, this.logPrefix(file), @@ -114,7 +130,7 @@ export class IdTagsCache { this.deleteIdTagsCache(file); this.deleteIdTagsCacheIndexes(file); } catch (error) { - FileUtils.handleFileException( + handleFileException( file, FileType.Authorization, error as NodeJS.ErrnoException, @@ -152,23 +168,19 @@ export class IdTagsCache { } 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(fs.readFileSync(file, 'utf8')) as string[]; } catch (error) { - FileUtils.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 => {