X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStationWorkerBroadcastChannel.ts;h=42448275432337772fe80a691a5364840680f068;hb=852a4c5f07886502e278d44fda06efdfa8c711d8;hp=5115d6cf7e99bb6f0114690aeb48b95f0c063a71;hpb=89b7a234c161f2c68b6a9499ff698488e1a35c6b;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/ChargingStationWorkerBroadcastChannel.ts index 5115d6cf..42448275 100644 --- a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/ChargingStationWorkerBroadcastChannel.ts @@ -1,7 +1,7 @@ -import { BroadcastChannel } from 'worker_threads'; - +import BaseError from '../exception/BaseError'; import { RequestCommand } from '../types/ocpp/Requests'; import { + AuthorizationStatus, StartTransactionRequest, StartTransactionResponse, StopTransactionReason, @@ -11,57 +11,135 @@ import { import { BroadcastChannelProcedureName, BroadcastChannelRequest, + BroadcastChannelRequestPayload, + BroadcastChannelResponsePayload, + MessageEvent, } from '../types/WorkerBroadcastChannel'; -import ChargingStation from './ChargingStation'; +import { ResponseStatus } from '../ui/web/src/type/UIProtocol'; +import logger from '../utils/Logger'; +import type ChargingStation from './ChargingStation'; +import WorkerBroadcastChannel from './WorkerBroadcastChannel'; + +const moduleName = 'ChargingStationWorkerBroadcastChannel'; -type MessageEvent = { data: unknown }; +type CommandResponse = StartTransactionResponse | StopTransactionResponse; -export default class ChargingStationWorkerBroadcastChannel extends BroadcastChannel { +export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChannel { private readonly chargingStation: ChargingStation; constructor(chargingStation: ChargingStation) { - super('worker'); + super(); this.chargingStation = chargingStation; - this.onmessage = this.handleRequest.bind(this) as (message: MessageEvent) => void; + this.onmessage = this.requestHandler.bind(this) as (message: MessageEvent) => void; + this.onmessageerror = this.messageErrorHandler.bind(this) as (message: MessageEvent) => void; } - private async handleRequest(messageEvent: MessageEvent): Promise { - const [, command, payload] = messageEvent.data as BroadcastChannelRequest; + private async requestHandler(messageEvent: MessageEvent): Promise { + if (this.isResponse(messageEvent.data)) { + return; + } + this.validateMessageEvent(messageEvent); + + const [uuid, command, requestPayload] = messageEvent.data as BroadcastChannelRequest; - if (payload.hashId !== this.chargingStation.hashId) { + if ( + requestPayload?.hashId === undefined && + (requestPayload?.hashIds as string[])?.includes(this.chargingStation.hashId) === false + ) { return; } + if ( + requestPayload?.hashIds === undefined && + requestPayload?.hashId !== this.chargingStation.hashId + ) { + return; + } + if (requestPayload?.hashId !== undefined) { + logger.warn( + `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'hashId' field usage in PDU is deprecated, use 'hashIds' instead` + ); + } + + let responsePayload: BroadcastChannelResponsePayload; + let commandResponse: CommandResponse; + try { + commandResponse = await this.commandHandler(command, requestPayload); + if (commandResponse === undefined) { + responsePayload = { status: ResponseStatus.SUCCESS }; + } else { + responsePayload = { status: this.commandResponseToResponseStatus(commandResponse) }; + } + } catch (error) { + logger.error( + `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: Handle request error:`, + error + ); + responsePayload = { + status: ResponseStatus.FAILURE, + command, + requestPayload, + commandResponse, + errorMessage: (error as Error).message, + errorStack: (error as Error).stack, + }; + } + this.sendResponse([uuid, responsePayload]); + } + + private messageErrorHandler(messageEvent: MessageEvent): void { + logger.error( + `${this.chargingStation.logPrefix()} ${moduleName}.messageErrorHandler: Error at handling message:`, + { messageEvent, messageEventData: messageEvent.data } + ); + } - // TODO: return a response stating the command success or failure + private async commandHandler( + command: BroadcastChannelProcedureName, + requestPayload: BroadcastChannelRequestPayload + ): Promise { switch (command) { case BroadcastChannelProcedureName.START_TRANSACTION: - await this.chargingStation.ocppRequestService.requestHandler< + return this.chargingStation.ocppRequestService.requestHandler< StartTransactionRequest, StartTransactionResponse >(this.chargingStation, RequestCommand.START_TRANSACTION, { - connectorId: payload.connectorId, - idTag: payload.idTag, + connectorId: requestPayload.connectorId, + idTag: requestPayload.idTag, }); - break; case BroadcastChannelProcedureName.STOP_TRANSACTION: - await this.chargingStation.ocppRequestService.requestHandler< + return this.chargingStation.ocppRequestService.requestHandler< StopTransactionRequest, StopTransactionResponse >(this.chargingStation, RequestCommand.STOP_TRANSACTION, { - transactionId: payload.transactionId, + transactionId: requestPayload.transactionId, meterStop: this.chargingStation.getEnergyActiveImportRegisterByTransactionId( - payload.transactionId + requestPayload.transactionId ), - idTag: this.chargingStation.getTransactionIdTag(payload.transactionId), + idTag: this.chargingStation.getTransactionIdTag(requestPayload.transactionId), reason: StopTransactionReason.NONE, }); - break; case BroadcastChannelProcedureName.START_CHARGING_STATION: this.chargingStation.start(); break; case BroadcastChannelProcedureName.STOP_CHARGING_STATION: await this.chargingStation.stop(); break; + case BroadcastChannelProcedureName.OPEN_CONNECTION: + this.chargingStation.openWSConnection(); + break; + case BroadcastChannelProcedureName.CLOSE_CONNECTION: + this.chargingStation.closeWSConnection(); + break; + default: + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + throw new BaseError(`Unknown worker broadcast channel command: ${command}`); + } + } + + private commandResponseToResponseStatus(commandResponse: CommandResponse): ResponseStatus { + if (commandResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { + return ResponseStatus.SUCCESS; } + return ResponseStatus.FAILURE; } }