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