+ static getDistributeStationsToTenantsEqually(): boolean {
+ Configuration.warnDeprecatedConfigurationKey('distributeStationToTenantEqually', null, 'Use \'distributeStationsToTenantsEqually\' instead');
+ return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'distributeStationsToTenantsEqually') ? Configuration.getConfig().distributeStationsToTenantsEqually : true;
+ }
+
+ private static logPrefix(): string {
+ return new Date().toLocaleString() + ' Simulator configuration |';
+ }
+
+ private static warnDeprecatedConfigurationKey(key: string, sectionName?: string, logMsgToAppend = '') {
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
+ if (sectionName && !Configuration.isUndefined(Configuration.getConfig()[sectionName]) && !Configuration.isUndefined(Configuration.getConfig()[sectionName][key])) {
+ console.error(chalk`{green ${Configuration.logPrefix()}} {red Deprecated configuration key '${key}' usage in section '${sectionName}'${logMsgToAppend && '. ' + logMsgToAppend}}`);
+ } else if (!Configuration.isUndefined(Configuration.getConfig()[key])) {
+ console.error(chalk`{green ${Configuration.logPrefix()}} {red Deprecated configuration key '${key}' usage${logMsgToAppend && '. ' + logMsgToAppend}}`);
+ }
+ }
+
+ // Read the config file
+ private static getConfig(): ConfigurationData {
+ if (!Configuration.configuration) {
+ try {
+ Configuration.configuration = JSON.parse(fs.readFileSync(Configuration.configurationFilePath, 'utf8')) as ConfigurationData;
+ } catch (error) {
+ Configuration.handleFileException(Configuration.logPrefix(), 'Configuration', Configuration.configurationFilePath, error);
+ }
+ if (!Configuration.configurationFileWatcher) {
+ Configuration.configurationFileWatcher = Configuration.getConfigurationFileWatcher();
+ }
+ }
+ return Configuration.configuration;
+ }
+
+ private static getConfigurationFileWatcher(): fs.FSWatcher {
+ try {
+ return fs.watch(Configuration.configurationFilePath, async (event, filename): Promise<void> => {
+ if (filename && event === 'change') {
+ // Nullify to force configuration file reading
+ Configuration.configuration = null;
+ if (!Configuration.isUndefined(Configuration.configurationChangeCallback)) {
+ await Configuration.configurationChangeCallback();
+ }
+ }
+ });
+ } catch (error) {
+ Configuration.handleFileException(Configuration.logPrefix(), 'Configuration', Configuration.configurationFilePath, error);
+ }
+ }
+
+ private static getDefaultPerformanceStorageURI(storageType: StorageType) {
+ const SQLiteFileName = `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`;
+ switch (storageType) {
+ case StorageType.JSON_FILE:
+ return `file://${path.join(path.resolve(__dirname, '../../'), Constants.DEFAULT_PERFORMANCE_RECORDS_FILENAME)}`;
+ case StorageType.SQLITE:
+ return `file://${path.join(path.resolve(__dirname, '../../'), SQLiteFileName)}`;
+ default:
+ throw new Error(`Performance storage URI is mandatory with storage type '${storageType}'`);
+ }
+ }
+
+ private static objectHasOwnProperty(object: unknown, property: string): boolean {
+ return Object.prototype.hasOwnProperty.call(object, property) as boolean;
+ }
+
+ private static isUndefined(obj: unknown): boolean {
+ return typeof obj === 'undefined';
+ }
+
+ private static handleFileException(logPrefix: string, fileType: string, filePath: string, error: NodeJS.ErrnoException): void {
+ const prefix = logPrefix.length !== 0 ? logPrefix + ' ' : '';
+ if (error.code === 'ENOENT') {
+ console.error(chalk.green(prefix) + chalk.red(fileType + ' file ' + filePath + ' not found: '), error);
+ } else if (error.code === 'EEXIST') {
+ console.error(chalk.green(prefix) + chalk.red(fileType + ' file ' + filePath + ' already exists: '), error);
+ } else if (error.code === 'EACCES') {
+ console.error(chalk.green(prefix) + chalk.red(fileType + ' file ' + filePath + ' access denied: '), error);
+ } else {
+ console.error(chalk.green(prefix) + chalk.red(fileType + ' file ' + filePath + ' error: '), error);
+ }
+ throw error;