Commit | Line | Data |
---|---|---|
e118beaa JB |
1 | import ConfigurationData, { StationTemplateURL } from '../types/ConfigurationData'; |
2 | ||
6af9012e | 3 | import Utils from './Utils'; |
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 { |
eb3937cb | 10 | Configuration.deprecateConfigurationKey(''); |
7dde0b73 | 11 | // Read conf |
2e6f5966 | 12 | return Utils.objectHasOwnProperty(Configuration.getConfig(), 'statisticsDisplayInterval') ? Configuration.getConfig().statisticsDisplayInterval : 60; |
7dde0b73 JB |
13 | } |
14 | ||
a4a21709 | 15 | static getAutoReconnectTimeout(): number { |
7dde0b73 | 16 | // Read conf |
1d7ca20c | 17 | return Utils.objectHasOwnProperty(Configuration.getConfig(), 'autoReconnectTimeout') ? Configuration.getConfig().autoReconnectTimeout : 10; |
7dde0b73 JB |
18 | } |
19 | ||
a4a21709 | 20 | static getAutoReconnectMaxRetries(): number { |
7dde0b73 | 21 | // Read conf |
1d7ca20c | 22 | return Utils.objectHasOwnProperty(Configuration.getConfig(), 'autoReconnectMaxRetries') ? Configuration.getConfig().autoReconnectMaxRetries : -1; |
7dde0b73 JB |
23 | } |
24 | ||
e118beaa | 25 | static getStationTemplateURLs(): StationTemplateURL[] { |
eb3937cb JB |
26 | Configuration.getConfig().stationTemplateURLs.forEach((stationURL: StationTemplateURL) => { |
27 | if (!Utils.isUndefined(stationURL['numberOfStation'])) { | |
28 | console.error(`Deprecated configuration key 'numberOfStation' usage for template file '${stationURL.file}' in 'stationTemplateURLs'. Use 'numberOfStations' instead`); | |
29 | } | |
30 | }); | |
7dde0b73 JB |
31 | // Read conf |
32 | return Configuration.getConfig().stationTemplateURLs; | |
33 | } | |
34 | ||
a4a21709 | 35 | static useWorkerPool(): boolean { |
7dde0b73 JB |
36 | return Configuration.getConfig().useWorkerPool; |
37 | } | |
38 | ||
a4a21709 | 39 | static getWorkerPoolSize(): number { |
7dde0b73 JB |
40 | return Configuration.getConfig().workerPoolSize; |
41 | } | |
42 | ||
7ec46a9a | 43 | static getLogConsole(): boolean { |
eb3937cb | 44 | Configuration.deprecateConfigurationKey('consoleLog', 'Use \'logConsole\' instead'); |
7ec46a9a | 45 | return Utils.objectHasOwnProperty(Configuration.getConfig(), 'logConsole') ? Configuration.getConfig().logConsole : false; |
7dde0b73 JB |
46 | } |
47 | ||
a4a21709 | 48 | static getLogFormat(): string { |
027b409a JB |
49 | return Utils.objectHasOwnProperty(Configuration.getConfig(), 'logFormat') ? Configuration.getConfig().logFormat : 'simple'; |
50 | } | |
51 | ||
6bf6769e JB |
52 | static getLogRotate(): boolean { |
53 | return Utils.objectHasOwnProperty(Configuration.getConfig(), 'logRotate') ? Configuration.getConfig().logRotate : true; | |
54 | } | |
55 | ||
56 | static getLogMaxFiles(): number { | |
57 | return Utils.objectHasOwnProperty(Configuration.getConfig(), 'logMaxFiles') ? Configuration.getConfig().logMaxFiles : 7; | |
58 | } | |
59 | ||
a4a21709 | 60 | static getLogLevel(): string { |
2e6f5966 JB |
61 | return Utils.objectHasOwnProperty(Configuration.getConfig(), 'logLevel') ? Configuration.getConfig().logLevel : 'info'; |
62 | } | |
63 | ||
a4a21709 | 64 | static getLogFile(): string { |
1d7ca20c | 65 | return Utils.objectHasOwnProperty(Configuration.getConfig(), 'logFile') ? Configuration.getConfig().logFile : 'combined.log'; |
7dde0b73 JB |
66 | } |
67 | ||
7ec46a9a | 68 | static getLogErrorFile(): string { |
eb3937cb | 69 | Configuration.deprecateConfigurationKey('errorFile', 'Use \'logErrorFile\' instead'); |
7ec46a9a | 70 | return Utils.objectHasOwnProperty(Configuration.getConfig(), 'logErrorFile') ? Configuration.getConfig().logErrorFile : 'error.log'; |
7dde0b73 JB |
71 | } |
72 | ||
e118beaa | 73 | static getSupervisionURLs(): string[] { |
7dde0b73 JB |
74 | // Read conf |
75 | return Configuration.getConfig().supervisionURLs; | |
76 | } | |
77 | ||
524d9cb3 | 78 | static getDistributeStationsToTenantsEqually(): boolean { |
eb3937cb | 79 | Configuration.deprecateConfigurationKey('distributeStationToTenantEqually', 'Use \'distributeStationsToTenantsEqually\' instead'); |
524d9cb3 | 80 | return Utils.objectHasOwnProperty(Configuration.getConfig(), 'distributeStationsToTenantsEqually') ? Configuration.getConfig().distributeStationsToTenantsEqually : true; |
7dde0b73 | 81 | } |
eb3937cb JB |
82 | |
83 | private static deprecateConfigurationKey(key: string, logMsgToAppend = '') { | |
84 | if (!Utils.isUndefined(Configuration.getConfig()[key])) { | |
85 | console.error(`Deprecated configuration key '${key}' usage${logMsgToAppend && '. ' + logMsgToAppend}`); | |
86 | } | |
87 | } | |
88 | ||
89 | // Read the config file | |
90 | private static getConfig(): ConfigurationData { | |
91 | if (!Configuration.configuration) { | |
92 | Configuration.configuration = JSON.parse(fs.readFileSync('./src/assets/config.json', 'utf8')) as ConfigurationData; | |
93 | } | |
94 | return Configuration.configuration; | |
95 | } | |
7dde0b73 | 96 | } |