Simplify ATG logPrefix() method
[e-mobility-charging-stations-simulator.git] / src / utils / Configuration.ts
index 57ee31df0c355f0ddc6dd736d35a402ae9e70cb6..491c377e9372cf7543406c67e49cda1d6f454005 100644 (file)
@@ -1,23 +1,25 @@
 import ConfigurationData, {
+  ServerOptions,
   StationTemplateUrl,
   StorageConfiguration,
   SupervisionUrlDistribution,
-  UIWebSocketServerConfiguration,
+  UIServerConfiguration,
 } from '../types/ConfigurationData';
 
 import Constants from './Constants';
 import { EmptyObject } from '../types/EmptyObject';
+import { FileType } from '../types/FileType';
 import { HandleErrorParams } from '../types/Error';
-import { ServerOptions } from 'ws';
 import { StorageType } from '../types/Storage';
 import type { WorkerChoiceStrategy } from 'poolifier';
+import WorkerConstants from '../worker/WorkerConstants';
 import { WorkerProcessType } from '../types/Worker';
 import chalk from 'chalk';
 import fs from 'fs';
 import path from 'path';
 
 export default class Configuration {
-  private static configurationFilePath = path.join(
+  private static configurationFile = path.join(
     path.resolve(__dirname, '../'),
     'assets',
     'config.json'
@@ -40,44 +42,46 @@ export default class Configuration {
     // Read conf
     return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logStatisticsInterval')
       ? Configuration.getConfig().logStatisticsInterval
-      : 60;
+      : Constants.DEFAULT_LOG_STATISTICS_INTERVAL;
   }
 
-  static getUIWebSocketServer(): UIWebSocketServerConfiguration {
+  static getUIServer(): UIServerConfiguration {
+    if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'uiWebSocketServer')) {
+      console.error(
+        chalk`{green ${Configuration.logPrefix()}} {red Deprecated configuration section 'uiWebSocketServer' usage. Use 'uiServer' instead}`
+      );
+    }
     let options: ServerOptions = {
       host: Constants.DEFAULT_UI_WEBSOCKET_SERVER_HOST,
       port: Constants.DEFAULT_UI_WEBSOCKET_SERVER_PORT,
     };
-    let uiWebSocketServerConfiguration: UIWebSocketServerConfiguration = {
+    let uiServerConfiguration: UIServerConfiguration = {
       enabled: true,
       options,
     };
-    if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'uiWebSocketServer')) {
-      if (
-        Configuration.objectHasOwnProperty(Configuration.getConfig().uiWebSocketServer, 'options')
-      ) {
+    if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'uiServer')) {
+      if (Configuration.objectHasOwnProperty(Configuration.getConfig().uiServer, 'options')) {
         options = {
           ...options,
           ...(Configuration.objectHasOwnProperty(
-            Configuration.getConfig().uiWebSocketServer.options,
+            Configuration.getConfig().uiServer.options,
             'host'
-          ) && { host: Configuration.getConfig().uiWebSocketServer.options.host }),
+          ) && { host: Configuration.getConfig().uiServer.options.host }),
           ...(Configuration.objectHasOwnProperty(
-            Configuration.getConfig().uiWebSocketServer.options,
+            Configuration.getConfig().uiServer.options,
             'port'
-          ) && { port: Configuration.getConfig().uiWebSocketServer.options.port }),
+          ) && { port: Configuration.getConfig().uiServer.options.port }),
         };
       }
-      uiWebSocketServerConfiguration = {
-        ...uiWebSocketServerConfiguration,
-        ...(Configuration.objectHasOwnProperty(
-          Configuration.getConfig().uiWebSocketServer,
-          'enabled'
-        ) && { enabled: Configuration.getConfig().uiWebSocketServer.enabled }),
+      uiServerConfiguration = {
+        ...uiServerConfiguration,
+        ...(Configuration.objectHasOwnProperty(Configuration.getConfig().uiServer, 'enabled') && {
+          enabled: Configuration.getConfig().uiServer.enabled,
+        }),
         options,
       };
     }
-    return uiWebSocketServerConfiguration;
+    return uiServerConfiguration;
   }
 
   static getPerformanceStorage(): StorageConfiguration {
@@ -158,9 +162,9 @@ export default class Configuration {
 
   static getWorkerProcess(): WorkerProcessType {
     Configuration.warnDeprecatedConfigurationKey(
-      'useWorkerPool;',
+      'useWorkerPool',
       null,
-      "Use 'workerProcess' to define the type of worker process to use instead"
+      "Use 'workerProcess' to define the type of worker process model to use instead"
     );
     return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerProcess')
       ? Configuration.getConfig().workerProcess
@@ -170,19 +174,19 @@ export default class Configuration {
   static getWorkerStartDelay(): number {
     return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerStartDelay')
       ? Configuration.getConfig().workerStartDelay
-      : Constants.WORKER_START_DELAY;
+      : WorkerConstants.DEFAULT_WORKER_START_DELAY;
   }
 
   static getElementStartDelay(): number {
     return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'elementStartDelay')
       ? Configuration.getConfig().elementStartDelay
-      : Constants.ELEMENT_START_DELAY;
+      : WorkerConstants.DEFAULT_ELEMENT_START_DELAY;
   }
 
   static getWorkerPoolMinSize(): number {
     return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerPoolMinSize')
       ? Configuration.getConfig().workerPoolMinSize
-      : Constants.DEFAULT_WORKER_POOL_MIN_SIZE;
+      : WorkerConstants.DEFAULT_POOL_MIN_SIZE;
   }
 
   static getWorkerPoolMaxSize(): number {
@@ -193,7 +197,7 @@ export default class Configuration {
     );
     return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerPoolMaxSize')
       ? Configuration.getConfig().workerPoolMaxSize
-      : Constants.DEFAULT_WORKER_POOL_MAX_SIZE;
+      : WorkerConstants.DEFAULT_POOL_MAX_SIZE;
   }
 
   static getWorkerPoolStrategy(): WorkerChoiceStrategy {
@@ -206,7 +210,7 @@ export default class Configuration {
       'chargingStationsPerWorker'
     )
       ? Configuration.getConfig().chargingStationsPerWorker
-      : Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER;
+      : WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER;
   }
 
   static getLogConsole(): boolean {
@@ -295,11 +299,12 @@ export default class Configuration {
     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])
+      !Configuration.isUndefined(
+        (Configuration.getConfig()[sectionName] as Record<string, unknown>)[key]
+      )
     ) {
       console.error(
         chalk`{green ${Configuration.logPrefix()}} {red Deprecated configuration key '${key}' usage in section '${sectionName}'${
@@ -320,14 +325,14 @@ export default class Configuration {
     if (!Configuration.configuration) {
       try {
         Configuration.configuration = JSON.parse(
-          fs.readFileSync(Configuration.configurationFilePath, 'utf8')
+          fs.readFileSync(Configuration.configurationFile, 'utf8')
         ) as ConfigurationData;
       } catch (error) {
         Configuration.handleFileException(
           Configuration.logPrefix(),
-          'Configuration',
-          Configuration.configurationFilePath,
-          error
+          FileType.Configuration,
+          Configuration.configurationFile,
+          error as NodeJS.ErrnoException
         );
       }
       if (!Configuration.configurationFileWatcher) {
@@ -339,7 +344,7 @@ export default class Configuration {
 
   private static getConfigurationFileWatcher(): fs.FSWatcher {
     try {
-      return fs.watch(Configuration.configurationFilePath, (event, filename): void => {
+      return fs.watch(Configuration.configurationFile, (event, filename): void => {
         if (filename && event === 'change') {
           // Nullify to force configuration file reading
           Configuration.configuration = null;
@@ -353,9 +358,9 @@ export default class Configuration {
     } catch (error) {
       Configuration.handleFileException(
         Configuration.logPrefix(),
-        'Configuration',
-        Configuration.configurationFilePath,
-        error as Error
+        FileType.Configuration,
+        Configuration.configurationFile,
+        error as NodeJS.ErrnoException
       );
     }
   }
@@ -385,7 +390,7 @@ export default class Configuration {
 
   private static handleFileException(
     logPrefix: string,
-    fileType: string,
+    fileType: FileType,
     filePath: string,
     error: NodeJS.ErrnoException,
     params: HandleErrorParams<EmptyObject> = { throwError: true }