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