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