X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStationWorkerBroadcastChannel.ts;h=dfb50ab1dd67d318f251ed88a953aae67011e276;hb=cf058664a0053a43b9ac08815b6e430179ff5ddf;hp=edb7475a097c522f4f76a1038a7a69342959cd42;hpb=1598b27c55255d49693d67950242661fefbe1d7d;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/ChargingStationWorkerBroadcastChannel.ts index edb7475a..dfb50ab1 100644 --- a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/ChargingStationWorkerBroadcastChannel.ts @@ -1,19 +1,33 @@ -import { RequestCommand } from '../types/ocpp/Requests'; +import BaseError from '../exception/BaseError'; +import type OCPPError from '../exception/OCPPError'; +import { RequestCommand, type StatusNotificationRequest } from '../types/ocpp/Requests'; +import type { StatusNotificationResponse } from '../types/ocpp/Responses'; import { + AuthorizationStatus, StartTransactionRequest, StartTransactionResponse, - StopTransactionReason, StopTransactionRequest, StopTransactionResponse, } from '../types/ocpp/Transaction'; import { BroadcastChannelProcedureName, BroadcastChannelRequest, + BroadcastChannelRequestPayload, + BroadcastChannelResponsePayload, + MessageEvent, } from '../types/WorkerBroadcastChannel'; -import ChargingStation from './ChargingStation'; +import { ResponseStatus } from '../ui/web/src/types/UIProtocol'; +import logger from '../utils/Logger'; +import Utils from '../utils/Utils'; +import type ChargingStation from './ChargingStation'; import WorkerBroadcastChannel from './WorkerBroadcastChannel'; -type MessageEvent = { data: unknown }; +const moduleName = 'ChargingStationWorkerBroadcastChannel'; + +type CommandResponse = + | StartTransactionResponse + | StopTransactionResponse + | StatusNotificationResponse; export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChannel { private readonly chargingStation: ChargingStation; @@ -21,46 +35,150 @@ 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; + this.onmessageerror = this.messageErrorHandler.bind(this) as (message: MessageEvent) => void; } - private async handleRequest(messageEvent: MessageEvent): Promise { - const [, command, payload] = messageEvent.data as BroadcastChannelRequest; - - if (payload.hashId !== this.chargingStation.hashId) { + private async requestHandler(messageEvent: MessageEvent): Promise { + if (this.isResponse(messageEvent.data) === true) { return; } + const [uuid, command, requestPayload] = this.validateMessageEvent(messageEvent) + .data as BroadcastChannelRequest; + + if (requestPayload?.hashIds !== undefined || requestPayload?.hashId !== undefined) { + if ( + requestPayload?.hashId === undefined && + requestPayload?.hashIds?.includes(this.chargingStation.stationInfo.hashId) === false + ) { + return; + } + if ( + requestPayload?.hashIds === undefined && + requestPayload?.hashId !== this.chargingStation.stationInfo.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 = { + hashId: this.chargingStation.stationInfo.hashId, + status: ResponseStatus.SUCCESS, + }; + } else { + responsePayload = { + hashId: this.chargingStation.stationInfo.hashId, + status: this.commandResponseToResponseStatus(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, + commandResponse, + errorMessage: (error as Error).message, + errorStack: (error as Error).stack, + errorDetails: (error as OCPPError).details, + }; + } + this.sendResponse([uuid, responsePayload]); + } - // TODO: return a response stating the command success or failure + private messageErrorHandler(messageEvent: MessageEvent): void { + logger.error( + `${this.chargingStation.logPrefix()} ${moduleName}.messageErrorHandler: Error at handling message:`, + { messageEvent } + ); + } + + private async commandHandler( + command: BroadcastChannelProcedureName, + requestPayload: BroadcastChannelRequestPayload + ): Promise { switch (command) { + 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; 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, + true ), - idTag: this.chargingStation.getTransactionIdTag(payload.transactionId), - reason: StopTransactionReason.NONE, + idTag: requestPayload.idTag, + reason: requestPayload.reason, }); + case BroadcastChannelProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR: + this.chargingStation.startAutomaticTransactionGenerator(requestPayload.connectorIds); break; - case BroadcastChannelProcedureName.START_CHARGING_STATION: - this.chargingStation.start(); - break; - case BroadcastChannelProcedureName.STOP_CHARGING_STATION: - await this.chargingStation.stop(); + case BroadcastChannelProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR: + this.chargingStation.stopAutomaticTransactionGenerator(requestPayload.connectorIds); break; + case BroadcastChannelProcedureName.STATUS_NOTIFICATION: + return this.chargingStation.ocppRequestService.requestHandler< + StatusNotificationRequest, + StatusNotificationResponse + >(this.chargingStation, RequestCommand.STATUS_NOTIFICATION, { + connectorId: requestPayload.connectorId, + errorCode: requestPayload.errorCode, + status: requestPayload.status, + ...(requestPayload.info && { info: requestPayload.info }), + ...(requestPayload.timestamp && { timestamp: requestPayload.timestamp }), + ...(requestPayload.vendorId && { vendorId: requestPayload.vendorId }), + ...(requestPayload.vendorErrorCode && { + vendorErrorCode: requestPayload.vendorErrorCode, + }), + }); + 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 ( + Utils.isEmptyObject(commandResponse) || + commandResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED + ) { + return ResponseStatus.SUCCESS; } + return ResponseStatus.FAILURE; } }