X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcharging-station%2Fbroadcast-channel%2FChargingStationWorkerBroadcastChannel.ts;h=26aa61d38e7c97149877fcb13584715afc430d34;hb=09e5a7a8ed45886b6bf1434c4e49c945ded4c7d8;hp=20f347fc48e2e920c342be8d920e5668206cc49a;hpb=a223d9be48ad8828e6aef060dd3c45d4f99ea9a9;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts index 20f347fc..26aa61d3 100644 --- a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts @@ -37,7 +37,13 @@ import { type StopTransactionRequest, type StopTransactionResponse } from '../../types/index.js' -import { Constants, convertToInt, isEmptyObject, logger } from '../../utils/index.js' +import { + Constants, + convertToInt, + isAsyncFunction, + isEmptyObject, + logger +} from '../../utils/index.js' import type { ChargingStation } from '../ChargingStation.js' import { getConfigurationKey } from '../ConfigurationKeyUtils.js' import { buildMeterValue } from '../ocpp/index.js' @@ -56,7 +62,7 @@ type CommandResponse = type CommandHandler = ( requestPayload?: BroadcastChannelRequestPayload // eslint-disable-next-line @typescript-eslint/no-invalid-void-type -) => Promise | void +) => Promise | CommandResponse | void export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChannel { private readonly commandHandlers: Map @@ -80,6 +86,12 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne await this.chargingStation.stop() } ], + [ + BroadcastChannelProcedureName.DELETE_CHARGING_STATIONS, + async (requestPayload?: BroadcastChannelRequestPayload) => { + await this.chargingStation.delete(requestPayload?.deleteConfiguration as boolean) + } + ], [ BroadcastChannelProcedureName.OPEN_CONNECTION, () => { @@ -256,7 +268,7 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne this.onmessageerror = this.messageErrorHandler.bind(this) as (message: unknown) => void } - private async requestHandler (messageEvent: MessageEvent): Promise { + private requestHandler (messageEvent: MessageEvent): void { const validatedMessageEvent = this.validateMessageEvent(messageEvent) if (validatedMessageEvent === false) { return @@ -279,42 +291,40 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne return } let responsePayload: BroadcastChannelResponsePayload | undefined - // eslint-disable-next-line @typescript-eslint/no-invalid-void-type - let commandResponse: CommandResponse | void - try { - commandResponse = await this.commandHandler(command, requestPayload) - if (commandResponse == null || isEmptyObject(commandResponse)) { + this.commandHandler(command, requestPayload) + .then(commandResponse => { + if (commandResponse == null || isEmptyObject(commandResponse)) { + responsePayload = { + hashId: this.chargingStation.stationInfo?.hashId, + status: ResponseStatus.SUCCESS + } + } else { + responsePayload = this.commandResponseToResponsePayload( + command, + requestPayload, + commandResponse + ) + } + }) + .catch(error => { + logger.error( + `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: Handle request error:`, + error + ) responsePayload = { hashId: this.chargingStation.stationInfo?.hashId, - status: ResponseStatus.SUCCESS - } - } else { - responsePayload = this.commandResponseToResponsePayload( + status: ResponseStatus.FAILURE, command, requestPayload, - commandResponse - ) - } - } catch (error) { - logger.error( - `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: Handle request error:`, - error - ) - responsePayload = { - hashId: this.chargingStation.stationInfo?.hashId, - status: ResponseStatus.FAILURE, - command, - requestPayload, + errorMessage: (error as OCPPError).message, + errorStack: (error as OCPPError).stack, + errorDetails: (error as OCPPError).details + } satisfies BroadcastChannelResponsePayload + }) + .finally(() => { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - commandResponse: commandResponse!, - errorMessage: (error as OCPPError).message, - errorStack: (error as OCPPError).stack, - errorDetails: (error as OCPPError).details - } - } finally { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.sendResponse([uuid, responsePayload!]) - } + this.sendResponse([uuid, responsePayload!]) + }) } private messageErrorHandler (messageEvent: MessageEvent): void { @@ -332,7 +342,16 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne if (this.commandHandlers.has(command)) { this.cleanRequestPayload(command, requestPayload) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - return await this.commandHandlers.get(command)!(requestPayload) + const commandHandler = this.commandHandlers.get(command)! + if (isAsyncFunction(commandHandler)) { + return await commandHandler(requestPayload) + } + return ( + commandHandler as ( + requestPayload?: BroadcastChannelRequestPayload + // eslint-disable-next-line @typescript-eslint/no-invalid-void-type + ) => CommandResponse | void + )(requestPayload) } throw new BaseError(`Unknown worker broadcast channel command: '${command}'`) }