UI Server: Add a unique request handler for procedure forwarded to charging
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 8 Sep 2022 18:56:07 +0000 (20:56 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 8 Sep 2022 18:56:07 +0000 (20:56 +0200)
stations via BC

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStationWorkerBroadcastChannel.ts
src/charging-station/ui-server/ui-services/AbstractUIService.ts
src/charging-station/ui-server/ui-services/UIService001.ts
src/types/UIProtocol.ts
src/types/WorkerBroadcastChannel.ts
src/ui/web/src/types/UIProtocol.ts

index 154e1afb14bbb8631fdb570c3a95c2888e4516f2..a67b9423649b1e0f97003bcf909bc67ff6b6e319 100644 (file)
@@ -146,7 +146,6 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
       );
       return;
     }
-
     let responsePayload: BroadcastChannelResponsePayload;
     let commandResponse: CommandResponse | void;
     try {
@@ -157,19 +156,19 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
           status: ResponseStatus.SUCCESS,
         };
       } else {
-        const responseStatus = this.commandResponseToResponseStatus(
+        const commandResponseStatus = this.commandResponseToResponseStatus(
           command,
           commandResponse as CommandResponse
         );
-        if (responseStatus === ResponseStatus.SUCCESS) {
+        if (commandResponseStatus === ResponseStatus.SUCCESS) {
           responsePayload = {
             hashId: this.chargingStation.stationInfo.hashId,
-            status: responseStatus,
+            status: commandResponseStatus,
           };
         } else {
           responsePayload = {
             hashId: this.chargingStation.stationInfo.hashId,
-            status: responseStatus,
+            status: commandResponseStatus,
             command,
             requestPayload,
             commandResponse: commandResponse as CommandResponse,
index a393986e8f3b2ae7f9db6f741382440183810ad0..356e908ca30d5688de229af443028eff56ef4e1d 100644 (file)
@@ -59,7 +59,7 @@ export default abstract class AbstractUIService {
       }
 
       // Call the request handler to build the response payload
-      responsePayload = await this.requestHandlers.get(command)(messageId, requestPayload);
+      responsePayload = await this.requestHandlers.get(command)(messageId, command, requestPayload);
     } catch (error) {
       // Log
       logger.error(`${this.logPrefix(moduleName, 'messageHandler')} Handle request error:`, error);
index 4be6a2f2b16f1903a78c0941016a65d1ce594eb5..82d0a9534adab7972dc615359ebc0e9c99c50cd4 100644 (file)
@@ -8,120 +8,85 @@ import { BroadcastChannelProcedureName } from '../../../types/WorkerBroadcastCha
 import type { AbstractUIServer } from '../AbstractUIServer';
 import AbstractUIService from './AbstractUIService';
 
+const ProcedureNameToBroadCastChannelProcedureNameMap: Omit<
+  Record<ProcedureName, BroadcastChannelProcedureName>,
+  'startSimulator' | 'stopSimulator' | 'listChargingStations'
+> = {
+  [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]:
+    BroadcastChannelProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR,
+  [ProcedureName.START_TRANSACTION]: BroadcastChannelProcedureName.START_TRANSACTION,
+  [ProcedureName.STOP_TRANSACTION]: BroadcastChannelProcedureName.STOP_TRANSACTION,
+  [ProcedureName.AUTHORIZE]: BroadcastChannelProcedureName.AUTHORIZE,
+  [ProcedureName.STATUS_NOTIFICATION]: BroadcastChannelProcedureName.STATUS_NOTIFICATION,
+  [ProcedureName.HEARTBEAT]: BroadcastChannelProcedureName.HEARTBEAT,
+};
+
 export default class UIService001 extends AbstractUIService {
   constructor(uiServer: AbstractUIServer) {
     super(uiServer, ProtocolVersion['0.0.1']);
     this.requestHandlers.set(
       ProcedureName.START_CHARGING_STATION,
-      this.handleStartChargingStation.bind(this) as ProtocolRequestHandler
+      this.handleProtocolRequest.bind(this) as ProtocolRequestHandler
     );
     this.requestHandlers.set(
       ProcedureName.STOP_CHARGING_STATION,
-      this.handleStopChargingStation.bind(this) as ProtocolRequestHandler
+      this.handleProtocolRequest.bind(this) as ProtocolRequestHandler
     );
     this.requestHandlers.set(
       ProcedureName.OPEN_CONNECTION,
-      this.handleOpenConnection.bind(this) as ProtocolRequestHandler
+      this.handleProtocolRequest.bind(this) as ProtocolRequestHandler
     );
     this.requestHandlers.set(
       ProcedureName.CLOSE_CONNECTION,
-      this.handleCloseConnection.bind(this) as ProtocolRequestHandler
+      this.handleProtocolRequest.bind(this) as ProtocolRequestHandler
     );
     this.requestHandlers.set(
       ProcedureName.START_TRANSACTION,
-      this.handleStartTransaction.bind(this) as ProtocolRequestHandler
+      this.handleProtocolRequest.bind(this) as ProtocolRequestHandler
     );
     this.requestHandlers.set(
       ProcedureName.STOP_TRANSACTION,
-      this.handleStopTransaction.bind(this) as ProtocolRequestHandler
+      this.handleProtocolRequest.bind(this) as ProtocolRequestHandler
     );
     this.requestHandlers.set(
       ProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR,
-      this.handleStartAutomaticTransactionGenerator.bind(this) as ProtocolRequestHandler
+      this.handleProtocolRequest.bind(this) as ProtocolRequestHandler
     );
     this.requestHandlers.set(
       ProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR,
-      this.handleStopAutomaticTransactionGenerator.bind(this) as ProtocolRequestHandler
+      this.handleProtocolRequest.bind(this) as ProtocolRequestHandler
     );
     this.requestHandlers.set(
       ProcedureName.AUTHORIZE,
-      this.handleAuthorize.bind(this) as ProtocolRequestHandler
+      this.handleProtocolRequest.bind(this) as ProtocolRequestHandler
     );
     this.requestHandlers.set(
       ProcedureName.STATUS_NOTIFICATION,
-      this.handleStatusNotification.bind(this) as ProtocolRequestHandler
+      this.handleProtocolRequest.bind(this) as ProtocolRequestHandler
     );
     this.requestHandlers.set(
       ProcedureName.HEARTBEAT,
-      this.handleHeartbeat.bind(this) as ProtocolRequestHandler
+      this.handleProtocolRequest.bind(this) as ProtocolRequestHandler
     );
   }
 
-  private handleStartChargingStation(uuid: string, payload: RequestPayload): void {
+  private handleProtocolRequest(
+    uuid: string,
+    procedureName: ProcedureName,
+    payload: RequestPayload
+  ): void {
     this.sendBroadcastChannelRequest(
       uuid,
-      BroadcastChannelProcedureName.START_CHARGING_STATION,
+      ProcedureNameToBroadCastChannelProcedureNameMap[
+        procedureName
+      ] as BroadcastChannelProcedureName,
       payload
     );
   }
-
-  private handleStopChargingStation(uuid: string, payload: RequestPayload): void {
-    this.sendBroadcastChannelRequest(
-      uuid,
-      BroadcastChannelProcedureName.STOP_CHARGING_STATION,
-      payload
-    );
-  }
-
-  private handleOpenConnection(uuid: string, payload: RequestPayload): void {
-    this.sendBroadcastChannelRequest(uuid, BroadcastChannelProcedureName.OPEN_CONNECTION, payload);
-  }
-
-  private handleCloseConnection(uuid: string, payload: RequestPayload): void {
-    this.sendBroadcastChannelRequest(uuid, BroadcastChannelProcedureName.CLOSE_CONNECTION, payload);
-  }
-
-  private handleStartTransaction(uuid: string, payload: RequestPayload): void {
-    this.sendBroadcastChannelRequest(
-      uuid,
-      BroadcastChannelProcedureName.START_TRANSACTION,
-      payload
-    );
-  }
-
-  private handleStopTransaction(uuid: string, payload: RequestPayload): void {
-    this.sendBroadcastChannelRequest(uuid, BroadcastChannelProcedureName.STOP_TRANSACTION, payload);
-  }
-
-  private handleStartAutomaticTransactionGenerator(uuid: string, payload: RequestPayload): void {
-    this.sendBroadcastChannelRequest(
-      uuid,
-      BroadcastChannelProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR,
-      payload
-    );
-  }
-
-  private handleStopAutomaticTransactionGenerator(uuid: string, payload: RequestPayload): void {
-    this.sendBroadcastChannelRequest(
-      uuid,
-      BroadcastChannelProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR,
-      payload
-    );
-  }
-
-  private handleAuthorize(uuid: string, payload: RequestPayload): void {
-    this.sendBroadcastChannelRequest(uuid, BroadcastChannelProcedureName.AUTHORIZE, payload);
-  }
-
-  private handleStatusNotification(uuid: string, payload: RequestPayload): void {
-    this.sendBroadcastChannelRequest(
-      uuid,
-      BroadcastChannelProcedureName.STATUS_NOTIFICATION,
-      payload
-    );
-  }
-
-  private handleHeartbeat(uuid: string, payload: RequestPayload): void {
-    this.sendBroadcastChannelRequest(uuid, BroadcastChannelProcedureName.HEARTBEAT, payload);
-  }
 }
index 75685141bb945eb12569aaaca795462eea23156d..6468290d59b8cd16c61f2ed990a6df44fee0832a 100644 (file)
@@ -23,21 +23,22 @@ export type ProtocolResponse = [string, ResponsePayload];
 
 export type ProtocolRequestHandler = (
   uuid?: string,
+  procedureName?: ProcedureName,
   payload?: RequestPayload
 ) => undefined | Promise<undefined> | ResponsePayload | Promise<ResponsePayload>;
 
 export enum ProcedureName {
+  START_SIMULATOR = 'startSimulator',
+  STOP_SIMULATOR = 'stopSimulator',
   LIST_CHARGING_STATIONS = 'listChargingStations',
   START_CHARGING_STATION = 'startChargingStation',
   STOP_CHARGING_STATION = 'stopChargingStation',
-  START_SIMULATOR = 'startSimulator',
-  STOP_SIMULATOR = 'stopSimulator',
   OPEN_CONNECTION = 'openConnection',
   CLOSE_CONNECTION = 'closeConnection',
-  START_TRANSACTION = 'startTransaction',
-  STOP_TRANSACTION = 'stopTransaction',
   START_AUTOMATIC_TRANSACTION_GENERATOR = 'startAutomaticTransactionGenerator',
   STOP_AUTOMATIC_TRANSACTION_GENERATOR = 'stopAutomaticTransactionGenerator',
+  START_TRANSACTION = 'startTransaction',
+  STOP_TRANSACTION = 'stopTransaction',
   AUTHORIZE = 'authorize',
   STATUS_NOTIFICATION = 'statusNotification',
   HEARTBEAT = 'heartbeat',
index be59789bf0882974a9ddf60a2eb4cd6e75640ae4..e7017356162205f91f4152a1862923cd7f06a4a0 100644 (file)
@@ -12,10 +12,10 @@ export enum BroadcastChannelProcedureName {
   STOP_CHARGING_STATION = 'stopChargingStation',
   OPEN_CONNECTION = 'openConnection',
   CLOSE_CONNECTION = 'closeConnection',
-  START_TRANSACTION = 'startTransaction',
-  STOP_TRANSACTION = 'stopTransaction',
   START_AUTOMATIC_TRANSACTION_GENERATOR = 'startAutomaticTransactionGenerator',
   STOP_AUTOMATIC_TRANSACTION_GENERATOR = 'stopAutomaticTransactionGenerator',
+  START_TRANSACTION = 'startTransaction',
+  STOP_TRANSACTION = 'stopTransaction',
   AUTHORIZE = 'authorize',
   STATUS_NOTIFICATION = 'statusNotification',
   HEARTBEAT = 'heartbeat',
index 5921315c546d702d2dbfc6ab8878b1b9b86e3c99..443e2352e05b82dca5f8bfdd16b331da70911b8a 100644 (file)
@@ -28,10 +28,10 @@ export enum ProcedureName {
   STOP_CHARGING_STATION = 'stopChargingStation',
   OPEN_CONNECTION = 'openConnection',
   CLOSE_CONNECTION = 'closeConnection',
-  START_TRANSACTION = 'startTransaction',
-  STOP_TRANSACTION = 'stopTransaction',
   START_AUTOMATIC_TRANSACTION_GENERATOR = 'startAutomaticTransactionGenerator',
   STOP_AUTOMATIC_TRANSACTION_GENERATOR = 'stopAutomaticTransactionGenerator',
+  START_TRANSACTION = 'startTransaction',
+  STOP_TRANSACTION = 'stopTransaction',
 }
 
 export interface RequestPayload extends JsonObject {