Commit | Line | Data |
---|---|---|
2dcfe98e | 1 | import ConfigurationData, { StationTemplateUrl, StorageConfiguration, SupervisionUrlDistribution, UIWebSocketServerConfiguration } from '../types/ConfigurationData'; |
e118beaa | 2 | |
322c9192 | 3 | import Constants from './Constants'; |
6a49ad23 | 4 | import { ServerOptions } from 'ws'; |
72f041bd | 5 | import { StorageType } from '../types/Storage'; |
9efbac5b | 6 | import type { WorkerChoiceStrategy } from 'poolifier'; |
a4624c96 | 7 | import { WorkerProcessType } from '../types/Worker'; |
8eac9a09 | 8 | import chalk from 'chalk'; |
3f40bc9c | 9 | import fs from 'fs'; |
bf1866b2 | 10 | import path from 'path'; |
7dde0b73 | 11 | |
3f40bc9c | 12 | export default class Configuration { |
bf1866b2 | 13 | private static configurationFilePath = path.join(path.resolve(__dirname, '../'), 'assets', 'config.json'); |
ded13d97 | 14 | private static configurationFileWatcher: fs.FSWatcher; |
6e0964c8 | 15 | private static configuration: ConfigurationData | null = null; |
e57acf6a JB |
16 | private static configurationChangeCallback: () => Promise<void>; |
17 | ||
18 | static setConfigurationChangeCallback(cb: () => Promise<void>): void { | |
19 | Configuration.configurationChangeCallback = cb; | |
20 | } | |
7dde0b73 | 21 | |
72f041bd | 22 | static getLogStatisticsInterval(): number { |
e4362ed7 | 23 | Configuration.warnDeprecatedConfigurationKey('statisticsDisplayInterval', null, 'Use \'logStatisticsInterval\' instead'); |
7dde0b73 | 24 | // Read conf |
72f041bd JB |
25 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logStatisticsInterval') ? Configuration.getConfig().logStatisticsInterval : 60; |
26 | } | |
27 | ||
6a49ad23 JB |
28 | static getUIWebSocketServer(): UIWebSocketServerConfiguration { |
29 | let options: ServerOptions = { | |
30 | host: Constants.DEFAULT_UI_WEBSOCKET_SERVER_HOST, | |
31 | port: Constants.DEFAULT_UI_WEBSOCKET_SERVER_PORT | |
32 | }; | |
33 | let uiWebSocketServerConfiguration: UIWebSocketServerConfiguration = { | |
34 | enabled: true, | |
35 | options | |
36 | }; | |
37 | if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'uiWebSocketServer')) { | |
38 | if (Configuration.objectHasOwnProperty(Configuration.getConfig().uiWebSocketServer, 'options')) { | |
39 | options = { | |
1ba1e8fb | 40 | ...options, |
6a49ad23 JB |
41 | ...Configuration.objectHasOwnProperty(Configuration.getConfig().uiWebSocketServer.options, 'host') && { host: Configuration.getConfig().uiWebSocketServer.options.host }, |
42 | ...Configuration.objectHasOwnProperty(Configuration.getConfig().uiWebSocketServer.options, 'port') && { port: Configuration.getConfig().uiWebSocketServer.options.port } | |
43 | }; | |
44 | } | |
45 | uiWebSocketServerConfiguration = | |
46 | { | |
1ba1e8fb | 47 | ...uiWebSocketServerConfiguration, |
6a49ad23 JB |
48 | ...Configuration.objectHasOwnProperty(Configuration.getConfig().uiWebSocketServer, 'enabled') && { enabled: Configuration.getConfig().uiWebSocketServer.enabled }, |
49 | options | |
50 | }; | |
51 | } | |
52 | return uiWebSocketServerConfiguration; | |
53 | } | |
54 | ||
72f041bd | 55 | static getPerformanceStorage(): StorageConfiguration { |
1f5df42a | 56 | Configuration.warnDeprecatedConfigurationKey('URI', 'performanceStorage', 'Use \'uri\' instead'); |
6a49ad23 JB |
57 | let storageConfiguration: StorageConfiguration = { |
58 | enabled: false, | |
59 | type: StorageType.JSON_FILE, | |
1f5df42a | 60 | uri: this.getDefaultPerformanceStorageUri(StorageType.JSON_FILE) |
6a49ad23 | 61 | }; |
72f041bd JB |
62 | if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'performanceStorage')) { |
63 | storageConfiguration = | |
64 | { | |
1ba1e8fb JB |
65 | ...storageConfiguration, |
66 | ...Configuration.objectHasOwnProperty(Configuration.getConfig().performanceStorage, 'enabled') && { enabled: Configuration.getConfig().performanceStorage.enabled }, | |
67 | ...Configuration.objectHasOwnProperty(Configuration.getConfig().performanceStorage, 'type') && { type: Configuration.getConfig().performanceStorage.type }, | |
68 | ...Configuration.objectHasOwnProperty(Configuration.getConfig().performanceStorage, 'uri') && { uri: this.getDefaultPerformanceStorageUri(Configuration.getConfig()?.performanceStorage?.type ?? StorageType.JSON_FILE) } | |
72f041bd | 69 | }; |
72f041bd JB |
70 | } |
71 | return storageConfiguration; | |
7dde0b73 JB |
72 | } |
73 | ||
9ccca265 | 74 | static getAutoReconnectMaxRetries(): number { |
e4362ed7 JB |
75 | Configuration.warnDeprecatedConfigurationKey('autoReconnectTimeout', null, 'Use \'ConnectionTimeOut\' OCPP parameter in charging station template instead'); |
76 | Configuration.warnDeprecatedConfigurationKey('connectionTimeout', null, 'Use \'ConnectionTimeOut\' OCPP parameter in charging station template instead'); | |
77 | Configuration.warnDeprecatedConfigurationKey('autoReconnectMaxRetries', null, 'Use it in charging station template instead'); | |
7dde0b73 | 78 | // Read conf |
963ee397 | 79 | if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'autoReconnectMaxRetries')) { |
3574dfd3 JB |
80 | return Configuration.getConfig().autoReconnectMaxRetries; |
81 | } | |
7dde0b73 JB |
82 | } |
83 | ||
1f5df42a JB |
84 | static getStationTemplateUrls(): StationTemplateUrl[] { |
85 | Configuration.warnDeprecatedConfigurationKey('stationTemplateURLs', null, 'Use \'stationTemplateUrls\' instead'); | |
86 | !Configuration.isUndefined(Configuration.getConfig()['stationTemplateURLs']) && (Configuration.getConfig().stationTemplateUrls = Configuration.getConfig()['stationTemplateURLs'] as StationTemplateUrl[]); | |
87 | Configuration.getConfig().stationTemplateUrls.forEach((stationUrl: StationTemplateUrl) => { | |
88 | if (!Configuration.isUndefined(stationUrl['numberOfStation'])) { | |
89 | console.error(chalk`{green ${Configuration.logPrefix()}} {red Deprecated configuration key 'numberOfStation' usage for template file '${stationUrl.file}' in 'stationTemplateUrls'. Use 'numberOfStations' instead}`); | |
eb3937cb JB |
90 | } |
91 | }); | |
7dde0b73 | 92 | // Read conf |
1f5df42a | 93 | return Configuration.getConfig().stationTemplateUrls; |
7dde0b73 JB |
94 | } |
95 | ||
a4624c96 | 96 | static getWorkerProcess(): WorkerProcessType { |
e4362ed7 | 97 | Configuration.warnDeprecatedConfigurationKey('useWorkerPool;', null, 'Use \'workerProcess\' to define the type of worker process to use instead'); |
a4624c96 JB |
98 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerProcess') ? Configuration.getConfig().workerProcess : WorkerProcessType.WORKER_SET; |
99 | } | |
100 | ||
322c9192 JB |
101 | static getWorkerStartDelay(): number { |
102 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerStartDelay') ? Configuration.getConfig().workerStartDelay : Constants.WORKER_START_DELAY; | |
103 | } | |
104 | ||
a4624c96 | 105 | static getWorkerPoolMinSize(): number { |
1f0052b9 | 106 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerPoolMinSize') ? Configuration.getConfig().workerPoolMinSize : Constants.DEFAULT_WORKER_POOL_MIN_SIZE; |
7dde0b73 JB |
107 | } |
108 | ||
4fa59b8a | 109 | static getWorkerPoolMaxSize(): number { |
e4362ed7 | 110 | Configuration.warnDeprecatedConfigurationKey('workerPoolSize;', null, 'Use \'workerPoolMaxSize\' instead'); |
1f0052b9 | 111 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerPoolMaxSize') ? Configuration.getConfig().workerPoolMaxSize : Constants.DEFAULT_WORKER_POOL_MAX_SIZE; |
7dde0b73 JB |
112 | } |
113 | ||
9efbac5b JB |
114 | static getWorkerPoolStrategy(): WorkerChoiceStrategy { |
115 | return Configuration.getConfig().workerPoolStrategy; | |
116 | } | |
117 | ||
3d2ff9e4 | 118 | static getChargingStationsPerWorker(): number { |
1f0052b9 | 119 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'chargingStationsPerWorker') ? Configuration.getConfig().chargingStationsPerWorker : Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER; |
3d2ff9e4 J |
120 | } |
121 | ||
7ec46a9a | 122 | static getLogConsole(): boolean { |
e4362ed7 | 123 | Configuration.warnDeprecatedConfigurationKey('consoleLog', null, 'Use \'logConsole\' instead'); |
963ee397 | 124 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logConsole') ? Configuration.getConfig().logConsole : false; |
7dde0b73 JB |
125 | } |
126 | ||
a4a21709 | 127 | static getLogFormat(): string { |
963ee397 | 128 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logFormat') ? Configuration.getConfig().logFormat : 'simple'; |
027b409a JB |
129 | } |
130 | ||
6bf6769e | 131 | static getLogRotate(): boolean { |
963ee397 | 132 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logRotate') ? Configuration.getConfig().logRotate : true; |
6bf6769e JB |
133 | } |
134 | ||
135 | static getLogMaxFiles(): number { | |
963ee397 | 136 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logMaxFiles') ? Configuration.getConfig().logMaxFiles : 7; |
6bf6769e JB |
137 | } |
138 | ||
324fd4ee JB |
139 | static getLogLevel(): string { |
140 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logLevel') ? Configuration.getConfig().logLevel.toLowerCase() : 'info'; | |
2e6f5966 JB |
141 | } |
142 | ||
a4a21709 | 143 | static getLogFile(): string { |
963ee397 | 144 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logFile') ? Configuration.getConfig().logFile : 'combined.log'; |
7dde0b73 JB |
145 | } |
146 | ||
7ec46a9a | 147 | static getLogErrorFile(): string { |
e4362ed7 | 148 | Configuration.warnDeprecatedConfigurationKey('errorFile', null, 'Use \'logErrorFile\' instead'); |
963ee397 | 149 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logErrorFile') ? Configuration.getConfig().logErrorFile : 'error.log'; |
7dde0b73 JB |
150 | } |
151 | ||
2dcfe98e | 152 | static getSupervisionUrls(): string | string[] { |
1f5df42a JB |
153 | Configuration.warnDeprecatedConfigurationKey('supervisionURLs', null, 'Use \'supervisionUrls\' instead'); |
154 | !Configuration.isUndefined(Configuration.getConfig()['supervisionURLs']) && (Configuration.getConfig().supervisionUrls = Configuration.getConfig()['supervisionURLs'] as string[]); | |
7dde0b73 | 155 | // Read conf |
1f5df42a | 156 | return Configuration.getConfig().supervisionUrls; |
7dde0b73 JB |
157 | } |
158 | ||
2dcfe98e JB |
159 | static getSupervisionUrlDistribution(): SupervisionUrlDistribution { |
160 | Configuration.warnDeprecatedConfigurationKey('distributeStationToTenantEqually', null, 'Use \'supervisionUrlDistribution\' instead'); | |
161 | Configuration.warnDeprecatedConfigurationKey('distributeStationsToTenantsEqually', null, 'Use \'supervisionUrlDistribution\' instead'); | |
162 | return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'supervisionUrlDistribution') ? Configuration.getConfig().supervisionUrlDistribution : SupervisionUrlDistribution.ROUND_ROBIN; | |
7dde0b73 | 163 | } |
eb3937cb | 164 | |
23132a44 JB |
165 | private static logPrefix(): string { |
166 | return new Date().toLocaleString() + ' Simulator configuration |'; | |
167 | } | |
168 | ||
912136b1 JB |
169 | private static warnDeprecatedConfigurationKey(key: string, sectionName?: string, logMsgToAppend = '') { |
170 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | |
171 | if (sectionName && !Configuration.isUndefined(Configuration.getConfig()[sectionName]) && !Configuration.isUndefined(Configuration.getConfig()[sectionName][key])) { | |
e4362ed7 | 172 | console.error(chalk`{green ${Configuration.logPrefix()}} {red Deprecated configuration key '${key}' usage in section '${sectionName}'${logMsgToAppend && '. ' + logMsgToAppend}}`); |
912136b1 | 173 | } else if (!Configuration.isUndefined(Configuration.getConfig()[key])) { |
e4362ed7 | 174 | console.error(chalk`{green ${Configuration.logPrefix()}} {red Deprecated configuration key '${key}' usage${logMsgToAppend && '. ' + logMsgToAppend}}`); |
eb3937cb JB |
175 | } |
176 | } | |
177 | ||
178 | // Read the config file | |
179 | private static getConfig(): ConfigurationData { | |
180 | if (!Configuration.configuration) { | |
23132a44 JB |
181 | try { |
182 | Configuration.configuration = JSON.parse(fs.readFileSync(Configuration.configurationFilePath, 'utf8')) as ConfigurationData; | |
183 | } catch (error) { | |
184 | Configuration.handleFileException(Configuration.logPrefix(), 'Configuration', Configuration.configurationFilePath, error); | |
185 | } | |
ded13d97 JB |
186 | if (!Configuration.configurationFileWatcher) { |
187 | Configuration.configurationFileWatcher = Configuration.getConfigurationFileWatcher(); | |
188 | } | |
eb3937cb JB |
189 | } |
190 | return Configuration.configuration; | |
191 | } | |
963ee397 | 192 | |
ded13d97 | 193 | private static getConfigurationFileWatcher(): fs.FSWatcher { |
23132a44 | 194 | try { |
860a9c5a | 195 | return fs.watch(Configuration.configurationFilePath, (event, filename): void => { |
3ec10737 JB |
196 | if (filename && event === 'change') { |
197 | // Nullify to force configuration file reading | |
198 | Configuration.configuration = null; | |
199 | if (!Configuration.isUndefined(Configuration.configurationChangeCallback)) { | |
dcaf96dc JB |
200 | Configuration.configurationChangeCallback().catch((error) => { |
201 | throw typeof error === 'string' ? new Error(error) : error; | |
202 | }); | |
3ec10737 | 203 | } |
23132a44 JB |
204 | } |
205 | }); | |
206 | } catch (error) { | |
860a9c5a | 207 | Configuration.handleFileException(Configuration.logPrefix(), 'Configuration', Configuration.configurationFilePath, error as Error); |
23132a44 | 208 | } |
ded13d97 JB |
209 | } |
210 | ||
1f5df42a | 211 | private static getDefaultPerformanceStorageUri(storageType: StorageType) { |
d5603918 JB |
212 | const SQLiteFileName = `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`; |
213 | switch (storageType) { | |
214 | case StorageType.JSON_FILE: | |
215 | return `file://${path.join(path.resolve(__dirname, '../../'), Constants.DEFAULT_PERFORMANCE_RECORDS_FILENAME)}`; | |
216 | case StorageType.SQLITE: | |
217 | return `file://${path.join(path.resolve(__dirname, '../../'), SQLiteFileName)}`; | |
218 | default: | |
219 | throw new Error(`Performance storage URI is mandatory with storage type '${storageType}'`); | |
220 | } | |
221 | } | |
222 | ||
73d09045 | 223 | private static objectHasOwnProperty(object: unknown, property: string): boolean { |
23132a44 | 224 | return Object.prototype.hasOwnProperty.call(object, property) as boolean; |
963ee397 JB |
225 | } |
226 | ||
73d09045 | 227 | private static isUndefined(obj: unknown): boolean { |
963ee397 JB |
228 | return typeof obj === 'undefined'; |
229 | } | |
23132a44 JB |
230 | |
231 | private static handleFileException(logPrefix: string, fileType: string, filePath: string, error: NodeJS.ErrnoException): void { | |
232 | const prefix = logPrefix.length !== 0 ? logPrefix + ' ' : ''; | |
233 | if (error.code === 'ENOENT') { | |
e4362ed7 | 234 | console.error(chalk.green(prefix) + chalk.red(fileType + ' file ' + filePath + ' not found: '), error); |
72f041bd | 235 | } else if (error.code === 'EEXIST') { |
e4362ed7 | 236 | console.error(chalk.green(prefix) + chalk.red(fileType + ' file ' + filePath + ' already exists: '), error); |
72f041bd | 237 | } else if (error.code === 'EACCES') { |
e4362ed7 | 238 | console.error(chalk.green(prefix) + chalk.red(fileType + ' file ' + filePath + ' access denied: '), error); |
23132a44 | 239 | } else { |
e4362ed7 | 240 | console.error(chalk.green(prefix) + chalk.red(fileType + ' file ' + filePath + ' error: '), error); |
23132a44 JB |
241 | } |
242 | throw error; | |
243 | } | |
7dde0b73 | 244 | } |