From 963ee397d378bffb9d41154a38bd6a83e4ac0fb8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 23 Nov 2020 20:06:31 +0100 Subject: [PATCH] Make Configuration class self-contained. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/utils/Configuration.ts | 35 +++++++++++++++++++++-------------- src/utils/Utils.ts | 4 ---- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index e22d8a8e..76069525 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -1,6 +1,5 @@ import ConfigurationData, { StationTemplateURL } from '../types/ConfigurationData'; -import Utils from './Utils'; import fs from 'fs'; export default class Configuration { @@ -8,14 +7,14 @@ export default class Configuration { static getStatisticsDisplayInterval(): number { // Read conf - return Utils.objectHasOwnProperty(Configuration.getConfig(), 'statisticsDisplayInterval') ? Configuration.getConfig().statisticsDisplayInterval : 60; + return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'statisticsDisplayInterval') ? Configuration.getConfig().statisticsDisplayInterval : 60; } static getConnectionTimeout(): number { Configuration.deprecateConfigurationKey('autoReconnectTimeout', 'Use \'connectionTimeout\' in charging station instead'); Configuration.deprecateConfigurationKey('connectionTimeout', 'Use it in charging station template instead'); // Read conf - if (Utils.objectHasOwnProperty(Configuration.getConfig(), 'connectionTimeout')) { + if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'connectionTimeout')) { return Configuration.getConfig().connectionTimeout; } } @@ -23,14 +22,14 @@ export default class Configuration { static getAutoReconnectMaxRetries(): number { Configuration.deprecateConfigurationKey('autoReconnectMaxRetries', 'Use it in charging station template instead'); // Read conf - if (Utils.objectHasOwnProperty(Configuration.getConfig(), 'autoReconnectMaxRetries')) { + if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'autoReconnectMaxRetries')) { return Configuration.getConfig().autoReconnectMaxRetries; } } static getStationTemplateURLs(): StationTemplateURL[] { Configuration.getConfig().stationTemplateURLs.forEach((stationURL: StationTemplateURL) => { - if (!Utils.isUndefined(stationURL['numberOfStation'])) { + if (!Configuration.isUndefined(stationURL['numberOfStation'])) { console.error(`Deprecated configuration key 'numberOfStation' usage for template file '${stationURL.file}' in 'stationTemplateURLs'. Use 'numberOfStations' instead`); } }); @@ -48,32 +47,32 @@ export default class Configuration { static getLogConsole(): boolean { Configuration.deprecateConfigurationKey('consoleLog', 'Use \'logConsole\' instead'); - return Utils.objectHasOwnProperty(Configuration.getConfig(), 'logConsole') ? Configuration.getConfig().logConsole : false; + return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logConsole') ? Configuration.getConfig().logConsole : false; } static getLogFormat(): string { - return Utils.objectHasOwnProperty(Configuration.getConfig(), 'logFormat') ? Configuration.getConfig().logFormat : 'simple'; + return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logFormat') ? Configuration.getConfig().logFormat : 'simple'; } static getLogRotate(): boolean { - return Utils.objectHasOwnProperty(Configuration.getConfig(), 'logRotate') ? Configuration.getConfig().logRotate : true; + return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logRotate') ? Configuration.getConfig().logRotate : true; } static getLogMaxFiles(): number { - return Utils.objectHasOwnProperty(Configuration.getConfig(), 'logMaxFiles') ? Configuration.getConfig().logMaxFiles : 7; + return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logMaxFiles') ? Configuration.getConfig().logMaxFiles : 7; } static getLogLevel(): string { - return Utils.objectHasOwnProperty(Configuration.getConfig(), 'logLevel') ? Configuration.getConfig().logLevel : 'info'; + return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logLevel') ? Configuration.getConfig().logLevel : 'info'; } static getLogFile(): string { - return Utils.objectHasOwnProperty(Configuration.getConfig(), 'logFile') ? Configuration.getConfig().logFile : 'combined.log'; + return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logFile') ? Configuration.getConfig().logFile : 'combined.log'; } static getLogErrorFile(): string { Configuration.deprecateConfigurationKey('errorFile', 'Use \'logErrorFile\' instead'); - return Utils.objectHasOwnProperty(Configuration.getConfig(), 'logErrorFile') ? Configuration.getConfig().logErrorFile : 'error.log'; + return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logErrorFile') ? Configuration.getConfig().logErrorFile : 'error.log'; } static getSupervisionURLs(): string[] { @@ -83,11 +82,11 @@ export default class Configuration { static getDistributeStationsToTenantsEqually(): boolean { Configuration.deprecateConfigurationKey('distributeStationToTenantEqually', 'Use \'distributeStationsToTenantsEqually\' instead'); - return Utils.objectHasOwnProperty(Configuration.getConfig(), 'distributeStationsToTenantsEqually') ? Configuration.getConfig().distributeStationsToTenantsEqually : true; + return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'distributeStationsToTenantsEqually') ? Configuration.getConfig().distributeStationsToTenantsEqually : true; } private static deprecateConfigurationKey(key: string, logMsgToAppend = '') { - if (!Utils.isUndefined(Configuration.getConfig()[key])) { + if (!Configuration.isUndefined(Configuration.getConfig()[key])) { console.error(`Deprecated configuration key '${key}' usage${logMsgToAppend && '. ' + logMsgToAppend}`); } } @@ -99,4 +98,12 @@ export default class Configuration { } return Configuration.configuration; } + + private static objectHasOwnProperty(object: any, property: string): boolean { + return Object.prototype.hasOwnProperty.call(object, property); + } + + private static isUndefined(obj: any): boolean { + return typeof obj === 'undefined'; + } } diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index a1f28155..4876f46d 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -117,10 +117,6 @@ export default class Utils { return date.toLocaleString() + prefixString; } - static objectHasOwnProperty(object, property): boolean { - return Object.prototype.hasOwnProperty.call(object, property); - } - static cloneObject(object) { return JSON.parse(JSON.stringify(object)); } -- 2.34.1