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