Unify request and response handler naming
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 23 Aug 2022 15:01:18 +0000 (17:01 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 23 Aug 2022 15:01:18 +0000 (17:01 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStationWorkerBroadcastChannel.ts
src/charging-station/WorkerBroadcastChannel.ts
src/charging-station/ui-server/AbstractUIServer.ts
src/charging-station/ui-server/UIWebSocketServer.ts
src/charging-station/ui-server/ui-services/AbstractUIService.ts
src/charging-station/ui-server/ui-services/UIService001.ts

index 7212f09d0cf7b6832f494ddf0fb0925a47bac440..6d38f06c0f12539bccd17daa78c47492efc6ac45 100644 (file)
@@ -23,10 +23,10 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
   constructor(chargingStation: ChargingStation) {
     super();
     this.chargingStation = chargingStation;
-    this.onmessage = this.handleRequest.bind(this) as (message: MessageEvent) => void;
+    this.onmessage = this.requestHandler.bind(this) as (message: MessageEvent) => void;
   }
 
-  private async handleRequest(messageEvent: MessageEvent): Promise<void> {
+  private async requestHandler(messageEvent: MessageEvent): Promise<void> {
     const [, command, payload] = messageEvent.data as BroadcastChannelRequest;
 
     if (payload.hashId !== this.chargingStation.hashId) {
index d1d430c3a0c39230aef0c00dd5de5660b25e42a0..2099d695ab683e75d315151e02c9c5914eb6cedd 100644 (file)
@@ -1,6 +1,6 @@
 import { BroadcastChannel } from 'worker_threads';
 
-import { BroadcastChannelRequest } from '../types/WorkerBroadcastChannel';
+import { BroadcastChannelRequest, BroadcastChannelResponse } from '../types/WorkerBroadcastChannel';
 
 export default class WorkerBroadcastChannel extends BroadcastChannel {
   constructor() {
@@ -10,4 +10,8 @@ export default class WorkerBroadcastChannel extends BroadcastChannel {
   public sendRequest(request: BroadcastChannelRequest): void {
     this.postMessage(request);
   }
+
+  public sendResponse(response: BroadcastChannelResponse): void {
+    this.postMessage(response);
+  }
 }
index 152749e5ffc14ac3114e2dd081d457fff37ab2df..e0b1207106e34ab1755a68cf9d3974d27a8d3088 100644 (file)
@@ -18,6 +18,7 @@ export abstract class AbstractUIServer {
 
   public abstract start(): void;
   public abstract stop(): void;
-  public abstract sendResponse(message: string): void;
+  public abstract sendRequest(request: string): void;
+  public abstract sendResponse(response: string): void;
   public abstract logPrefix(modName?: string, methodName?: string): string;
 }
index d75b9ab8e860a6748b26077c88f4913a06c33a48..66980f610f08e86a4f111ded7db26a9bab7ec41e 100644 (file)
@@ -31,7 +31,7 @@ export default class UIWebSocketServer extends AbstractUIServer {
       socket.on('message', (messageData) => {
         this.uiServices
           .get(version)
-          .messageHandler(messageData)
+          .requestHandler(messageData)
           .catch((error) => {
             logger.error(
               `${this.logPrefix(
@@ -55,8 +55,12 @@ export default class UIWebSocketServer extends AbstractUIServer {
     this.server.close();
   }
 
-  public sendResponse(message: string): void {
-    this.broadcastToClients(message);
+  public sendRequest(request: string): void {
+    this.broadcastToClients(request);
+  }
+
+  public sendResponse(response: string): void {
+    this.broadcastToClients(response);
   }
 
   public logPrefix(modName?: string, methodName?: string): string {
index 2e35299cace8ca77d8bf5c04ec102a155e84343d..30ad449a33775b4ed49d970cf2a419c5ee23a458 100644 (file)
@@ -23,13 +23,13 @@ const moduleName = 'AbstractUIService';
 export default abstract class AbstractUIService {
   protected readonly version: ProtocolVersion;
   protected readonly uiServer: AbstractUIServer;
-  protected readonly messageHandlers: Map<ProcedureName, ProtocolRequestHandler>;
+  protected readonly requestHandlers: Map<ProcedureName, ProtocolRequestHandler>;
   protected workerBroadcastChannel: WorkerBroadcastChannel;
 
   constructor(uiServer: AbstractUIServer, version: ProtocolVersion) {
     this.version = version;
     this.uiServer = uiServer;
-    this.messageHandlers = new Map<ProcedureName, ProtocolRequestHandler>([
+    this.requestHandlers = new Map<ProcedureName, ProtocolRequestHandler>([
       [ProcedureName.LIST_CHARGING_STATIONS, this.handleListChargingStations.bind(this)],
       [ProcedureName.START_SIMULATOR, this.handleStartSimulator.bind(this)],
       [ProcedureName.STOP_SIMULATOR, this.handleStopSimulator.bind(this)],
@@ -37,7 +37,7 @@ export default abstract class AbstractUIService {
     this.workerBroadcastChannel = new WorkerBroadcastChannel();
   }
 
-  public async messageHandler(request: RawData): Promise<void> {
+  public async requestHandler(request: RawData): Promise<void> {
     let messageId: string;
     let command: ProcedureName;
     let requestPayload: RequestPayload;
@@ -45,7 +45,7 @@ export default abstract class AbstractUIService {
     try {
       [messageId, command, requestPayload] = this.dataValidation(request);
 
-      if (this.messageHandlers.has(command) === false) {
+      if (this.requestHandlers.has(command) === false) {
         throw new BaseError(
           `${command} is not implemented to handle message payload ${JSON.stringify(
             requestPayload,
@@ -56,7 +56,7 @@ export default abstract class AbstractUIService {
       }
 
       // Call the message handler to build the response payload
-      responsePayload = await this.messageHandlers.get(command)(messageId, requestPayload);
+      responsePayload = await this.requestHandlers.get(command)(messageId, requestPayload);
     } catch (error) {
       // Log
       logger.error(
@@ -80,6 +80,14 @@ export default abstract class AbstractUIService {
     this.uiServer.sendResponse(this.buildProtocolResponse(messageId, responsePayload));
   }
 
+  protected buildProtocolRequest(
+    messageId: string,
+    procedureName: ProcedureName,
+    payload: RequestPayload
+  ): string {
+    return JSON.stringify([messageId, procedureName, payload] as ProtocolRequest);
+  }
+
   protected buildProtocolResponse(messageId: string, payload: ResponsePayload): string {
     return JSON.stringify([messageId, payload] as ProtocolResponse);
   }
index 1f926c939f7ce2703e58388d6117ebd210844a93..5c9b7dc4dc4525dd177cb1040879d1f281c791c3 100644 (file)
@@ -16,19 +16,19 @@ import AbstractUIService from './AbstractUIService';
 export default class UIService001 extends AbstractUIService {
   constructor(uiServer: AbstractUIServer) {
     super(uiServer, ProtocolVersion['0.0.1']);
-    this.messageHandlers.set(
+    this.requestHandlers.set(
       ProcedureName.START_TRANSACTION,
       this.handleStartTransaction.bind(this) as ProtocolRequestHandler
     );
-    this.messageHandlers.set(
+    this.requestHandlers.set(
       ProcedureName.STOP_TRANSACTION,
       this.handleStopTransaction.bind(this) as ProtocolRequestHandler
     );
-    this.messageHandlers.set(
+    this.requestHandlers.set(
       ProcedureName.START_CHARGING_STATION,
       this.handleStartChargingStation.bind(this) as ProtocolRequestHandler
     );
-    this.messageHandlers.set(
+    this.requestHandlers.set(
       ProcedureName.STOP_CHARGING_STATION,
       this.handleStopChargingStation.bind(this) as ProtocolRequestHandler
     );