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