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