From 5e8e29f4f47246cdc49449aa6ca472c64393f7e8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 8 Sep 2022 20:56:07 +0200 Subject: [PATCH] UI Server: Add a unique request handler for procedure forwarded to charging stations via BC MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ChargingStationWorkerBroadcastChannel.ts | 9 +- .../ui-services/AbstractUIService.ts | 2 +- .../ui-server/ui-services/UIService001.ts | 111 ++++++------------ src/types/UIProtocol.ts | 9 +- src/types/WorkerBroadcastChannel.ts | 4 +- src/ui/web/src/types/UIProtocol.ts | 4 +- 6 files changed, 52 insertions(+), 87 deletions(-) diff --git a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/ChargingStationWorkerBroadcastChannel.ts index 154e1afb..a67b9423 100644 --- a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/ChargingStationWorkerBroadcastChannel.ts @@ -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, diff --git a/src/charging-station/ui-server/ui-services/AbstractUIService.ts b/src/charging-station/ui-server/ui-services/AbstractUIService.ts index a393986e..356e908c 100644 --- a/src/charging-station/ui-server/ui-services/AbstractUIService.ts +++ b/src/charging-station/ui-server/ui-services/AbstractUIService.ts @@ -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); diff --git a/src/charging-station/ui-server/ui-services/UIService001.ts b/src/charging-station/ui-server/ui-services/UIService001.ts index 4be6a2f2..82d0a953 100644 --- a/src/charging-station/ui-server/ui-services/UIService001.ts +++ b/src/charging-station/ui-server/ui-services/UIService001.ts @@ -8,120 +8,85 @@ import { BroadcastChannelProcedureName } from '../../../types/WorkerBroadcastCha import type { AbstractUIServer } from '../AbstractUIServer'; import AbstractUIService from './AbstractUIService'; +const ProcedureNameToBroadCastChannelProcedureNameMap: Omit< + Record, + '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); - } } diff --git a/src/types/UIProtocol.ts b/src/types/UIProtocol.ts index 75685141..6468290d 100644 --- a/src/types/UIProtocol.ts +++ b/src/types/UIProtocol.ts @@ -23,21 +23,22 @@ export type ProtocolResponse = [string, ResponsePayload]; export type ProtocolRequestHandler = ( uuid?: string, + procedureName?: ProcedureName, payload?: RequestPayload ) => undefined | Promise | ResponsePayload | Promise; 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', diff --git a/src/types/WorkerBroadcastChannel.ts b/src/types/WorkerBroadcastChannel.ts index be59789b..e7017356 100644 --- a/src/types/WorkerBroadcastChannel.ts +++ b/src/types/WorkerBroadcastChannel.ts @@ -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', diff --git a/src/ui/web/src/types/UIProtocol.ts b/src/ui/web/src/types/UIProtocol.ts index 5921315c..443e2352 100644 --- a/src/ui/web/src/types/UIProtocol.ts +++ b/src/ui/web/src/types/UIProtocol.ts @@ -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 { -- 2.34.1