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