Apply prettier formating
[e-mobility-charging-stations-simulator.git] / src / utils / Configuration.ts
1 import ConfigurationData, {
2 StationTemplateUrl,
3 StorageConfiguration,
4 SupervisionUrlDistribution,
5 UIWebSocketServerConfiguration,
6 } from '../types/ConfigurationData';
7
8 import Constants from './Constants';
9 import { EmptyObject } from '../types/EmptyObject';
10 import { HandleErrorParams } from '../types/Error';
11 import { ServerOptions } from 'ws';
12 import { StorageType } from '../types/Storage';
13 import type { WorkerChoiceStrategy } from 'poolifier';
14 import { WorkerProcessType } from '../types/Worker';
15 import chalk from 'chalk';
16 import fs from 'fs';
17 import path from 'path';
18
19 export default class Configuration {
20 private static configurationFilePath = path.join(
21 path.resolve(__dirname, '../'),
22 'assets',
23 'config.json'
24 );
25 private static configurationFileWatcher: fs.FSWatcher;
26 private static configuration: ConfigurationData | null = null;
27 private static configurationChangeCallback: () => Promise<void>;
28
29 static setConfigurationChangeCallback(cb: () => Promise<void>): void {
30 Configuration.configurationChangeCallback = cb;
31 }
32
33 static getLogStatisticsInterval(): number {
34 Configuration.warnDeprecatedConfigurationKey(
35 'statisticsDisplayInterval',
36 null,
37 "Use 'logStatisticsInterval' instead"
38 );
39 // Read conf
40 return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logStatisticsInterval')
41 ? Configuration.getConfig().logStatisticsInterval
42 : 60;
43 }
44
45 static getUIWebSocketServer(): UIWebSocketServerConfiguration {
46 let options: ServerOptions = {
47 host: Constants.DEFAULT_UI_WEBSOCKET_SERVER_HOST,
48 port: Constants.DEFAULT_UI_WEBSOCKET_SERVER_PORT,
49 };
50 let uiWebSocketServerConfiguration: UIWebSocketServerConfiguration = {
51 enabled: true,
52 options,
53 };
54 if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'uiWebSocketServer')) {
55 if (
56 Configuration.objectHasOwnProperty(Configuration.getConfig().uiWebSocketServer, 'options')
57 ) {
58 options = {
59 ...options,
60 ...(Configuration.objectHasOwnProperty(
61 Configuration.getConfig().uiWebSocketServer.options,
62 'host'
63 ) && { host: Configuration.getConfig().uiWebSocketServer.options.host }),
64 ...(Configuration.objectHasOwnProperty(
65 Configuration.getConfig().uiWebSocketServer.options,
66 'port'
67 ) && { port: Configuration.getConfig().uiWebSocketServer.options.port }),
68 };
69 }
70 uiWebSocketServerConfiguration = {
71 ...uiWebSocketServerConfiguration,
72 ...(Configuration.objectHasOwnProperty(
73 Configuration.getConfig().uiWebSocketServer,
74 'enabled'
75 ) && { enabled: Configuration.getConfig().uiWebSocketServer.enabled }),
76 options,
77 };
78 }
79 return uiWebSocketServerConfiguration;
80 }
81
82 static getPerformanceStorage(): StorageConfiguration {
83 Configuration.warnDeprecatedConfigurationKey('URI', 'performanceStorage', "Use 'uri' instead");
84 let storageConfiguration: StorageConfiguration = {
85 enabled: false,
86 type: StorageType.JSON_FILE,
87 uri: this.getDefaultPerformanceStorageUri(StorageType.JSON_FILE),
88 };
89 if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'performanceStorage')) {
90 storageConfiguration = {
91 ...storageConfiguration,
92 ...(Configuration.objectHasOwnProperty(
93 Configuration.getConfig().performanceStorage,
94 'enabled'
95 ) && { enabled: Configuration.getConfig().performanceStorage.enabled }),
96 ...(Configuration.objectHasOwnProperty(
97 Configuration.getConfig().performanceStorage,
98 'type'
99 ) && { type: Configuration.getConfig().performanceStorage.type }),
100 ...(Configuration.objectHasOwnProperty(
101 Configuration.getConfig().performanceStorage,
102 'uri'
103 ) && {
104 uri: this.getDefaultPerformanceStorageUri(
105 Configuration.getConfig()?.performanceStorage?.type ?? StorageType.JSON_FILE
106 ),
107 }),
108 };
109 }
110 return storageConfiguration;
111 }
112
113 static getAutoReconnectMaxRetries(): number {
114 Configuration.warnDeprecatedConfigurationKey(
115 'autoReconnectTimeout',
116 null,
117 "Use 'ConnectionTimeOut' OCPP parameter in charging station template instead"
118 );
119 Configuration.warnDeprecatedConfigurationKey(
120 'connectionTimeout',
121 null,
122 "Use 'ConnectionTimeOut' OCPP parameter in charging station template instead"
123 );
124 Configuration.warnDeprecatedConfigurationKey(
125 'autoReconnectMaxRetries',
126 null,
127 'Use it in charging station template instead'
128 );
129 // Read conf
130 if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'autoReconnectMaxRetries')) {
131 return Configuration.getConfig().autoReconnectMaxRetries;
132 }
133 }
134
135 static getStationTemplateUrls(): StationTemplateUrl[] {
136 Configuration.warnDeprecatedConfigurationKey(
137 'stationTemplateURLs',
138 null,
139 "Use 'stationTemplateUrls' instead"
140 );
141 !Configuration.isUndefined(Configuration.getConfig()['stationTemplateURLs']) &&
142 (Configuration.getConfig().stationTemplateUrls = Configuration.getConfig()[
143 'stationTemplateURLs'
144 ] as StationTemplateUrl[]);
145 Configuration.getConfig().stationTemplateUrls.forEach((stationUrl: StationTemplateUrl) => {
146 if (!Configuration.isUndefined(stationUrl['numberOfStation'])) {
147 console.error(
148 chalk`{green ${Configuration.logPrefix()}} {red Deprecated configuration key 'numberOfStation' usage for template file '${
149 stationUrl.file
150 }' in 'stationTemplateUrls'. Use 'numberOfStations' instead}`
151 );
152 }
153 });
154 // Read conf
155 return Configuration.getConfig().stationTemplateUrls;
156 }
157
158 static getWorkerProcess(): WorkerProcessType {
159 Configuration.warnDeprecatedConfigurationKey(
160 'useWorkerPool;',
161 null,
162 "Use 'workerProcess' to define the type of worker process to use instead"
163 );
164 return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerProcess')
165 ? Configuration.getConfig().workerProcess
166 : WorkerProcessType.WORKER_SET;
167 }
168
169 static getWorkerStartDelay(): number {
170 return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerStartDelay')
171 ? Configuration.getConfig().workerStartDelay
172 : Constants.WORKER_START_DELAY;
173 }
174
175 static getElementStartDelay(): number {
176 return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'elementStartDelay')
177 ? Configuration.getConfig().elementStartDelay
178 : Constants.ELEMENT_START_DELAY;
179 }
180
181 static getWorkerPoolMinSize(): number {
182 return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerPoolMinSize')
183 ? Configuration.getConfig().workerPoolMinSize
184 : Constants.DEFAULT_WORKER_POOL_MIN_SIZE;
185 }
186
187 static getWorkerPoolMaxSize(): number {
188 Configuration.warnDeprecatedConfigurationKey(
189 'workerPoolSize;',
190 null,
191 "Use 'workerPoolMaxSize' instead"
192 );
193 return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerPoolMaxSize')
194 ? Configuration.getConfig().workerPoolMaxSize
195 : Constants.DEFAULT_WORKER_POOL_MAX_SIZE;
196 }
197
198 static getWorkerPoolStrategy(): WorkerChoiceStrategy {
199 return Configuration.getConfig().workerPoolStrategy;
200 }
201
202 static getChargingStationsPerWorker(): number {
203 return Configuration.objectHasOwnProperty(
204 Configuration.getConfig(),
205 'chargingStationsPerWorker'
206 )
207 ? Configuration.getConfig().chargingStationsPerWorker
208 : Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER;
209 }
210
211 static getLogConsole(): boolean {
212 Configuration.warnDeprecatedConfigurationKey('consoleLog', null, "Use 'logConsole' instead");
213 return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logConsole')
214 ? Configuration.getConfig().logConsole
215 : false;
216 }
217
218 static getLogFormat(): string {
219 return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logFormat')
220 ? Configuration.getConfig().logFormat
221 : 'simple';
222 }
223
224 static getLogRotate(): boolean {
225 return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logRotate')
226 ? Configuration.getConfig().logRotate
227 : true;
228 }
229
230 static getLogMaxFiles(): number {
231 return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logMaxFiles')
232 ? Configuration.getConfig().logMaxFiles
233 : 7;
234 }
235
236 static getLogLevel(): string {
237 return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logLevel')
238 ? Configuration.getConfig().logLevel.toLowerCase()
239 : 'info';
240 }
241
242 static getLogFile(): string {
243 return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logFile')
244 ? Configuration.getConfig().logFile
245 : 'combined.log';
246 }
247
248 static getLogErrorFile(): string {
249 Configuration.warnDeprecatedConfigurationKey('errorFile', null, "Use 'logErrorFile' instead");
250 return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logErrorFile')
251 ? Configuration.getConfig().logErrorFile
252 : 'error.log';
253 }
254
255 static getSupervisionUrls(): string | string[] {
256 Configuration.warnDeprecatedConfigurationKey(
257 'supervisionURLs',
258 null,
259 "Use 'supervisionUrls' instead"
260 );
261 !Configuration.isUndefined(Configuration.getConfig()['supervisionURLs']) &&
262 (Configuration.getConfig().supervisionUrls = Configuration.getConfig()[
263 'supervisionURLs'
264 ] as string[]);
265 // Read conf
266 return Configuration.getConfig().supervisionUrls;
267 }
268
269 static getSupervisionUrlDistribution(): SupervisionUrlDistribution {
270 Configuration.warnDeprecatedConfigurationKey(
271 'distributeStationToTenantEqually',
272 null,
273 "Use 'supervisionUrlDistribution' instead"
274 );
275 Configuration.warnDeprecatedConfigurationKey(
276 'distributeStationsToTenantsEqually',
277 null,
278 "Use 'supervisionUrlDistribution' instead"
279 );
280 return Configuration.objectHasOwnProperty(
281 Configuration.getConfig(),
282 'supervisionUrlDistribution'
283 )
284 ? Configuration.getConfig().supervisionUrlDistribution
285 : SupervisionUrlDistribution.ROUND_ROBIN;
286 }
287
288 private static logPrefix(): string {
289 return new Date().toLocaleString() + ' Simulator configuration |';
290 }
291
292 private static warnDeprecatedConfigurationKey(
293 key: string,
294 sectionName?: string,
295 logMsgToAppend = ''
296 ) {
297 // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
298 if (
299 sectionName &&
300 !Configuration.isUndefined(Configuration.getConfig()[sectionName]) &&
301 !Configuration.isUndefined(Configuration.getConfig()[sectionName][key])
302 ) {
303 console.error(
304 chalk`{green ${Configuration.logPrefix()}} {red Deprecated configuration key '${key}' usage in section '${sectionName}'${
305 logMsgToAppend && '. ' + logMsgToAppend
306 }}`
307 );
308 } else if (!Configuration.isUndefined(Configuration.getConfig()[key])) {
309 console.error(
310 chalk`{green ${Configuration.logPrefix()}} {red Deprecated configuration key '${key}' usage${
311 logMsgToAppend && '. ' + logMsgToAppend
312 }}`
313 );
314 }
315 }
316
317 // Read the config file
318 private static getConfig(): ConfigurationData {
319 if (!Configuration.configuration) {
320 try {
321 Configuration.configuration = JSON.parse(
322 fs.readFileSync(Configuration.configurationFilePath, 'utf8')
323 ) as ConfigurationData;
324 } catch (error) {
325 Configuration.handleFileException(
326 Configuration.logPrefix(),
327 'Configuration',
328 Configuration.configurationFilePath,
329 error
330 );
331 }
332 if (!Configuration.configurationFileWatcher) {
333 Configuration.configurationFileWatcher = Configuration.getConfigurationFileWatcher();
334 }
335 }
336 return Configuration.configuration;
337 }
338
339 private static getConfigurationFileWatcher(): fs.FSWatcher {
340 try {
341 return fs.watch(Configuration.configurationFilePath, (event, filename): void => {
342 if (filename && event === 'change') {
343 // Nullify to force configuration file reading
344 Configuration.configuration = null;
345 if (!Configuration.isUndefined(Configuration.configurationChangeCallback)) {
346 Configuration.configurationChangeCallback().catch((error) => {
347 throw typeof error === 'string' ? new Error(error) : error;
348 });
349 }
350 }
351 });
352 } catch (error) {
353 Configuration.handleFileException(
354 Configuration.logPrefix(),
355 'Configuration',
356 Configuration.configurationFilePath,
357 error as Error
358 );
359 }
360 }
361
362 private static getDefaultPerformanceStorageUri(storageType: StorageType) {
363 const SQLiteFileName = `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`;
364 switch (storageType) {
365 case StorageType.JSON_FILE:
366 return `file://${path.join(
367 path.resolve(__dirname, '../../'),
368 Constants.DEFAULT_PERFORMANCE_RECORDS_FILENAME
369 )}`;
370 case StorageType.SQLITE:
371 return `file://${path.join(path.resolve(__dirname, '../../'), SQLiteFileName)}`;
372 default:
373 throw new Error(`Performance storage URI is mandatory with storage type '${storageType}'`);
374 }
375 }
376
377 private static objectHasOwnProperty(object: unknown, property: string): boolean {
378 return Object.prototype.hasOwnProperty.call(object, property) as boolean;
379 }
380
381 private static isUndefined(obj: unknown): boolean {
382 return typeof obj === 'undefined';
383 }
384
385 private static handleFileException(
386 logPrefix: string,
387 fileType: string,
388 filePath: string,
389 error: NodeJS.ErrnoException,
390 params: HandleErrorParams<EmptyObject> = { throwError: true }
391 ): void {
392 const prefix = logPrefix.length !== 0 ? logPrefix + ' ' : '';
393 if (error.code === 'ENOENT') {
394 console.error(
395 chalk.green(prefix) + chalk.red(fileType + ' file ' + filePath + ' not found: '),
396 error
397 );
398 } else if (error.code === 'EEXIST') {
399 console.error(
400 chalk.green(prefix) + chalk.red(fileType + ' file ' + filePath + ' already exists: '),
401 error
402 );
403 } else if (error.code === 'EACCES') {
404 console.error(
405 chalk.green(prefix) + chalk.red(fileType + ' file ' + filePath + ' access denied: '),
406 error
407 );
408 } else {
409 console.error(
410 chalk.green(prefix) + chalk.red(fileType + ' file ' + filePath + ' error: '),
411 error
412 );
413 }
414 if (params?.throwError) {
415 throw error;
416 }
417 }
418 }