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