build: rollup -> esbuild
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / ui-services / AbstractUIService.ts
index bfe3751c4c20b73f1dd903b3c19458fca930f0ce..20531c6f37ce4002d48ac5fe255628086ab5c0e2 100644 (file)
@@ -19,34 +19,40 @@ import type { AbstractUIServer } from '../AbstractUIServer';
 const moduleName = 'AbstractUIService';
 
 export abstract class AbstractUIService {
-  protected static readonly ProcedureNameToBroadCastChannelProcedureNameMap: Omit<
-    Record<ProcedureName, BroadcastChannelProcedureName>,
-    | ProcedureName.START_SIMULATOR
-    | ProcedureName.STOP_SIMULATOR
-    | ProcedureName.LIST_CHARGING_STATIONS
-  > = {
-    [ProcedureName.START_CHARGING_STATION]: BroadcastChannelProcedureName.START_CHARGING_STATION,
-    [ProcedureName.STOP_CHARGING_STATION]: BroadcastChannelProcedureName.STOP_CHARGING_STATION,
-    [ProcedureName.CLOSE_CONNECTION]: BroadcastChannelProcedureName.CLOSE_CONNECTION,
-    [ProcedureName.OPEN_CONNECTION]: BroadcastChannelProcedureName.OPEN_CONNECTION,
-    [ProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR]:
+  protected static readonly ProcedureNameToBroadCastChannelProcedureNameMapping = new Map<
+    ProcedureName,
+    BroadcastChannelProcedureName
+  >([
+    [ProcedureName.START_CHARGING_STATION, BroadcastChannelProcedureName.START_CHARGING_STATION],
+    [ProcedureName.STOP_CHARGING_STATION, BroadcastChannelProcedureName.STOP_CHARGING_STATION],
+    [ProcedureName.CLOSE_CONNECTION, BroadcastChannelProcedureName.CLOSE_CONNECTION],
+    [ProcedureName.OPEN_CONNECTION, BroadcastChannelProcedureName.OPEN_CONNECTION],
+    [
+      ProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR,
       BroadcastChannelProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR,
-    [ProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR]:
+    ],
+    [
+      ProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR,
       BroadcastChannelProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR,
-    [ProcedureName.SET_SUPERVISION_URL]: BroadcastChannelProcedureName.SET_SUPERVISION_URL,
-    [ProcedureName.START_TRANSACTION]: BroadcastChannelProcedureName.START_TRANSACTION,
-    [ProcedureName.STOP_TRANSACTION]: BroadcastChannelProcedureName.STOP_TRANSACTION,
-    [ProcedureName.AUTHORIZE]: BroadcastChannelProcedureName.AUTHORIZE,
-    [ProcedureName.BOOT_NOTIFICATION]: BroadcastChannelProcedureName.BOOT_NOTIFICATION,
-    [ProcedureName.STATUS_NOTIFICATION]: BroadcastChannelProcedureName.STATUS_NOTIFICATION,
-    [ProcedureName.HEARTBEAT]: BroadcastChannelProcedureName.HEARTBEAT,
-    [ProcedureName.METER_VALUES]: BroadcastChannelProcedureName.METER_VALUES,
-    [ProcedureName.DATA_TRANSFER]: BroadcastChannelProcedureName.DATA_TRANSFER,
-    [ProcedureName.DIAGNOSTICS_STATUS_NOTIFICATION]:
+    ],
+    [ProcedureName.SET_SUPERVISION_URL, BroadcastChannelProcedureName.SET_SUPERVISION_URL],
+    [ProcedureName.START_TRANSACTION, BroadcastChannelProcedureName.START_TRANSACTION],
+    [ProcedureName.STOP_TRANSACTION, BroadcastChannelProcedureName.STOP_TRANSACTION],
+    [ProcedureName.AUTHORIZE, BroadcastChannelProcedureName.AUTHORIZE],
+    [ProcedureName.BOOT_NOTIFICATION, BroadcastChannelProcedureName.BOOT_NOTIFICATION],
+    [ProcedureName.STATUS_NOTIFICATION, BroadcastChannelProcedureName.STATUS_NOTIFICATION],
+    [ProcedureName.HEARTBEAT, BroadcastChannelProcedureName.HEARTBEAT],
+    [ProcedureName.METER_VALUES, BroadcastChannelProcedureName.METER_VALUES],
+    [ProcedureName.DATA_TRANSFER, BroadcastChannelProcedureName.DATA_TRANSFER],
+    [
+      ProcedureName.DIAGNOSTICS_STATUS_NOTIFICATION,
       BroadcastChannelProcedureName.DIAGNOSTICS_STATUS_NOTIFICATION,
-    [ProcedureName.FIRMWARE_STATUS_NOTIFICATION]:
+    ],
+    [
+      ProcedureName.FIRMWARE_STATUS_NOTIFICATION,
       BroadcastChannelProcedureName.FIRMWARE_STATUS_NOTIFICATION,
-  };
+    ],
+  ]);
 
   protected readonly requestHandlers: Map<ProcedureName, ProtocolRequestHandler>;
   private readonly version: ProtocolVersion;
@@ -67,8 +73,8 @@ export abstract class AbstractUIService {
   }
 
   public async requestHandler(request: ProtocolRequest): Promise<ProtocolResponse | undefined> {
-    let messageId: string;
-    let command: ProcedureName;
+    let messageId: string | undefined;
+    let command: ProcedureName | undefined;
     let requestPayload: RequestPayload | undefined;
     let responsePayload: ResponsePayload | undefined;
     try {
@@ -78,14 +84,18 @@ export abstract class AbstractUIService {
         throw new BaseError(
           `${command} is not implemented to handle message payload ${JSON.stringify(
             requestPayload,
-            null,
-            2
-          )}`
+            undefined,
+            2,
+          )}`,
         );
       }
 
       // Call the request handler to build the response payload
-      responsePayload = await this.requestHandlers.get(command)(messageId, command, requestPayload);
+      responsePayload = await this.requestHandlers.get(command)!(
+        messageId,
+        command,
+        requestPayload,
+      );
     } catch (error) {
       // Log
       logger.error(`${this.logPrefix(moduleName, 'requestHandler')} Handle request error:`, error);
@@ -101,17 +111,17 @@ export abstract class AbstractUIService {
       };
     }
     if (!isNullOrUndefined(responsePayload)) {
-      return this.uiServer.buildProtocolResponse(messageId, responsePayload);
+      return this.uiServer.buildProtocolResponse(messageId!, responsePayload!);
     }
   }
 
   // public sendRequest(
   //   messageId: string,
   //   procedureName: ProcedureName,
-  //   requestPayload: RequestPayload
+  //   requestPayload: RequestPayload,
   // ): void {
   //   this.uiServer.sendRequest(
-  //     this.uiServer.buildProtocolRequest(messageId, procedureName, requestPayload)
+  //     this.uiServer.buildProtocolRequest(messageId, procedureName, requestPayload),
   //   );
   // }
 
@@ -136,39 +146,37 @@ export abstract class AbstractUIService {
   protected handleProtocolRequest(
     uuid: string,
     procedureName: ProcedureName,
-    payload: RequestPayload
+    payload: RequestPayload,
   ): void {
     this.sendBroadcastChannelRequest(
       uuid,
-      AbstractUIService.ProcedureNameToBroadCastChannelProcedureNameMap[
-        procedureName
-      ] as BroadcastChannelProcedureName,
-      payload
+      AbstractUIService.ProcedureNameToBroadCastChannelProcedureNameMapping.get(procedureName)!,
+      payload,
     );
   }
 
   private sendBroadcastChannelRequest(
     uuid: string,
     procedureName: BroadcastChannelProcedureName,
-    payload: BroadcastChannelRequestPayload
+    payload: BroadcastChannelRequestPayload,
   ): void {
     if (isNotEmptyArray(payload.hashIds)) {
       payload.hashIds = payload.hashIds
-        .filter((hashId) => !isNullOrUndefined(hashId))
-        .map((hashId) => {
+        ?.filter((hashId) => !isNullOrUndefined(hashId))
+        ?.map((hashId) => {
           if (this.uiServer.chargingStations.has(hashId) === true) {
             return hashId;
           }
           logger.warn(
             `${this.logPrefix(
               moduleName,
-              'sendBroadcastChannelRequest'
-            )} Charging station with hashId '${hashId}' not found`
+              'sendBroadcastChannelRequest',
+            )} Charging station with hashId '${hashId}' not found`,
           );
-        });
+        }) as string[];
     }
     const expectedNumberOfResponses = isNotEmptyArray(payload.hashIds)
-      ? payload.hashIds.length
+      ? payload.hashIds!.length
       : this.uiServer.chargingStations.size;
     this.uiServiceWorkerBroadcastChannel.sendRequest([uuid, procedureName, payload]);
     this.broadcastChannelRequests.set(uuid, expectedNumberOfResponses);
@@ -185,7 +193,7 @@ export abstract class AbstractUIService {
     try {
       await Bootstrap.getInstance().start();
       return { status: ResponseStatus.SUCCESS };
-    } catch (error) {
+    } catch {
       return { status: ResponseStatus.FAILURE };
     }
   }
@@ -194,7 +202,7 @@ export abstract class AbstractUIService {
     try {
       await Bootstrap.getInstance().stop();
       return { status: ResponseStatus.SUCCESS };
-    } catch (error) {
+    } catch {
       return { status: ResponseStatus.FAILURE };
     }
   }