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