X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStationWorkerBroadcastChannel.ts;h=0c3af5887ee06b494269b426df6736df25374a37;hb=60ddad538d0a01ece43f4f70928a9decf3531dda;hp=42448275432337772fe80a691a5364840680f068;hpb=852a4c5f07886502e278d44fda06efdfa8c711d8;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/ChargingStationWorkerBroadcastChannel.ts index 42448275..0c3af588 100644 --- a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/ChargingStationWorkerBroadcastChannel.ts @@ -15,7 +15,7 @@ import { BroadcastChannelResponsePayload, MessageEvent, } from '../types/WorkerBroadcastChannel'; -import { ResponseStatus } from '../ui/web/src/type/UIProtocol'; +import { ResponseStatus } from '../ui/web/src/types/UIProtocol'; import logger from '../utils/Logger'; import type ChargingStation from './ChargingStation'; import WorkerBroadcastChannel from './WorkerBroadcastChannel'; @@ -38,26 +38,27 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca if (this.isResponse(messageEvent.data)) { return; } - this.validateMessageEvent(messageEvent); + const [uuid, command, requestPayload] = this.validateMessageEvent(messageEvent) + .data as BroadcastChannelRequest; - const [uuid, command, requestPayload] = messageEvent.data as BroadcastChannelRequest; - - 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` - ); + 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; @@ -65,9 +66,15 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca try { commandResponse = await this.commandHandler(command, requestPayload); if (commandResponse === undefined) { - responsePayload = { status: ResponseStatus.SUCCESS }; + responsePayload = { + hashId: this.chargingStation.stationInfo.hashId, + status: ResponseStatus.SUCCESS, + }; } else { - responsePayload = { status: this.commandResponseToResponseStatus(commandResponse) }; + responsePayload = { + hashId: this.chargingStation.stationInfo.hashId, + status: this.commandResponseToResponseStatus(commandResponse), + }; } } catch (error) { logger.error( @@ -75,6 +82,7 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca error ); responsePayload = { + hashId: this.chargingStation.stationInfo.hashId, status: ResponseStatus.FAILURE, command, requestPayload, @@ -89,7 +97,7 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca private messageErrorHandler(messageEvent: MessageEvent): void { logger.error( `${this.chargingStation.logPrefix()} ${moduleName}.messageErrorHandler: Error at handling message:`, - { messageEvent, messageEventData: messageEvent.data } + { messageEvent } ); } @@ -98,6 +106,18 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca 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: return this.chargingStation.ocppRequestService.requestHandler< StartTransactionRequest, @@ -113,22 +133,17 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca >(this.chargingStation, RequestCommand.STOP_TRANSACTION, { transactionId: requestPayload.transactionId, meterStop: this.chargingStation.getEnergyActiveImportRegisterByTransactionId( - requestPayload.transactionId + requestPayload.transactionId, + true ), idTag: this.chargingStation.getTransactionIdTag(requestPayload.transactionId), - reason: StopTransactionReason.NONE, + ...(requestPayload.reason && { reason: requestPayload.reason }), }); - case BroadcastChannelProcedureName.START_CHARGING_STATION: - this.chargingStation.start(); + case BroadcastChannelProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR: + this.chargingStation.startAutomaticTransactionGenerator(requestPayload.connectorIds); 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(); + case BroadcastChannelProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR: + this.chargingStation.stopAutomaticTransactionGenerator(requestPayload.connectorIds); break; default: // eslint-disable-next-line @typescript-eslint/restrict-template-expressions