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