Commit | Line | Data |
---|---|---|
e118beaa JB |
1 | import ConfigurationData, { StationTemplateURL } from '../types/ConfigurationData'; |
2 | ||
ded13d97 | 3 | import Bootstrap from '../charging-station/Bootstrap'; |
a4624c96 | 4 | import { WorkerProcessType } from '../types/Worker'; |
3f40bc9c | 5 | import fs from 'fs'; |
7dde0b73 | 6 | |
3f40bc9c | 7 | export default class Configuration { |
ded13d97 JB |
8 | private static configurationFilePath = './src/assets/config.json'; |
9 | private static configurationFileWatcher: fs.FSWatcher; | |
eb3937cb | 10 | private static configuration: ConfigurationData; |
7dde0b73 | 11 | |
a4a21709 | 12 | static getStatisticsDisplayInterval(): number { |
7dde0b73 | 13 | // Read conf |
963ee397 | 14 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'statisticsDisplayInterval') ? Configuration.getConfig().statisticsDisplayInterval : 60; |
7dde0b73 JB |
15 | } |
16 | ||
032d6efc | 17 | static getConnectionTimeout(): number { |
3574dfd3 JB |
18 | Configuration.deprecateConfigurationKey('autoReconnectTimeout', 'Use \'connectionTimeout\' in charging station instead'); |
19 | Configuration.deprecateConfigurationKey('connectionTimeout', 'Use it in charging station template instead'); | |
7dde0b73 | 20 | // Read conf |
963ee397 | 21 | if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'connectionTimeout')) { |
3574dfd3 JB |
22 | return Configuration.getConfig().connectionTimeout; |
23 | } | |
7dde0b73 JB |
24 | } |
25 | ||
a4a21709 | 26 | static getAutoReconnectMaxRetries(): number { |
3574dfd3 | 27 | Configuration.deprecateConfigurationKey('autoReconnectMaxRetries', 'Use it in charging station template instead'); |
7dde0b73 | 28 | // Read conf |
963ee397 | 29 | if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'autoReconnectMaxRetries')) { |
3574dfd3 JB |
30 | return Configuration.getConfig().autoReconnectMaxRetries; |
31 | } | |
7dde0b73 JB |
32 | } |
33 | ||
e118beaa | 34 | static getStationTemplateURLs(): StationTemplateURL[] { |
eb3937cb | 35 | Configuration.getConfig().stationTemplateURLs.forEach((stationURL: StationTemplateURL) => { |
963ee397 | 36 | if (!Configuration.isUndefined(stationURL['numberOfStation'])) { |
eb3937cb JB |
37 | console.error(`Deprecated configuration key 'numberOfStation' usage for template file '${stationURL.file}' in 'stationTemplateURLs'. Use 'numberOfStations' instead`); |
38 | } | |
39 | }); | |
7dde0b73 JB |
40 | // Read conf |
41 | return Configuration.getConfig().stationTemplateURLs; | |
42 | } | |
43 | ||
a4624c96 JB |
44 | static getWorkerProcess(): WorkerProcessType { |
45 | Configuration.deprecateConfigurationKey('useWorkerPool;', 'Use \'workerProcess\' to define the type of worker process to use instead'); | |
46 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerProcess') ? Configuration.getConfig().workerProcess : WorkerProcessType.WORKER_SET; | |
47 | } | |
48 | ||
49 | static getWorkerPoolMinSize(): number { | |
50 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerPoolMinSize') ? Configuration.getConfig().workerPoolMinSize : 4; | |
7dde0b73 JB |
51 | } |
52 | ||
4fa59b8a JB |
53 | static getWorkerPoolMaxSize(): number { |
54 | Configuration.deprecateConfigurationKey('workerPoolSize;', 'Use \'workerPoolMaxSize\' instead'); | |
a4624c96 | 55 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerPoolMaxSize') ? Configuration.getConfig().workerPoolMaxSize : 16; |
7dde0b73 JB |
56 | } |
57 | ||
3d2ff9e4 J |
58 | static getChargingStationsPerWorker(): number { |
59 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'chargingStationsPerWorker') ? Configuration.getConfig().chargingStationsPerWorker : 1; | |
60 | } | |
61 | ||
7ec46a9a | 62 | static getLogConsole(): boolean { |
eb3937cb | 63 | Configuration.deprecateConfigurationKey('consoleLog', 'Use \'logConsole\' instead'); |
963ee397 | 64 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logConsole') ? Configuration.getConfig().logConsole : false; |
7dde0b73 JB |
65 | } |
66 | ||
a4a21709 | 67 | static getLogFormat(): string { |
963ee397 | 68 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logFormat') ? Configuration.getConfig().logFormat : 'simple'; |
027b409a JB |
69 | } |
70 | ||
6bf6769e | 71 | static getLogRotate(): boolean { |
963ee397 | 72 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logRotate') ? Configuration.getConfig().logRotate : true; |
6bf6769e JB |
73 | } |
74 | ||
75 | static getLogMaxFiles(): number { | |
963ee397 | 76 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logMaxFiles') ? Configuration.getConfig().logMaxFiles : 7; |
6bf6769e JB |
77 | } |
78 | ||
a4a21709 | 79 | static getLogLevel(): string { |
963ee397 | 80 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logLevel') ? Configuration.getConfig().logLevel : 'info'; |
2e6f5966 JB |
81 | } |
82 | ||
a4a21709 | 83 | static getLogFile(): string { |
963ee397 | 84 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logFile') ? Configuration.getConfig().logFile : 'combined.log'; |
7dde0b73 JB |
85 | } |
86 | ||
7ec46a9a | 87 | static getLogErrorFile(): string { |
eb3937cb | 88 | Configuration.deprecateConfigurationKey('errorFile', 'Use \'logErrorFile\' instead'); |
963ee397 | 89 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logErrorFile') ? Configuration.getConfig().logErrorFile : 'error.log'; |
7dde0b73 JB |
90 | } |
91 | ||
e118beaa | 92 | static getSupervisionURLs(): string[] { |
7dde0b73 JB |
93 | // Read conf |
94 | return Configuration.getConfig().supervisionURLs; | |
95 | } | |
96 | ||
524d9cb3 | 97 | static getDistributeStationsToTenantsEqually(): boolean { |
eb3937cb | 98 | Configuration.deprecateConfigurationKey('distributeStationToTenantEqually', 'Use \'distributeStationsToTenantsEqually\' instead'); |
963ee397 | 99 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'distributeStationsToTenantsEqually') ? Configuration.getConfig().distributeStationsToTenantsEqually : true; |
7dde0b73 | 100 | } |
eb3937cb JB |
101 | |
102 | private static deprecateConfigurationKey(key: string, logMsgToAppend = '') { | |
963ee397 | 103 | if (!Configuration.isUndefined(Configuration.getConfig()[key])) { |
eb3937cb JB |
104 | console.error(`Deprecated configuration key '${key}' usage${logMsgToAppend && '. ' + logMsgToAppend}`); |
105 | } | |
106 | } | |
107 | ||
108 | // Read the config file | |
109 | private static getConfig(): ConfigurationData { | |
110 | if (!Configuration.configuration) { | |
ded13d97 JB |
111 | Configuration.configuration = JSON.parse(fs.readFileSync(Configuration.configurationFilePath, 'utf8')) as ConfigurationData; |
112 | if (!Configuration.configurationFileWatcher) { | |
113 | Configuration.configurationFileWatcher = Configuration.getConfigurationFileWatcher(); | |
114 | } | |
eb3937cb JB |
115 | } |
116 | return Configuration.configuration; | |
117 | } | |
963ee397 | 118 | |
ded13d97 | 119 | private static getConfigurationFileWatcher(): fs.FSWatcher { |
a4cc42ea | 120 | // eslint-disable-next-line @typescript-eslint/no-misused-promises |
71623267 | 121 | return fs.watch(Configuration.configurationFilePath).on('change', async (e): Promise<void> => { |
ded13d97 JB |
122 | // Nullify to force configuration file reading |
123 | Configuration.configuration = null; | |
124 | await Bootstrap.getInstance().restart(); | |
125 | }); | |
126 | } | |
127 | ||
963ee397 | 128 | private static objectHasOwnProperty(object: any, property: string): boolean { |
b3f9c515 | 129 | return Object.prototype.hasOwnProperty.call(object, property) as boolean; |
963ee397 JB |
130 | } |
131 | ||
132 | private static isUndefined(obj: any): boolean { | |
133 | return typeof obj === 'undefined'; | |
134 | } | |
7dde0b73 | 135 | } |