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