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