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