refactor(simulator): factor out common helpers
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / ui-services / AbstractUIService.ts
index 62249549c268b11b383dd151f91d23429a70fb4b..71b057abe4a2f7369c1376d97ffcc55046eb0e89 100644 (file)
@@ -1,7 +1,7 @@
-import BaseError from '../../../exception/BaseError';
-import type OCPPError from '../../../exception/OCPPError';
-import { Bootstrap } from '../../../internal';
+import { BaseError, type OCPPError } from '../../../exception';
 import {
+  BroadcastChannelProcedureName,
+  type BroadcastChannelRequestPayload,
   ProcedureName,
   type ProtocolRequest,
   type ProtocolRequestHandler,
@@ -9,19 +9,15 @@ import {
   type RequestPayload,
   type ResponsePayload,
   ResponseStatus,
-} from '../../../types/UIProtocol';
-import {
-  BroadcastChannelProcedureName,
-  type BroadcastChannelRequestPayload,
-} from '../../../types/WorkerBroadcastChannel';
-import logger from '../../../utils/Logger';
-import Utils from '../../../utils/Utils';
-import UIServiceWorkerBroadcastChannel from '../../UIServiceWorkerBroadcastChannel';
+} from '../../../types';
+import { Utils, logger } from '../../../utils';
+import { Bootstrap } from '../../Bootstrap';
+import { UIServiceWorkerBroadcastChannel } from '../../broadcast-channel/UIServiceWorkerBroadcastChannel';
 import type { AbstractUIServer } from '../AbstractUIServer';
 
 const moduleName = 'AbstractUIService';
 
-export default abstract class AbstractUIService {
+export abstract class AbstractUIService {
   protected static readonly ProcedureNameToBroadCastChannelProcedureNameMap: Omit<
     Record<ProcedureName, BroadcastChannelProcedureName>,
     | ProcedureName.START_SIMULATOR
@@ -36,6 +32,7 @@ export default abstract class AbstractUIService {
       BroadcastChannelProcedureName.START_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,
@@ -103,7 +100,7 @@ export default abstract class AbstractUIService {
       };
     } finally {
       // Send response for payload not forwarded to broadcast channel
-      if (responsePayload !== undefined) {
+      if (!Utils.isNullOrUndefined(responsePayload)) {
         this.sendResponse(messageId, responsePayload);
       }
     }
@@ -156,6 +153,7 @@ export default abstract class AbstractUIService {
   ): void {
     if (Utils.isNotEmptyArray(payload.hashIds)) {
       payload.hashIds = payload.hashIds
+        .filter((hashId) => !Utils.isNullOrUndefined(hashId))
         .map((hashId) => {
           if (this.uiServer.chargingStations.has(hashId) === true) {
             return hashId;
@@ -166,8 +164,7 @@ export default abstract class AbstractUIService {
               'sendBroadcastChannelRequest'
             )} Charging station with hashId '${hashId}' not found`
           );
-        })
-        .filter((hashId) => hashId !== undefined);
+        });
     }
     const expectedNumberOfResponses = Utils.isNotEmptyArray(payload.hashIds)
       ? payload.hashIds.length