From: Jérôme Benoit Date: Tue, 23 Aug 2022 15:01:18 +0000 (+0200) Subject: Unify request and response handler naming X-Git-Tag: v1.1.66~23 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=02a6943a30a9a6c93e5dcbdb79aa8d9746ae368e;p=e-mobility-charging-stations-simulator.git Unify request and response handler naming Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/ChargingStationWorkerBroadcastChannel.ts index 7212f09d..6d38f06c 100644 --- a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/ChargingStationWorkerBroadcastChannel.ts @@ -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 { + private async requestHandler(messageEvent: MessageEvent): Promise { const [, command, payload] = messageEvent.data as BroadcastChannelRequest; if (payload.hashId !== this.chargingStation.hashId) { diff --git a/src/charging-station/WorkerBroadcastChannel.ts b/src/charging-station/WorkerBroadcastChannel.ts index d1d430c3..2099d695 100644 --- a/src/charging-station/WorkerBroadcastChannel.ts +++ b/src/charging-station/WorkerBroadcastChannel.ts @@ -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); + } } diff --git a/src/charging-station/ui-server/AbstractUIServer.ts b/src/charging-station/ui-server/AbstractUIServer.ts index 152749e5..e0b12071 100644 --- a/src/charging-station/ui-server/AbstractUIServer.ts +++ b/src/charging-station/ui-server/AbstractUIServer.ts @@ -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; } diff --git a/src/charging-station/ui-server/UIWebSocketServer.ts b/src/charging-station/ui-server/UIWebSocketServer.ts index d75b9ab8..66980f61 100644 --- a/src/charging-station/ui-server/UIWebSocketServer.ts +++ b/src/charging-station/ui-server/UIWebSocketServer.ts @@ -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 { diff --git a/src/charging-station/ui-server/ui-services/AbstractUIService.ts b/src/charging-station/ui-server/ui-services/AbstractUIService.ts index 2e35299c..30ad449a 100644 --- a/src/charging-station/ui-server/ui-services/AbstractUIService.ts +++ b/src/charging-station/ui-server/ui-services/AbstractUIService.ts @@ -23,13 +23,13 @@ const moduleName = 'AbstractUIService'; export default abstract class AbstractUIService { protected readonly version: ProtocolVersion; protected readonly uiServer: AbstractUIServer; - protected readonly messageHandlers: Map; + protected readonly requestHandlers: Map; protected workerBroadcastChannel: WorkerBroadcastChannel; constructor(uiServer: AbstractUIServer, version: ProtocolVersion) { this.version = version; this.uiServer = uiServer; - this.messageHandlers = new Map([ + this.requestHandlers = new Map([ [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 { + public async requestHandler(request: RawData): Promise { 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); } diff --git a/src/charging-station/ui-server/ui-services/UIService001.ts b/src/charging-station/ui-server/ui-services/UIService001.ts index 1f926c93..5c9b7dc4 100644 --- a/src/charging-station/ui-server/ui-services/UIService001.ts +++ b/src/charging-station/ui-server/ui-services/UIService001.ts @@ -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 );