refactor(simulator): remove unneeded condition check in log message
[e-mobility-charging-stations-simulator.git] / src / charging-station / AuthorizedTagsCache.ts
CommitLineData
130783a7 1import fs from 'node:fs';
8114d10e 2
268a74bb 3import { FileType } from '../types';
60a74391 4import { FileUtils, Utils, logger } from '../utils';
9d7484a4 5
d6a62ad9
JB
6type TagsCacheValueType = {
7 tags: string[];
8 tagsFileWatcher: fs.FSWatcher | undefined;
9};
10
268a74bb 11export class AuthorizedTagsCache {
9d7484a4 12 private static instance: AuthorizedTagsCache | null = null;
d6a62ad9 13 private readonly tagsCaches: Map<string, TagsCacheValueType>;
9d7484a4
JB
14
15 private constructor() {
d6a62ad9 16 this.tagsCaches = new Map<string, TagsCacheValueType>();
9d7484a4
JB
17 }
18
19 public static getInstance(): AuthorizedTagsCache {
1ca780f9 20 if (AuthorizedTagsCache.instance === null) {
9d7484a4
JB
21 AuthorizedTagsCache.instance = new AuthorizedTagsCache();
22 }
23 return AuthorizedTagsCache.instance;
24 }
25
1895299d 26 public getAuthorizedTags(file: string): string[] | undefined {
0638ddd2 27 if (this.hasTags(file) === false) {
9d7484a4 28 this.setTags(file, this.getAuthorizedTagsFromFile(file));
9d7484a4
JB
29 }
30 return this.getTags(file);
31 }
32
a36bad10
JB
33 public deleteAuthorizedTags(file: string): boolean {
34 return this.deleteTags(file);
35 }
36
9d7484a4
JB
37 private hasTags(file: string): boolean {
38 return this.tagsCaches.has(file);
39 }
40
41 private setTags(file: string, tags: string[]) {
d6a62ad9
JB
42 return this.tagsCaches.set(file, {
43 tags,
44 tagsFileWatcher: FileUtils.watchJsonFile(
45 file,
46 FileType.Authorization,
47 this.logPrefix(file),
48 undefined,
49 (event, filename) => {
50 if (Utils.isNotEmptyString(filename) && event === 'change') {
51 try {
52 logger.debug(
53 `${this.logPrefix(file)} ${FileType.Authorization} file have changed, reload`
54 );
55 this.deleteTags(file);
56 } catch (error) {
57 FileUtils.handleFileException(
58 file,
59 FileType.Authorization,
60 error as NodeJS.ErrnoException,
61 this.logPrefix(file),
62 {
63 throwError: false,
64 }
65 );
66 }
67 }
68 }
69 ),
70 });
9d7484a4
JB
71 }
72
1895299d 73 private getTags(file: string): string[] | undefined {
d6a62ad9 74 return this.tagsCaches.get(file)?.tags;
9d7484a4
JB
75 }
76
77 private deleteTags(file: string): boolean {
d6a62ad9 78 this.tagsCaches.get(file)?.tagsFileWatcher?.close();
9d7484a4
JB
79 return this.tagsCaches.delete(file);
80 }
81
9d7484a4
JB
82 private getAuthorizedTagsFromFile(file: string): string[] {
83 let authorizedTags: string[] = [];
84 if (file) {
85 try {
86 // Load authorization file
87 authorizedTags = JSON.parse(fs.readFileSync(file, 'utf8')) as string[];
88 } catch (error) {
89 FileUtils.handleFileException(
9d7484a4 90 file,
7164966d
JB
91 FileType.Authorization,
92 error as NodeJS.ErrnoException,
93 this.logPrefix(file)
9d7484a4
JB
94 );
95 }
96 } else {
44eb6026 97 logger.info(`${this.logPrefix(file)} No authorization file given`);
9d7484a4
JB
98 }
99 return authorizedTags;
100 }
101
8b7072dc 102 private logPrefix = (file: string): string => {
9d7484a4 103 return Utils.logPrefix(` Authorized tags cache for authorization file '${file}' |`);
8b7072dc 104 };
9d7484a4 105}