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