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