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