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